wait4

wait4会挂起当前进程,等待指定的子进程状态改变,并且会返回关于子进程使用资源的信息。所谓的状态的改变包括终止,挂起所有的进程状态的改变。另外,在linux中进程终止后不会释放其资源,它会进入一种僵死的状态,内核会为僵死态的进程保留最少的信息(进程标识,终止状态和资源使用信息).而wait4就可以释放与子进程关联的资源。如果父进程不执行wait,而父进程又死亡后,僵死态的子进程会被指定成系统初始化进程init的子进程.下面是wait4使用方法:

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);
pid是要关注的子进程的pid(进程标识号)
status子进程的返回状态。
options进程等待选项
rusage死亡进程资源使用记录


pid这个很好理解,进程标识号,也是你ps -le显示出来的pid。如果指定了pid则会在指定pid发生变化后唤醒父进程,此外还有一下4种情况:
< -1   which means to wait for any child process whose process group ID is equal to the absolute value of pid.
-1     which means to wait for any child process; this is equivalent to calling wait3.
0      which means to wait for any child process whose process group ID is equal to that of the calling process.
> 0    which means to wait for the child whose process ID is equal to the value of pid.


status子进程的返回状态,这是个用于描述进程返回状态的整数,有各种宏操作可以查看,如下:
WIFEXITED(status)
is non-zero if the child exited normally.


WEXITSTATUS(status)
evaluates to the least significant eight bits of the return code of the child which terminated, which may have been set as the argument to a call
to exit() or as the argument for a return statement in the main program. This macro can only be evaluated if WIFEXITED returned non-zero.


WIFSIGNALED(status)
returns true if the child process exited because of a signal which was not caught.


WTERMSIG(status)
returns the number of the signal that caused the child process to terminate. This macro can only be evaluated if WIFSIGNALED returned non-zero.


WIFSTOPPED(status)
returns true if the child process which caused the return is currently stopped; this is only possible if the call was done using WUNTRACED.


WSTOPSIG(status)
returns the number of the signal which caused the child to stop. This macro can only be evaluated if WIFSTOPPED returned non-zero.


options进程等待选项,当为WNOHANG表示立即返回,而当WUNTRACED表示等子进程状态发生变化后才返回。


最后rusage记录死亡进程资源使用信息,这个数据结构定义如下:
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long   ru_maxrss;        /* maximum resident set size */
long   ru_ixrss;         /* integral shared memory size */
long   ru_idrss;         /* integral unshared data size */
long   ru_isrss;         /* integral unshared stack size */
long   ru_minflt;        /* page reclaims */
long   ru_majflt;        /* page faults */
long   ru_nswap;         /* swaps */
long   ru_inblock;       /* block input operations */
long   ru_oublock;       /* block output operations */
long   ru_msgsnd;        /* messages sent */
long   ru_msgrcv;        /* messages received */
long   ru_nsignals;      /* signals received */
long   ru_nvcsw;         /* voluntary context switches */
long   ru_nivcsw;        /* involuntary context switches */
};


使用样例
[gavin@gavin]# cat print.c
#include "stdio.h"


int main() {
int i;
for (i = 0; i < 50; i++)
printf("Hello, This is print!!!\n");
exit(0);
}
[gavin@gavin]# gcc print.c -o print
[gavin@gavin]# cat test.c
#include "stdio.h" 
#include <sys/types.h> 
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>


int main() {
int child;
int * status;
struct rusage * rus;
char * argu[] = {"/program/wait/print", NULL};
if(!(child = vfork())) {
execve("/program/wait/print", argu, NULL);
} else {
wait4(child,status,WUNTRACED,rus);
printf("The son's pid:%d, status:%d, The memory is:%d\n", child, WIFEXITED(status),rus->ru_nswap);
}
exit(0);
}

posted @ 2014-03-24 18:57  剑不飞  阅读(1051)  评论(0编辑  收藏  举报