The shell can wait for the child to terminate by calling waitpid:
int n; int status; ... n = waitpid(child_pid, &status, 0);
This would cause the parent (e.g., the shell) to wait until the child process whose pid is child_pid to terminate.
- The return value would be equal to child_pid or -1 if no such process existed or has already terminated and been waited for.
- The status integer has some bits indicating the way the child terminated (e.g. normally or killed by a signal) and if terminated normally, its exit or return value.
The last parameter specifies options for waiting; 0, means wait for the child to terminate, but other options can specify wait until the child's state changes, not just termination.
A successful call to waitpid will reap the child process; free its process table entry and discard all information about the process.