intmain(int argc,constchar*argv[]){pid_t pid=fork();if(pid>0){printf("this is parent:%d child:%d\n",getpid(),pid);}elseif(pid==0){sleep(2);printf("this is child ppid:%d child: %d\n",getppid(),getpid());}else{perror("fork");return-1;}while(1)sleep(1);return0;}
僵尸进程
#include<stdio.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>#include<unistd.h>intmain(int argc,constchar*argv[]){pid_t pid=fork();if(pid>0){while(1){printf("this is parent:%d child:%d\n",getpid(),pid);sleep(2);}}elseif(pid==0){while(1){printf("this is child ppid:%d child: %d\n",getppid(),getpid());_exit(0);}}else{perror("fork");return-1;}return0;}
this is parent:16159 child:16160
this is child ppid:16159 child:16160
this is parent:16159 child:16160
this is parent:16159 child:16160
this is parent:16159 child:16160
this is parent:16159 child:16160
this is parent:16159 child:1616016159161601615911157 pts/216159 Z+10000:00[a.out]<defunct>