Continuing our exploration of processes, we look at how to wait for a child process and the way that process IDs are recycled by the kernel.
- Using waitpid to wait until a child process has completed.
- A process ID is an unsigned short integer and it must be unique to all running processes on the machine—
which means that there are not a lot of process IDs available, and IDs must be recycled. A processed ID can only be recycled after it has been reaped by the parent process using waitpid, so that the process isn’t confused with another one that recycles the PID.
- If a parent process exits without waiting for a child process to complete, the orphaned child process is adopted by the init process.
- Passing -1 instead of a PID to waitpid allows the waitpid call to complete when any child process finishes.The video says 0 instead of -1. Although 0 also works in the example, using -1 is more general, and wait corresponds to using -1.
- Using execve to run a specified program in place of the current program.
- Combining fork and execve to run a program as a separate process.