在 main 中等待 execvp

发布于 2024-09-16 21:49:31 字数 298 浏览 3 评论 0原文

int main()
{
    ...
    if(!fork())
        {
            execvp(cmdName,cmdParam);
        }
    printf("In main()...");
return(0);
}
  1. 假设我已经正确传递了 cmdName & cmdParam 参数,在恢复 main() 的执行之前,如何等待 execvp 创建的进程完成?
  2. execvp() 是否创建一个新进程 fork() 的子进程?
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心在旅行 2024-09-23 21:49:31
  1. 在父进程中,fork返回子进程的PID,因此您可以将其存储在变量中,然后使用waitpid等待子进程

  2. 并非如此 - 由 fork 创建的新子进程是父进程的副本,然后 execvp 将其进程映像替换为新映像。实际上,您最初拥有父进程的两个“副本”,其中一个随后“成为”新程序。

    事实

  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.

锦上情书 2024-09-23 21:49:31

如前所述,您需要保存 fork 调用的值。你真的应该在叉子上使用比 if 更多的东西。分三种情况:

  1. 0:你是子进程
  2. 0:您是父级并且获得了子级 PID

  3. -1:发生了可怕的事情并且 fork 失败

你真的很想了解情况 3,它会毁了你一整天。 (也是 exec 调用)

int main() {
  int pid = fork();
  if(-1 == pid) {
     fprintf(stderr, "Big problems forking %s\n", strerror(errno);
     exit(-1);//or whatever
  }
  else if (0 == pid) {
    if (-1 == execvp(cmdName,cmdParam)) {
      //like above, get some output about what happened
    }
  }
  //no need to else here, execvp  shouldn't return 
  // if it does you've taken care of it above
  waitpid(pid, NULL, 0);
  printf("Resuming main()...");
}

return(0);
}

As noted you need to save the value of the fork call. You should really use more than an if on the fork. There are three cases:

  1. 0: you're the child process
  2. 0: you're the parent and got a child PID back

  3. -1: something horrible happened and fork failed

You really want to know about case 3, it'll ruin your whole day. (also the exec call)

int main() {
  int pid = fork();
  if(-1 == pid) {
     fprintf(stderr, "Big problems forking %s\n", strerror(errno);
     exit(-1);//or whatever
  }
  else if (0 == pid) {
    if (-1 == execvp(cmdName,cmdParam)) {
      //like above, get some output about what happened
    }
  }
  //no need to else here, execvp  shouldn't return 
  // if it does you've taken care of it above
  waitpid(pid, NULL, 0);
  printf("Resuming main()...");
}

return(0);
}

待天淡蓝洁白时 2024-09-23 21:49:31

对于你的第一个问题:

像这样使用 waitpid(2) :

int pid = fork();
if (!pid)
  {
    execvp(cmdName, cmdParam);
  }
waitpid(pid, NULL, 0);
printf("Resuming main()...\n");

对于第二部分:所有 exec 函数调用都接管进程(它们都不返回)

For your first question:

Use waitpid(2) like this:

int pid = fork();
if (!pid)
  {
    execvp(cmdName, cmdParam);
  }
waitpid(pid, NULL, 0);
printf("Resuming main()...\n");

For the second part: all exec function calls take the process over (none of them return)

你是暖光i 2024-09-23 21:49:31

您需要存储fork()的返回值,该值向每个可执行文件返回不同的值(如果您是子PID,如果您是父PID,则返回0),然后您需要执行<代码>waitpid

You need to store the return value of fork(), which returns a different value to each executable (0 if you are the child PID if you are the parent), and then you need to do a waitpid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文