关于waitpid()与getpid()的理解确认及疑问咨询
getpid() and waitpid() for Parent/Child Process Management Hey Chris, let's break this down clearly to confirm your understanding and fill in the gaps you're curious about!
First: Your understanding of getpid() is 100% correct!
You nailed it:
getpid()returns the PID of the process that calls it. If the parent process runs it, you get the parent's PID; if the child runs it, you get the child's PID.- The return type is indeed
pid_t(a signed integer type designed specifically for process IDs).
Now let's unpack waitpid() thoroughly
First, let's cover the implementation basics, then dive into return values across different scenarios.
How waitpid() works under the hood
The kernel keeps track of every process's child processes and their states (running, terminated, etc.). When a parent calls waitpid() without the WNOHANG flag:
- The kernel puts the parent process into a sleeping (blocked) state.
- It stays blocked until one of two things happens:
- The target child process terminates (either normally or via a signal), or
- The parent is interrupted by an unhandled signal.
- Once the child terminates, the kernel cleans up the child's "zombie" process entry (a temporary state where the kernel retains exit status info until the parent retrieves it) and wakes the parent, passing back the child's PID and exit status.
If you use the WNOHANG flag, the kernel checks the child's state immediately and returns right away—no blocking, even if the child is still running.
Return values of waitpid() across scenarios
Let's break this down by who's calling, and the child's state:
1. Parent process calls waitpid() (target is a valid child)
- Child is still running:
- Without
WNOHANG: Parent blocks until child terminates, then returns the child's PID. - With
WNOHANG: Returns0immediately (signals the child is still alive, no termination to report).
- Without
- Child has already terminated (is a zombie process):
- Regardless of
WNOHANG, returns the child's PID immediately. The kernel also cleans up the zombie process so it no longer takes up system resources.
- Regardless of
- No matching child exists (e.g., you specified a PID that's not your child, or all children have been waited on):
- Returns
-1, and setserrnotoECHILD(you can check this withperror()to debug).
- Returns
- Call is interrupted by a signal:
- Returns
-1, and setserrnotoEINTR.
- Returns
2. Child process calls waitpid()
Unless the child has itself forked another process (creating its own child), it has no children to wait on. So:
- Returns
-1, witherrnoset toECHILD. The call doesn't do anything useful here unless the child is a parent to other processes.
Quick note on the pid argument of waitpid()
Just to clarify, the first argument to waitpid() lets you target specific children:
pid > 0: Wait for the child with exactly this PID.pid = -1: Wait for any of your child processes (behaves likewait()).pid = 0: Wait for any child process in the same process group as the parent.pid < -1: Wait for any child process whose process group ID is the absolute value ofpid.
内容的提问来源于stack exchange,提问作者Chris T

