wait3和wait4函数(转)

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,分别获取当前进程的资源使用情况和所有已终止且被父进程获取其终止状态的所有子进程的资源使用总情况。

posted @ 2017-01-29 03:10  极客先锋  阅读(506)  评论(0编辑  收藏  举报