- 一例孤儿进程代码的演示
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<sys/types.h> #include<fcntl.h> int main() { pid_t pid; pid =fork(); if(pid==0) { while(1) { printf("i'm child=%d, parent=%d\n",getpid(),getppid()); sleep(1); } } else if(pid==-1) { perror("process failed!"); exit(1); } else { printf("i'm the master process %d\n",getpid()); sleep(10); printf("master process %d died\n",getpid()); } return 0; }
输出结果:
由此可见,进程13345的父进程结束后,变为孤儿进程,其父进程转为1365