在主等待execvp [英] Waiting for execvp in main

查看:238
本文介绍了在主等待execvp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  INT的main()
{
    ...
    如果(!叉())
        {
            execvp(cmdName,cmdParam);
        }
    的printf(在main()......);
返回(0);
}


  1. 假设我已经正确地通过了cmdName&放大器; cmdParam争论,我怎么等待由execvp创建的过程完成,恢复的main()?
  2. 执行前
  3. 请问execvp()创建一个过程,是新fork()的编辑过程中的孩子?


解决方案

  1. 在父进程,返回子进程的PID,这样你就可以存储在一个变量,然后使用 waitpid函数来等待子进程终止。


  2. 不是真的 - 通过创建新的子进程是父的副本,而 execvp 然后用一个新形象替换其处理图像。有效地最初有父母的两个拷贝,然后一变成了新的方案。


int main()
{
    ...
    if(!fork())
        {
            execvp(cmdName,cmdParam);
        }
    printf("In main()...");
return(0);
}

  1. Assuming I have correctly passed the cmdName & cmdParam arguments, how do I wait for the process created by execvp to finish, before resuming the execution of main()?
  2. Does the execvp() create a process which is a child of the newly fork()ed process?

解决方案

  1. In the parent process, fork returns the PID of the child process, so you can store that in a variable, and then use waitpid to wait for the child process to terminate.

  2. Not really - the new child process created by fork is a duplicate of the parent, and execvp then replaces its process image with a new image. Effectively you initially have two 'copies' of the parent, one of which then 'becomes' the new program.

这篇关于在主等待execvp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆