Chinaunix首页 | 论坛 | 博客
  • 博客访问: 353447
  • 博文数量: 158
  • 博客积分: 52
  • 博客等级: 民兵
  • 技术积分: 613
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-27 11:58
文章分类

全部博文(158)

文章存档

2017年(1)

2016年(5)

2015年(19)

2014年(8)

2013年(13)

2012年(80)

2011年(32)

分类:

2011-11-19 22:09:46

原文地址:waitpid 作者:lisongggg

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>

  6. int main(int argc, char* argv[])
  7. {
  8.     pid_t pid1,pid2,wait_pid;
  9.     int status;

  10.     if(0 == (pid1 = fork())) /* 子进程1 */
  11.     {
  12.         printf("child process1 :%d has created. \n", getpid());
  13.         pause();
  14.         _exit(0);
  15.     }
  16.     else if(0 == (pid2 = fork())) /* 子进程2 */
  17.     {
  18.         printf("child process2 :%d has created. \n",getpid());
  19.         pause();
  20.         _exit(0);
  21.     }
  22.     else /* 父进程 */
  23.     {
  24.         while((wait_pid = waitpid(pid1,&status,WUNTRACED|WCONTINUED)) && (-1 != wait_pid))
  25.         {
  26.             if(WIFEXITED(status))
  27.             {
  28.                 printf("child process %d exites, status:%d.\n", wait_pid, WEXITSTATUS(status));
  29.             }
  30.             if(WIFSIGNALED(status))
  31.             {
  32.                 printf("child process %d is killed by signal:%d.\n", wait_pid, WTERMSIG(status));
  33.             }
  34.             if(WIFSTOPPED(status))
  35.             {
  36.                 printf("child process %d is stopped by signal:%d.\n", wait_pid, WSTOPSIG(status));
  37.             }
  38.             if(WIFCONTINUED(status))
  39.             {
  40.                 printf("child process %d is resume running...\n", wait_pid);
  41.             }
  42.         }
  43.     }
  44.     _exit(0);
  45.     return 0;
  46. }

运行结果:
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) |
给主人留下些什么吧!~~