首页 > 代码库 > init进程接管孤儿进程的验证

init进程接管孤儿进程的验证

 
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include<stdlib.h>#include<signal.h>int main(int argc , char **argv){
int id;
 id=fork(); if(id<0)  {  printf("fork error\n");  } else if(id==0)  {   printf("I‘m in child process%d parent process%d\n",getpid(),getppid());   sleep(120);   exit(0);   } else if(id>0)   {   printf("I‘m in parent process%d\n",getpid());   }     return 0;}[root@linux Desktop]# gcc 僵尸进程.c[root@linux Desktop]# ./a.outIm in parent process4308[root@linux Desktop]# Im in child process4309 parent process 1[root@linux Desktop]# 从上面的输出结果可以看出孩子进程4309的父进程是1号进程也就是init进程,这是因为子进程的父进程死亡后子进程被init进程接管!所以说输出他的父进程号为1;[root@linux Desktop]# ps --ppid 1  PID TTY          TIME CMD  494 ?        00:00:00 udevd 1474 ?        00:00:00 vmware-vmblock- 1494 ?        00:00:20 vmtoolsd 1640 ?        00:00:00 portreserve 1647 ?        00:00:00 rsyslogd 1700 ?        00:00:02 dbus-daemon 1711 ?        00:00:00 NetworkManager 1717 ?        00:00:00 modem-manager 1723 ?        00:00:00 avahi-daemon 1733 ?        00:00:00 wpa_supplicant 1737 ?        00:00:00 cupsd 1762 ?        00:00:00 acpid 1771 ?        00:00:00 hald 1840 ?        00:00:00 sshd 1878 ?        00:00:00 tpvmlp 1951 ?        00:00:00 mysqld_safe 2113 ?        00:00:00 abrtd 2121 ?        00:00:00 abrt-dump-oops 2129 ?        00:00:01 crond 2140 ?        00:00:00 atd 2155 ?        00:00:00 rhsmcertd 2171 ?        00:00:00 gdm-binary 2178 tty2     00:00:00 mingetty 2180 tty3     00:00:00 mingetty 2182 tty4     00:00:00 mingetty 2190 tty5     00:00:00 mingetty 2192 tty6     00:00:00 mingetty 2229 ?        00:00:00 console-kit-dae 2299 ?        00:00:00 dbus-launch 2315 ?        00:00:00 devkit-power-da 2323 ?        00:00:00 auditd 2383 ?        00:00:00 polkitd 2389 ?        00:00:00 rtkit-daemon 2414 ?        00:00:00 gnome-keyring-d 2432 ?        00:00:00 dbus-launch 2433 ?        00:00:00 dbus-daemon 2447 ?        00:00:01 gconfd-2 2452 ?        00:00:20 gnome-settings- 2457 ?        00:00:00 seahorse-daemon 2462 ?        00:00:00 gvfsd 2468 ?        00:00:00 gvfs-fuse-daemo 2487 ?        00:00:00 bonobo-activati 2495 ?        00:00:13 wnck-applet 2496 ?        00:00:00 trashapplet 2499 ?        00:00:39 vmtoolsd 2503 ?        00:00:00 gvfs-gdu-volume 2510 ?        00:00:00 udisks-daemon 2516 ?        00:00:03 pulseaudio 2541 ?        00:00:00 gvfs-gphoto2-vo 2543 ?        00:00:01 gvfs-afc-volume 2555 ?        00:00:00 clock-applet 2556 ?        00:00:00 gdm-user-switch 2557 ?        00:00:00 gnote 2558 ?        00:00:00 notification-ar 2563 ?        00:00:00 restorecond 2566 ?        00:00:00 gvfsd-trash 2573 ?        00:00:12 gnome-screensav 2665 ?        00:00:00 gvfsd-burn 2677 ?        00:00:00 gvfsd-metadata 2706 ?        00:00:08 notification-da 2725 ?        00:00:44 gnome-terminal 2783 ?        00:00:44 gedit 4309 pts/0    00:00:00 a.out   注意下面这句没有输出[root@linux Desktop]# ps --ppid 1  PID TTY          TIME CMD  494 ?        00:00:00 udevd 1474 ?        00:00:00 vmware-vmblock- 1494 ?        00:00:20 vmtoolsd 1640 ?        00:00:00 portreserve 1647 ?        00:00:00 rsyslogd 1700 ?        00:00:02 dbus-daemon 1711 ?        00:00:00 NetworkManager 1717 ?        00:00:00 modem-manager 1723 ?        00:00:00 avahi-daemon 1733 ?        00:00:00 wpa_supplicant 1737 ?        00:00:00 cupsd 1762 ?        00:00:00 acpid 1771 ?        00:00:00 hald 1840 ?        00:00:00 sshd 1878 ?        00:00:00 tpvmlp 1951 ?        00:00:00 mysqld_safe 2113 ?        00:00:00 abrtd 2121 ?        00:00:00 abrt-dump-oops 2129 ?        00:00:01 crond 2140 ?        00:00:00 atd 2155 ?        00:00:00 rhsmcertd 2171 ?        00:00:00 gdm-binary 2178 tty2     00:00:00 mingetty 2180 tty3     00:00:00 mingetty 2182 tty4     00:00:00 mingetty 2190 tty5     00:00:00 mingetty 2192 tty6     00:00:00 mingetty 2229 ?        00:00:00 console-kit-dae 2299 ?        00:00:00 dbus-launch 2315 ?        00:00:00 devkit-power-da 2323 ?        00:00:00 auditd 2383 ?        00:00:00 polkitd 2389 ?        00:00:00 rtkit-daemon 2414 ?        00:00:00 gnome-keyring-d 2432 ?        00:00:00 dbus-launch 2433 ?        00:00:00 dbus-daemon 2447 ?        00:00:01 gconfd-2 2452 ?        00:00:20 gnome-settings- 2457 ?        00:00:00 seahorse-daemon 2462 ?        00:00:00 gvfsd 2468 ?        00:00:00 gvfs-fuse-daemo 2487 ?        00:00:00 bonobo-activati 2495 ?        00:00:13 wnck-applet 2496 ?        00:00:00 trashapplet 2499 ?        00:00:39 vmtoolsd 2503 ?        00:00:00 gvfs-gdu-volume 2510 ?        00:00:00 udisks-daemon 2516 ?        00:00:03 pulseaudio 2541 ?        00:00:00 gvfs-gphoto2-vo 2543 ?        00:00:01 gvfs-afc-volume 2555 ?        00:00:00 clock-applet 2556 ?        00:00:00 gdm-user-switch 2557 ?        00:00:00 gnote 2558 ?        00:00:00 notification-ar 2563 ?        00:00:00 restorecond 2566 ?        00:00:00 gvfsd-trash 2573 ?        00:00:12 gnome-screensav 2665 ?        00:00:00 gvfsd-burn 2677 ?        00:00:00 gvfsd-metadata 2706 ?        00:00:08 notification-da 2725 ?        00:00:44 gnome-terminal 2783 ?        00:00:44 gedit[root@linux Desktop]# 在我测试这两幅图的时候,第一幅图输出后,要等大概两分多钟,也就是大概子进程执行完后,init进程调用wait会清理掉刚才接收的子进程(4309 )。子进程4309 

//如果将signal(SIGCHLD, SIG_IGN);这行代码加上放在main函数的第一行,应该说会出现一种情况当父进程退出时,父进程的父进程不会理会他,而直接让init进程接管他,但是用ps --ppid 1输出时,没有发现父进程。!只有子进程号!
 

 

init进程接管孤儿进程的验证