You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于waitpid()与getpid()的理解确认及疑问咨询

Clarifying 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:

  1. The kernel puts the parent process into a sleeping (blocked) state.
  2. 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.
  3. 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: Returns 0 immediately (signals the child is still alive, no termination to report).
  • 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.
  • 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 sets errno to ECHILD (you can check this with perror() to debug).
  • Call is interrupted by a signal:
    • Returns -1, and sets errno to EINTR.

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, with errno set to ECHILD. 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 like wait()).
  • 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 of pid.

内容的提问来源于stack exchange,提问作者Chris T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:00:26