博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wait3,wait4 用法
阅读量:2183 次
发布时间:2019-05-02

本文共 3741 字,大约阅读时间需要 12 分钟。

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

redis的用法。

[cpp]   
 
  1. int statloc;  
  2.        pid_t pid;  
  3.   
  4.        if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {  
  5.            int exitcode = WEXITSTATUS(statloc);  
  6.            int bysignal = 0;  
  7.   
  8.            if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);  

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

[cpp]   
 
  1. wait3, wait4 - wait for process to change state, BSD style  
  2.   
  3. Synopsis  
  4.   
  5. #include <sys/types.h>  
  6. #include <sys/time.h>  
  7. #include <sys/resource.h>  
  8. #include <sys/wait.h>  
  9.   
  10. pid_t wait3(int *status, int options,  
  11.             struct rusage *rusage);  
  12.   
  13. pid_t wait4(pid_t pid, int *status, int options,  
  14.             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)取得引发子进程暂停的信号代码,

你可能感兴趣的文章
Cisco Packet Tracer教程
查看>>
02. 交换机的基本配置和管理
查看>>
03. 交换机的Telnet远程登陆配置
查看>>
微信小程序-调用-腾讯视频-解决方案
查看>>
phpStudy安装yaf扩展
查看>>
密码 加密 加盐 常用操作记录
查看>>
TP 分页后,调用指定页。
查看>>
Oracle数据库中的(+)连接
查看>>
java-oracle中几十个实用的PL/SQL
查看>>
PLSQL常用方法汇总
查看>>
几个基本的 Sql Plus 命令 和 例子
查看>>
PLSQL单行函数和组函数详解
查看>>
Oracle PL/SQL语言初级教程之异常处理
查看>>
Oracle PL/SQL语言初级教程之游标
查看>>
Oracle PL/SQL语言初级教程之操作和控制语言
查看>>
Oracle PL/SQL语言初级教程之过程和函数
查看>>
Oracle PL/SQL语言初级教程之表和视图
查看>>
Oracle PL/SQL语言初级教程之完整性约束
查看>>
PL/SQL学习笔记
查看>>
如何分析SQL语句
查看>>