wait和waitpid

本文详细解析了Linux中wait与waitpid函数的工作原理及使用方法,包括如何通过这些函数处理子进程的终止状态,并介绍了与状态检查相关的宏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

故事

当一个里程终止时,可以是正常的终止,也可以是不正常的终止,linux kernel会发一个SIGCHLD的signal给parent。终止一个child是一个异步的事件,所以parent必须要能够处理child的事件,这就需要wait或waitpid来帮忙实现。

wait或waitpid可以

  • Block, if all of its children are still running
  • Return immediately with the termination status of a child, if a child has
    terminated and is waiting for its termination status to be fetched
  • Return immediately with an error, if it doesn’t have any child processes

函数原形

#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);

Both return: process ID if OK, 0 (see later), or1 on error

wait与waitpid的不同点

  • The wait function can block the caller until a child process terminates, whereas
    waitpid has an option that prevents it from blocking.
  • The waitpid function doesn’t wait for the child that terminates first; it has a
    number of options that control which process it waits for

专用的宏用于处理status

If status is not NULL, wait() and waitpid() store status information in the int to which it points.  This integer
       can be inspected with the following macros (which take the integer itself as an argument, not a pointer to it, as
       is done in wait() and waitpid()!):

       WIFEXITED(status)
              returns true if the child terminated normally, that is, by calling exit(3) or _exit(2),  or  by  returning
              from main().

       WEXITSTATUS(status)
              returns  the  exit status of the child.  This consists of the least significant 8 bits of the status argu‐
              ment that the child specified in a call to exit(3) or _exit(2) or as the argument for a  return  statement
              in main().  This macro should be employed only if WIFEXITED returned true.

       WIFSIGNALED(status)
              returns true if the child process was terminated by a signal.

       WTERMSIG(status)
              returns  the  number  of  the  signal  that  caused  the child process to terminate.  This macro should be
              employed only if WIFSIGNALED returned true.

       WCOREDUMP(status)
              returns true if the child produced a core dump.   This  macro  should  be  employed  only  if  WIFSIGNALED
              returned  true.  This macro is not specified in POSIX.1-2001 and is not available on some UNIX implementa‐
              tions (e.g., AIX, SunOS).  Only use this enclosed in #ifdef WCOREDUMP ... #endif.

       WIFSTOPPED(status)
              returns true if the child process was stopped by delivery of a signal; this is possible only if  the  call
              was done using WUNTRACED or when the child is being traced (see ptrace(2)).

       WSTOPSIG(status)
              returns  the  number  of the signal which caused the child to stop.  This macro should be employed only if
              WIFSTOPPED returned true.

       WIFCONTINUED(status)
              (since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT.

waitpid的pid参数

       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.

waitpid的option参数

       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.

小结:wait VS waitpid

1. The waitpid function lets us wait for one particular process, whereas the wait
function returns the status of any terminated child. We’ll return to this feature
when we discuss the popen function.
2. The waitpid function provides a nonblocking version of wait. There are
times when we want to fetch a child’s status, but we don’t want to block.
3. The waitpid function provides support for job control with the WUNTRACED
and WCONTINUED options.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JXES智能生态系统

如文章对你有用,请作者喝个咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值