当前位置: 技术问答>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进程接管,继续执行
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。