如何退出子进程 - _exit()与出口 [英] how to exit a child process - _exit() vs. exit
问题描述
考虑这个code片断:
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
我该如何退出,如果execvp返回子进程?我将使用的exit()或_exit()?
How shall I exit the child process if execvp returns? Shall I use exit() or _exit()?
推荐答案
您一定要使用 _Exit()
。 退出()
打电话给你添加了的atexit函数()
并删除与 TMPFILE创建的文件()
。由于父进程是真的想要这些东西做,当它存在,你应该叫一个 _Exit()
,它没有做以上。
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()
以大写E. _exit(2)
很可能不是你想要怎样称呼直。 退出(3)
和 _Exit(3)
将调用此为您服务。如果你没有 _Exit(3)
,那么, _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()与出口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!