wait3,wait4 用法

本文属于互联网文章集合。

在阅读redis代码的时候看到的一些新知识,新用法,wait3的用法。

redis的用法。

 int statloc;
        pid_t pid;

        if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {
            int exitcode = WEXITSTATUS(statloc);
            int bysignal = 0;

            if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);

所以,我们要了解一下wait3,wait4


wait3, wait4 - wait for process to change state, BSD style

Synopsis

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>

pid_t wait3(int *status, int options,
            struct rusage *rusage);

pid_t wait4(pid_t pid, int *status, int options,
            struct rusage *rusage);

以下为转载

http://blog.sina.com.cn/s/blog_65bda7120100kjxx.html

wait3和wait4函数除了可以获取子进程状态转变信息外,还可以获得子进程的资源使用信息。

    pid_t wait3 ( int *status, int option, struct rusage *ru );

    pid_t wait4 ( pid_t pid, int *status, int option, struct rusage *ru );

    option的可选值有:WNOHANG、WCONTINUED、WUNTRACED。

    wait3等待所有的子进程;wait4可以像waitpid一样指定要等待的子进程:pid>0表示子进程ID;pid=0表示当前进程组中的子进程;pid=-1表示等待所有子进程;pid<-1表示进程组ID为pid绝对值的子进程。

    通过ru指针可以返回子进程的资源使用情况。

    struct rusage {
                struct timeval ru_utime;
                struct timeval ru_stime;
                long   ru_maxrss;       
                long   ru_ixrss;        
                long   ru_idrss;        
                long   ru_isrss;        
                long   ru_minflt;       
                long   ru_majflt;       
                long   ru_nswap;        
                long   ru_inblock;      

                long   ru_oublock;      
                long   ru_msgsnd;       
                long   ru_msgrcv;       
                long   ru_nsignals;     
                long   ru_nvcsw;        
                long   ru_nivcsw;       
            };

    也可以通过getrusage函数获取进程资源使用情况。

    int getrusage ( int who, struct rusage *ru );

    who可取RUSAGE_SELF、RUSAGE_CHILDREN,分别获取当前进程的资源使用情况和所有已终止且被父进程获取其终止状态的所有子进程的资源使用总情况。


下面是一些宏的用法。

If the exit status value (*note Program Termination::) of the child
process is zero, then the status value reported by `waitpid' or `wait'
is also zero. You can test for other kinds of information encoded in
the returned status value using the following macros. These macros are
defined in the header file `sys/wait.h'.

-- Macro: int WIFEXITED (int STATUS)
     This macro returns a nonzero value if the child process terminated
     normally with `exit' or `_exit'.

-- Macro: int WEXITSTATUS (int STATUS)
     If `WIFEXITED' is true of STATUS, this macro returns the low-order
     8 bits of the exit status value from the child process. *Note
     Exit Status::.

-- Macro: int WIFSIGNALED (int STATUS)
     This macro returns a nonzero value if the child process terminated
     because it received a signal that was not handled. *Note Signal
     Handling::.

子进程的结束状态返回后存于status,底下有几个宏可判别结束情况
WIFEXITED(status)如果子进程正常结束则为非0值。
WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。
WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真
WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。
WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。
WSTOPSIG(status)取得引发子进程暂停的信号代码,



If the exit status value (*note Program Termination::) of the child
process is zero, then the status value reported by `waitpid' or `wait'
is also zero. You can test for other kinds of information encoded in
the returned status value using the following macros. These macros are
defined in the header file `sys/wait.h'.

-- Macro: int WIFEXITED (int STATUS)
     This macro returns a nonzero value if the child process terminated
     normally with `exit' or `_exit'.

-- Macro: int WEXITSTATUS (int STATUS)
     If `WIFEXITED' is true of STATUS, this macro returns the low-order
     8 bits of the exit status value from the child process. *Note
     Exit Status::.

-- Macro: int WIFSIGNALED (int STATUS)
     This macro returns a nonzero value if the child process terminated
     because it received a signal that was not handled. *Note Signal
     Handling::.

子进程的结束状态返回后存于status,底下有几个宏可判别结束情况
WIFEXITED(status)如果子进程正常结束则为非0值。
WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。
WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真
WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。
WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。
WSTOPSIG(status)取得引发子进程暂停的信号代码,

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值