我们都知道在Linux上孤儿进程和僵尸进程都会被进程号为1的init进程收养,收尸,但这在使用Systemd来管理系统的发行版上比如Ubuntu上就不是那么靠谱了
首先我们写一个简单的孤儿进程的例子
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int
main(void)
{
switch(fork()){
case 0:
printf("Child process\n");
printf("child process: %d\n", getpid());
printf("father pid:%d\n", getppid());
sleep(5);
printf("\nnow pid: %d\t ppid:%d \n",getpid(),getppid());
break;
case -1:
printf("Fork failed");
exit(-1);
default:
printf("Father Process\n");
sleep(1);
printf("Father exit\n");
break;
}
return 0;
}
首先我们在图像界面上伪终端中运行