简体   繁体   English

为什么在 execvp 之后我们需要子出口?

[英]Why d we need child exit after execvp?

I have been recently wondering why do we need to use exit after the execution of a child after performing execvp.最近一直在想,为什么执行完execvp后,还需要在child执行完后使用exit。 An in depth explannation is welcome.欢迎深入解释。

You do not need for the child to call exit() after execvp() , but it is often wise for you to ensure that it does so.不需要孩子在execvp() exit() ) ,但确保它这样做通常对您来说是明智的。 So much so as to support giving that to novices as a rule.以至于支持将其作为规则提供给新手。

When a call to execvp() or one of the other exec functions succeeds, it has the effect of replacing the currently-running process image with a new one.当对execvp()或其他 exec 函数之一的调用成功时,它具有将当前运行的进程映像替换为新进程映像的效果。 As a result, these functions do not return on success, and nothing that follows matters in that case.因此,这些函数不会在成功时返回,在这种情况下,后面的任何内容都不重要。 The issue, then, is entirely about what to do in the event that the exec* call fails .那么,问题就完全是关于在exec*调用失败的情况下该怎么做。

If you do not exit the child in the event that an exec fails then it will continue running the code of the parent.如果在 exec 失败的情况下不退出子进程,那么它将继续运行父进程的代码。 That is rarely intended or wanted.这很少是有意或想要的。 It may do work that it was not intended to do, delay the parent (that is often wait() ing for it) by not exiting promptly, and eventually mislead the parent with an exit status that is not reflective of what actually happened.它可能会做它不打算做的工作,通过不及时退出来延迟父级(通常是wait() ing),并最终以不反映实际发生情况的退出状态误导父级。

All of that, and perhaps more, is addressed by ensuring that the control flow following an exec* call pretty quickly reaches program termination with a failure status (typically via _Exit() or _exit() ; less typically via exit() or abort() ).所有这些,也许还有更多,都是通过确保exec*调用之后的控制流很快到达程序终止并出现故障状态来解决的(通常通过_Exit()_exit() ;不太常见的是通过exit()abort() )。 Depending on the circumstances, it may or may not be appropriate to emit a diagnostic message before exiting, or to perform some other kind of cleanup.根据具体情况,在退出之前发出诊断消息或执行某种其他类型的清理可能合适也可能不合适。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM