1

From Advanced Programming in the Unix Environment:

A process can set the process group ID of only itself or any of its children. Furthermore, it can’t change the process group ID of one of its children after that child has called one of the exec functions.

Why can't it "change the process group ID of one of its children after that child has called one of the exec functions"? Thanks.

2 Answers 2

1

I do not know the "official" reason but I would guess that the idea is that a process shall not have to expect that its PGID is suddenly changed.

So this is allowed after a fork so that shell pipelines can be set up but after the execve() the new binary find a certain state, and this shall be permanent (until the new binary decides to change it).

0

I had the same confusion: why, after execve, can't we change the pgid of a child but the child itself can change its own pgid?

Listed here are few reasons I found might be the cause of this:

  1. Empty group issues: when we set up pgid once and execve, we set that process group as the foreground process group. Now tty points to the pid struct of that process group. But if we were allowed to change the pgid again later, then tty would still point to the older pgid pid struct. So signals would not be delivered correctly.

  2. Race condition window: when we fork a child, we don't know who will be scheduled first. So of both calls for setpgid() (but this is only if we specifically do that or shell does it, if not it's inherited by parent pgid), one is idempotent but still okay. But after execve, it's prohibited to call setpgid by the parent because the child might have won the race and may have made decisions based on that pgid. Now, if the parent tried to change it, it would cause undefined behaviors.

  3. Runtime assumptions would break: a process might use or make decisions at runtime based on its pgid, so if we change the pgid, it might cause undefined behaviors like if the process checks if it is the foreground process group. It might see; if it's not, then it will go back to sleep, but no one knows if it's sleeping or no one to wake it up.

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.

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.