C语言 如何退出子进程 - _exit() 与退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2329640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:36:05  来源:igfitidea点击:

how to exit a child process - _exit() vs. exit

cprocessfork

提问by helpermethod

Consider this code snippet:

考虑这个代码片段:

pid_t cpid = fork();

if (cpid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
}

if (cpid == 0) { // in child
    execvp(argv[1], argv + 1);
    perror("execvp");
    _exit(EXIT_FAILURE);
}

// in parent

How shall I exit the child process if execvp returns? Shall I use exit() or _exit()?

如果 execvp 返回,我该如何退出子进程?我应该使用 exit() 还是 _exit()?

回答by Variable Length Coder

You should definitely use _Exit(). exit()calls the functions you added with atexit()and deletes files created with tmpfile(). Since the parent process is really the one that wants these things done when it exists, you should call _Exit(), which does none of these.

你绝对应该使用_Exit(). exit()调用您添加的函数atexit()并删除使用tmpfile(). 由于父进程确实希望在它存在时完成这些事情,因此您应该调用_Exit(),它不执行任何这些操作。

Notice _Exit()with a capital E. _exit(2)is probably not what you want to call directly. exit(3)and _Exit(3)will call this for you. If you don't have _Exit(3), then yes, _exit()is what you wanted.

注意_Exit()大写 E. _exit(2)可能不是您想要直接调用的。 exit(3)并将_Exit(3)为您调用此方法。如果你没有_Exit(3),那么是的,_exit()就是你想要的。

回答by Joshua

The child of fork() should always call _exit().

fork() 的子级应该总是调用 _exit()。

Calling exit() instead is a good way to cause pending stdio buffers to be flushed twice.

调用 exit() 是导致挂起的 stdio 缓冲区被刷新两次的好方法。

回答by Nicolas Guillaume

execvp will exit the child if successfull so you don't have to exit.

如果成功,execvp 将退出子进程,因此您不必退出。

On execve failure I simply use exit(EXIT_FAILURE);in the child.

在执行失败时,我只是exit(EXIT_FAILURE);在孩子中使用。

Edit : i found that after some research : http://www.unixguide.net/unix/programming/1.1.3.shtml

编辑:我发现经过一些研究:http: //www.unixguide.net/unix/programming/1.1.3.shtml

So it's looks like it's better to use _exit()in a fork child specially when you are in C++ :p Thanks for your question i learned something :D

所以_exit()当你在 C++ 中时,看起来最好在 fork child 中使用:p 谢谢你的问题,我学到了一些东西:D

回答by jabbie

It depends on the behavior you want: man -s 3 exitand man _exitfor more details on your system. In general I believe the _exit doesn't run functions that are registered with atexit() whereas exit does (these functions better not call exit - otherwise you get recursion).

这取决于您想要的行为:man -s 3 exit以及man _exit有关您系统的更多详细信息。一般来说,我相信 _exit 不会运行使用 atexit() 注册的函数,而 exit 会运行(这些函数最好不要调用 exit - 否则会递归)。

In general I would prefer exit over _exit except for in functions registered with atexit, in those I would call _exit, if needed.

一般来说,我更喜欢退出而不是 _exit,除了在 atexit 注册的函数中,如果需要,我会调用 _exit。

回答by ImanKh

exit()is ANSI-C function and therefore, it is operating system independent. It closes all ANSI-C standard functions. _exit()is called by exit()to close operating system-dependent functionalities, because exit()has no idea about them.(exitis operating system independent)

exit()是 ANSI-C 函数,因此它与操作系统无关。它关闭了所有 ANSI-C 标准功能。 _exit()被调用exit()以关闭依赖于操作系统的功能,因为exit()不知道它们。(exit与操作系统无关)