当前位置: 技术问答>linux和unix
菜鸟求助, Linux进程的问题
来源: 互联网 发布时间:2016-03-21
本文导语: 看Linux网络编程一书,在父进程终止的情况下,子进程应该交给初始化进程,即子进程不再执行吧? 可是下面程序的执行情况却表明在父进程调用 exit后子进程仍然在执行。请高手指点。 int tst_fork() { int ppid, cpid; ...
看Linux网络编程一书,在父进程终止的情况下,子进程应该交给初始化进程,即子进程不再执行吧? 可是下面程序的执行情况却表明在父进程调用 exit后子进程仍然在执行。请高手指点。
int tst_fork()
{
int ppid, cpid;
int data = 1;
printf("parent process is running!n");
cpid = fork();
if (0 == cpid)
{
ppid = getppid();
data = 2;
sleep(5);
printf("after parent process exit, child process is still running!n");
}
else if (cpid > 0)
{
printf("parent process exit firstn");
exit(1);
}
else
{
printf("fork failedn");
}
return 0;
}
调用上面这个函数的结果:
parent process is running!
parent process exit first
wofei@wofei-laptop:~/project/tstFile$ after parent process exit, child process is still running!
int tst_fork()
{
int ppid, cpid;
int data = 1;
printf("parent process is running!n");
cpid = fork();
if (0 == cpid)
{
ppid = getppid();
data = 2;
sleep(5);
printf("after parent process exit, child process is still running!n");
}
else if (cpid > 0)
{
printf("parent process exit firstn");
exit(1);
}
else
{
printf("fork failedn");
}
return 0;
}
调用上面这个函数的结果:
parent process is running!
parent process exit first
wofei@wofei-laptop:~/project/tstFile$ after parent process exit, child process is still running!
|
父进程先于子进程结束,这个子进程叫孤儿进程,孤儿进程由init进程接管,继续执行