How can a parent process be made to wait till the completion of one or more of its child process?

How can a parent process be made to wait till the completion of one or more of its child process?

wait waits for a child process to terminate, and returns that child process’s pid . On error (eg when there are no child processes), -1 is returned. So, basically, the code keeps waiting for child processes to finish, until the wait ing errors out, and then you know they are all finished.

What happens when a process parent finishes before the child?

When a parent process dies before a child process, the kernel knows that it’s not going to get a wait call, so instead it makes these processes “orphans” and puts them under the care of init (remember mother of all processes). Init will eventually perform the wait system call for these orphans so they can die.

What happens when a child process exits?

When a child process terminates, some information is returned to the parent process. When a child process terminates before the parent has called wait, the kernel retains some information about the process, such as its exit status, to enable its parent to call wait later.

Which process executes first parent or child?

There is not really one executing before the other. It is simply that the parent will fork() then wait() for the child to complete. It may even fork several times if you use a piped series of commands for instance.

How to make child process die after parent exits?

When a parent process dies (for any reason) the child’s parent process becomes process 1. If the child simply polls periodically, it can check if its parent is 1. If it is, the child should exit.

When to return the exit status of a child process?

It is known that fork () system call is used to create a new process which becomes child of the caller process. Upon exit, the child leaves an exit status that should be returned to the parent.

Why does parent process wait ( to terminate ) until all of its children?

The main reason for this convention is that if the parent process terminates before the children, the children’s exit code will be lost and the child process may remain a zombie for a while. After a child process terminates, it will stay around (and consume memory) until the exit code is read.

Why does the parent process need to read the exit code?

It is generally the parent process’s responsibility to read the exit code and allow the zombie process to completely go away. Since the parent should keep track of the children, it will hopefully be quick to clean up. If the parent process does not do that, you have a resource leak.