简体   繁体   English

当父进程调用exec命令时,子进程会发生什么情况

[英]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. 这种方法的问题在于,新替换的父进程通常不会意识到它先前已经生成了一个子进程,因此不会在其上调用waitwaitpid This will cause the child process to become a zombie when it exits. 这将导致子进程退出时变成僵尸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用exec后,子进程会怎样? - What happens to the child process after calling exec? 在fork调用子进程和父进程之后,值的区别是什么? - What will be the difference in values after fork calls for child and parent process? 在子进程中调用dup2和execvp之后,父进程中的文件描述符表会如何处理? - What happens to the file descriptor table in parent process after calling dup2 and execvp in the child process? 在fork()进程中没有针对父进程的操作时会发生什么? - What happens when there are no actions for parent process during fork() processes? 当父调用exit(0)时子进程卡在fork()中 - child process stuck in fork() when parent calls exit(0) 当你派生一个父进程时,活动子进程会发生什么,所有活动进程都会创建另一个进程吗? 叉子(); - What happens to active child processes when you fork a parent, do all active processes create another process ? Fork(); 僵尸进程的父进程终止后会发生什么? - What happens after the parent of zombie process terminates? 如果 execvp() 成功,子进程会发生什么? - what happens to child process if it execvp() is successful? 当父母发生某些事情时,父母程序如何告诉孩子去做某事? - How can parent process tell child to do something when something happens to parent? 子进程/父进程 - Child Process / Parent Process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM