孤儿进程:
孤儿进程是指父进程提前退出而使得子进程被init收养的进程
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc,char* argv[]) { pid_t pid; if((pid=fork())==-1) perror("fork"); else if(pid==0) { printf("pid=%d,ppid=%d\n",getpid(),getppid()); sleep(2); printf("pid=%d,ppid=%d\n",getpid(),getppid()); } else exit(0); }
[root@sun task]# ./orphan
pid=3666,ppid=3665
pid=3666,ppid=1
僵死进程:
进程已经退出,但是其父进程还没有回收他的内核资源的进程
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 5 int main() 6 { 7 pid_t pid; 8 if((pid=fork())==-1) 9 perror("fork"); 10 else if(pid==0) 11 { 12 printf("child_pid pid=%d,ppid=%d\n",getpid(),getppid()); 13 exit(0); 14 } 15 sleep(3); 16 system("ps"); 17 exit(0); 18 }
[root@sun task]# ./dead
child_pid pid=3780,ppid=3779
PID TTY TIME CMD
2724 pts/0 00:00:00 su
2732 pts/0 00:00:00 bash
3779 pts/0 00:00:00 dead
3780 pts/0 00:00:00 dead <defunct>
3781 pts/0 00:00:00 ps