Reputation: 67221
When using the fork system call, I have seen that many a times that the parent calls waitpid so that the child can finish.
My question here is does the child send a notification to the parent preocess. Without using any shared resource, how can a parent identify that the chils has been completed?
Upvotes: 1
Views: 1001
Reputation: 118500
In addition to receiving SIGCHLD
as mentioned by Erik, you can poll the child for events of interest by using WNOHANG
. Here's an example:
pid_t pid = fork();
if (pid) {
while (1) {
int status;
int waitpid(pid, &status, WNOHANG);
if (pid == waitpid && WIFEXITED(status))
break;
// do other stuff
}
}
Upvotes: 0
Reputation: 2969
In Linux Operating system, every child is having some parent and even if the parent dies before the child, the child is inherited by init(PID:1) process. This thing is done so that there should not any zombie process(process entry is there in the process table but process is actually already died) which is taking memory space for no use.
The kernel keeps an eye on all the processes which are died either due to their complete execution or due to some other reason(like invalid memory access) and retains some information like child's exit status. When the child terminates a SIGCHLD
signal is sent to the parent. By default the signal is simply ignored. However commonly wait()
system call implemented in a handler for the SIGCHLD, so that the parent may act upon the exit status of the child.
Upvotes: 1
Reputation: 91270
The OS sends a SIGCHLD
to the parent when the child exits. You can choose to poll waitpid()
using WNOHANG
, or to just check with wait()
after receiving a SIGCHLD
. The child process doesn't need to do anything in particular, this is all managed by the OS.
Upvotes: 5