有关孤儿进程的讲解:
孤儿进程讲解博客传送门
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <unistd.h>
5
6 int main(){
7
8 pid_t pid = fork();
9 if(pid < 0){
10 perror("fork error:");
11 exit(1);
12 }
13
14 if(pid == 0){
15 printf("I am a chile process!!! child pid[%d]-----father pid[%d]\n", getpid(), getppid());
16 printf("I will sleep 5s, when I wake up, my father have already exited\n");
17 sleep(5);
18 printf("child pid:[%d]--------father pid[%d]\n", getpid(), getppid());
19 printf("child process exit\n");
20 }
21 else{
22 printf("I am father process!!!pid:[%d]\n", getpid());
23 sleep(1); //保证子进程打印进程id
24 printf("father process exit\n");
25 }
26 return 0;
27 }
打印效果: