0

Why can a process not change its Pgid if it is a session leader? What problems or conflicts does this avoid?

New contributor
Bhushitha Hashan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

1

https://pubs.opengroup.org/onlinepubs/9699919799/functions/setpgid.html claims that enabling this would present "certain security problems". I don't know which these are, my suspicion is that it would either enable race conditions by assuming a process group ID of a recently exited group, or could lead to ID collisions when flawed PRNGs are used to generate IDs. Always using the PID of a related process apparently avoids this.

2
  • or could it be that if we allowed session leader to join another pgid in the same session it would also get the signals for that group and might be killed by a signal and now session leader is gone and behaviors are undefined ?? Commented yesterday
  • 2
    The "certain security problems" refers to using an arbitrary value for new PGID. Commented yesterday
1

You need not forget the reason process groups and sessions were implemented - job control.

There is shell that performs job control and there are (child) processes that are managed by this shell in units of process groups identified by PGID. Allowing shell to belong to one of process groups it manages would be confusing at best and dangerous at worst. Consider sending SIGKILL to the process group to which the shell itself belongs.

So the restriction is not as much about an abstract "session leader" as about shell - it is just that traditionally the shell was also the session leader for all processes it managed which provided a simple and efficient way to make sure shell and jobs it manages are separated.

1
  • Thank you and this is the clearest explanation I've too found after a lot of digging. I actually reasoned my way to something similar before posting: if the session leader joined one of its child groups, and then tried to do cleanup by sending SIGKILL to that group's PGID, it would kill itself before finishing , leaving the remaining jobs without a cleanup signal, effectively making them true orphans.So it's not just that the shell gets accidentally killed , it's that the entire cleanup chain breaks mid way. The shell dies before it can signal the rest of its job table Commented yesterday

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.