控制权在“execvp()”之后返回吗?

发布于 2024-09-01 17:32:46 字数 380 浏览 1 评论 0原文


if(pid == 0)
{
      execvp(cmd, args);
      // printf("hello"); // apparently, putting this or not does not work.
      _exit(-1);
}
else
{
      // parent process work
}

“execvp()”用要执行的程序替换当前程序(当然在相同的进程上下文中)。因此,在 execvp() 之后放置任何 printf() 调用将不起作用。文档是这么说的,我也验证过。

但是,为什么需要 _exit() 呢?控制是否会返回到 execvp() 之后的语句?

我将不胜感激任何指点。

谢谢


if(pid == 0)
{
      execvp(cmd, args);
      // printf("hello"); // apparently, putting this or not does not work.
      _exit(-1);
}
else
{
      // parent process work
}

"execvp()" replaces the current program with the to-be-execed program (of course in the same process context). So, putting, say, any printf() calls after execvp() won't work. That is what the docs say, and I have verified it as well.

But then, why is _exit() needed..? Does it so happen that the control DOES return to statements post execvp() ?

I will be grateful for any pointers.

Thanks

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

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

发布评论

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

评论(4

独自唱情﹋歌 2024-09-08 17:32:46

如果失败,该函数将返回。

如果 exec 函数之一返回到调用进程映像,则发生错误;返回值为-1,并设置errno来指示错误。

即使 exec 失败,_exit() 也允许正确终止进程并返回退出代码。

The function will return if it has failed.

If one of the exec functions returns to the calling process image, an error has occurred; the return value shall be -1, and errno shall be set to indicate the error.

The _exit() allows terminating the process properly and return an exit code, even if exec fails.

小…红帽 2024-09-08 17:32:46

execve() 系统调用可能会失败。这样做的典型原因是文件不存在或不可执行。 execvp() 包裹 execve() 以添加路径搜索和默认环境处理(几乎总是您想要的!),因此它添加了另外一些故障模式,特别是尝试使用不在用户路径上的简单名称运行某些内容。无论如何,失败就是失败,当它发生时,你无能为力,除了报告它出了问题并让(现在无用的)子进程摆脱闪避。 (最简单的错误报告方法是打印错误消息,也许使用 perror(),但还有其他方法。)

为什么需要 _exit() 而不是更正常的 exit() 是因为您想退出子进程,但您不想运行任何与父进程关联的已注册清理代码。好吧,其中很多可能是无害的,但是像向套接字写入告别消息之类的事情会很糟糕,而且使用 atexit() 注册的内容通常并不明显。让父进程关心它的资源;孩子除了它的堆栈框架之外基本上不拥有任何东西!

The execve() syscall can fail. The classic reason for doing this would be if the file isn't there or isn't executable. execvp() wraps around execve() to add path searching and default environment handling (virtually always what you want!) and so it adds in another few failure modes, notably trying to run something with a simple name that's not on the user's path. In any case, failure is failure and there's not a lot you can do when it happens except report that it has gone wrong and Get the (now useless) child process Out Of Dodge. (The simplest error reporting method is to print an error message, perhaps with perror(), but there are others.)

The reason why you need _exit() as opposed to the more normal exit() is because you want to quit the child process but you do not want to run any registered cleanup code associated with the parent process. OK, a lot of it might be harmless, but doing things like writing goodbye messages to a socket or something would be bad, and it's often not at all obvious what has been registered with atexit(). Let the parent process worry about its resources; the child basically owns nothing other than its stack frame!

看海 2024-09-08 17:32:46

如果execvp失败,_exit将被调用。

execvp 的手册页显示:

返回值
如果任何 exec() 函数返回,就会发生错误。返回值为-1,并且将设置全局变量errno来指示错误。

If execvp fails, _exit will be called.

execvp's man page says:

Return Value
If any of the exec() functions returns, an error will have occurred. The return value is -1, and the global variable errno will be set to indicate the error.

暗喜 2024-09-08 17:32:46

需要注意的一件事是,您通常不希望对进程的退出状态进行签名(如果可移植性很重要)。虽然 exec() 可以在失败时自由返回 -1,但它会返回该值,以便您可以在子代码中处理该失败。

子进程的实际 _exit() 状态应为 0 - 255,具体取决于引发的 errno 类型。

One thing to note, you generally don't want a process' exit status to be signed (if portability matters). While exec() is free to return -1 on failure, its returning that so you can handle that failure within the child code.

The actual _exit() status of the child should be 0 - 255, depending on what errno was raised.

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