fork一个子进程,在子进程中打印父进程终止前后的PPID:
按照正常的理解,首先会打印父进程的PID,然后会打印init(PID=1)。因为子进程转为孤儿进程被init进程收养。
<pre name="code" class="cpp">#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
pid_t pid;
pid = fork();
if(pid == 0)
{
while(1)
{
printf("I am child exit,my parent %d\n",getppid());
sleep(1);
}
}
else if(pid > 0)
{
printf("I am parent\n");
sleep(1);
}
else
{
perror("fork");
exit(1);
}
return 0;
}
在Red Hat Enterprise Linux Server release 6.4 (Santiago)下运行的结果为:
<img src="