培养对 waitpid() 和 getpid() 的正确理解

Developing a correct understanding of waitpid() and getpid()

我正在学习我的系统编程 forksexecl 和 parent 以及 child 进程 class。令我困惑的一件事是 waitpid()getpid()。有人可以确认或纠正我对这两个功能的理解吗?

getpid() 将 return 调用它的任何进程的进程 ID。如果 parent 调用它,它 return 是 parent 的 pid。 child 同样如此。 (根据联机帮助页,它实际上 return 是一个类型 pid_t 的值)。

waitpid() 似乎更复杂。我知道如果我在 parent 进程中使用它,没有任何标志来防止它阻塞(使用 WNOHANG),它将停止 parent 进程直到 child进程终止。然而,我有点不确定 waitpid() 是如何管理这一切的。 waitpid() 也 returns pid_tpid_t waitpid() return 的值是多少?根据 parent 或 child 调用它,以及 child 进程是否仍在 运行 或已终止,这会如何变化?

waitpid"shall only return the status of a child process"(来自POSIX spec)。所以 pid_t waitpid returns 属于调用 waitpid 的进程的当前或前 children 之一。例如,如果 child 最近终止,则 return 就是 child 的 PID。

waitpid 仅在从 parent 进程调用时有用。如果从没有任何 children 的进程调用,它 returns ECHILD.

waitpid 可以检查 children 的状态,这些 children 已经终止,或者最近停止或继续(例如,来自 shell 的 ^Z)。规范中的各种 pid/option 参数组合告诉您可以 return 的各种类型的信息。例如,WCONTINUED 选项请求状态 recently-continued children 而不是 recently-terminated children.

你对getpid的理解是正确的,return是运行进程的PID。

waitpid 用于(如您所说)阻止进程的执行(除非 WNOHANG 被传递)并在进程的一个(或更多)child 时恢复执行 结束。 waitpid returns 状态已更改的 child 的 pid,-1 失败。如果 WNOHANG 已指定但 child 未指定,它也可以 return 0 改变了状态。参见:

man waitpid

RETURN VALUE

waitpid(): on success, returns the process ID of the child whose state has changed; if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned. On error, -1 is returned.

根据传递给 waitpid 的参数,它的行为会有所不同。这里 我再次引用手册页:

man waitpid

pid_t waitpid(pid_t pid, int *wstatus, int options);

...

The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument, as described below:

The value of pid can be:

  • < -1: meaning wait for any child process whose process group ID is equal to the absolute value of pid.
  • -1: meaning wait for any child process.
  • 0: meaning wait for any child process whose process group ID is equal to that of the calling process.
  • > 0: meaning wait for the child whose process ID is equal to the value of pid.

The value of options is an OR of zero or more of the following constants:

  • WNOHANG: return immediately if no child has exited.
  • WUNTRACED also return if a child has stopped (but not traced via ptrace(2)). Status for traced children which have stopped is provided even if this option is not specified.
  • WCONTINUED (since Linux 2.6.10) also return if a stopped child has been resumed by delivery of SIGCONT.
  • I'm a little unsure as to how waitpid() manages all this

waitpid 是一个系统调用,OS 处理这个。

  • How does this change depending on whether or not a parent or child calls it, and whether or not a child process is still running, or has terminated?

wait 只能由已执行 fork() 的进程调用。所以 parent 进程应该校准 wait()/waitpid。如果 child 进程还没有调用 fork(),则不需要调用其中任何一个函数。然而,如果 child 进程调用了 fork(),那么它也应该调用 wait()/waitpid().

这些函数的行为在手册页中有很好的解释,我引用了其中的重要部分。您应该阅读整个手册页 以便更好地理解它。