- #include <stdio.h>
-
#include <unistd.h>
-
#include <sys/types.h>
-
#include <sys/wait.h>
-
#include <stdlib.h>
-
-
int main(int argc, char* argv[])
-
{
-
pid_t pid1,pid2,wait_pid;
-
int status;
-
-
if(0 == (pid1 = fork())) /* 子进程1 */
-
{
-
printf("child process1 :%d has created. \n", getpid());
-
pause();
-
_exit(0);
-
}
-
else if(0 == (pid2 = fork())) /* 子进程2 */
-
{
-
printf("child process2 :%d has created. \n",getpid());
-
pause();
-
_exit(0);
-
}
-
else /* 父进程 */
-
{
-
while((wait_pid = waitpid(pid1,&status,WUNTRACED|WCONTINUED)) && (-1 != wait_pid))
-
{
-
if(WIFEXITED(status))
-
{
-
printf("child process %d exites, status:%d.\n", wait_pid, WEXITSTATUS(status));
-
}
-
if(WIFSIGNALED(status))
-
{
-
printf("child process %d is killed by signal:%d.\n", wait_pid, WTERMSIG(status));
-
}
-
if(WIFSTOPPED(status))
-
{
-
printf("child process %d is stopped by signal:%d.\n", wait_pid, WSTOPSIG(status));
-
}
-
if(WIFCONTINUED(status))
-
{
-
printf("child process %d is resume running...\n", wait_pid);
-
}
-
}
-
}
-
_exit(0);
-
return 0;
-
}
运行结果:
lisong@lisong:~/code/experiment/proc/wait_pid$ ./waitpid &
[1] 2996
child process1 :2997 has created.
child process2 :2998 has created.
lisong@lisong:~/code/experiment/proc/wait_pid$ ps
PID TTY TIME CMD
2181 pts/1 00:00:01 bash
2996 pts/1 00:00:00 waitpid
2997 pts/1 00:00:00 waitpid
2998 pts/1 00:00:00 waitpid
3003 pts/1 00:00:00 ps
lisong@lisong:~/code/experiment/proc/wait_pid$ kill -19 2997
child process 2997 is stopped by signal:19.
lisong@lisong:~/code/experiment/proc/wait_pid$ kill -19 2998
lisong@lisong:~/code/experiment/proc/wait_pid$ ps
PID TTY TIME CMD
2181 pts/1 00:00:01 bash
2996 pts/1 00:00:00 waitpid
2997 pts/1 00:00:00 waitpid
2998 pts/1 00:00:00 waitpid
3014 pts/1 00:00:00 ps
lisong@lisong:~/code/experiment/proc/wait_pid$ kill -18 2998
lisong@lisong:~/code/experiment/proc/wait_pid$ ps
PID TTY TIME CMD
2181 pts/1 00:00:01 bash
2996 pts/1 00:00:00 waitpid
2997 pts/1 00:00:00 waitpid
2998 pts/1 00:00:00 waitpid
3015 pts/1 00:00:00 ps
lisong@lisong:~/code/experiment/proc/wait_pid$ kill -18 2997
child process 2997 is resume running...
lisong@lisong:~/code/experiment/proc/wait_pid$ ps
PID TTY TIME CMD
2181 pts/1 00:00:01 bash
2996 pts/1 00:00:00 waitpid
2997 pts/1 00:00:00 waitpid
2998 pts/1 00:00:00 waitpid
3020 pts/1 00:00:00 ps
lisong@lisong:~/code/experiment/proc/wait_pid$
lisong@lisong:~/code/experiment/proc/wait_pid$
lisong@lisong:~/code/experiment/proc/wait_pid$ kill -9 2998
lisong@lisong:~/code/experiment/proc/wait_pid$ ps
PID TTY TIME CMD
2181 pts/1 00:00:01 bash
2996 pts/1 00:00:00 waitpid
2997 pts/1 00:00:00 waitpid
2998 pts/1 00:00:00 waitpid
3022 pts/1 00:00:00 ps
lisong@lisong:~/code/experiment/proc/wait_pid$ kill -9 2997
child process 2997 is killed by signal:9.
lisong@lisong:~/code/experiment/proc/wait_pid$ ps
PID TTY TIME CMD
2181 pts/1 00:00:01 bash
3027 pts/1 00:00:00 ps
[1]+ 完成 ./waitpid
分析:
可以看到,对子进程2(pid:2998) 发送信号,并没有得到父进程处理。杀死子进程2之后,由于父进程不处理,子进程2变成了僵尸进程(defunct)。当父进程结束之后,子进程2被init进程消灭。
阅读(373) | 评论(0) | 转发(0) |