I fork() a parent process to a child, the PID returned by fork() is stored in the parent’s memory, then time passes and the child terminates; Now can I determine if the PID value stored in the parent’s memory still refers to the same forked child, and how can I ensure that this PID doesn’t refer to a different process with the same PID, which may eventually have born after the child terminated?
Share
The operating system cannot reuse the child’s PID until the parent has acknowledged that it knows the child has stopped executing.
The parent makes the acknowledgment using the
waitandwaitpidcalls. The children that terminate are kept in a “zombie” state while the parent doesn’t call these functions. After these calls return the parent will know that if there’s a process running with the same PID that the child had, it’s not the child.For extra safety you might be interested in checking the parent PID of the child process.