[英]What happens to the child process, when the parent process calls an exec command
Suppose we have a parent process and if it calls an exec function, after calling a fork to create child process. 假设我们有一个父进程,并且在调用fork创建子进程之后,如果它调用了exec函数。
Now what happens to the child process: will it act like the original parent process, such that the user won't spot the difference that the parent process got replaced with some other binary ? 现在,子进程会发生什么:它会像原始父进程一样工作,以使用户不会发现父进程被其他二进制替换的区别吗?
I think this question differs from the following question what happens to child process? 我认为这个问题与以下问题不同,子进程会发生什么? .
。
if ( (pid == fork ()) != 0 )
{
if (strcmp(cmd,"mypwd")==0)
{
execlp (“mypwd”,0);
}
...
...
else if (strcmp(cmd,"myexit")==0)
exit(1);
}
The pid of the parent process will remain the same after the exec
, so the process hierarchy won't be affected. exec
之后,父进程的pid将保持不变,因此进程层次结构不会受到影响。
The problem with this approach is that the newly replaced parent process generally won't be aware that it has previously spawned a child and will not call wait
or waitpid
on it. 这种方法的问题在于,新替换的父进程通常不会意识到它先前已经生成了一个子进程,因此不会在其上调用
wait
或waitpid
。 This will cause the child process to become a zombie when it exits. 这将导致子进程退出时变成僵尸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.