Why can a process not change its Pgid if it is a session leader? What problems or conflicts does this avoid?
2 Answers
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.
-
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 ??Bhushitha Hashan– Bhushitha Hashan2026-04-29 13:30:15 +00:00Commented yesterday
-
2The "certain security problems" refers to using an arbitrary value for new
PGID.Andrei Borzenkov– Andrei Borzenkov2026-04-29 17:56:27 +00:00Commented yesterday
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.
-
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 tableBhushitha Hashan– Bhushitha Hashan2026-04-30 01:42:36 +00:00Commented yesterday