问题描述
我一直在试图找出叉EXEC机制是如何内部的Linux使用。一切都会在按计划直到某些网页开始迷惑我。
I've been trying to figure out how the fork-exec mechanism is used inside Linux. Everything was going on according to the plan until some web pages started to confuse me.
有人说,一个子进程应严格使用 _exit()
,而不是一个简单的退出()
或从正常返回主()
。
It is said that a child process should strictly use _exit()
instead of a simple exit()
or a normal return from main()
.
据我所知,Linux的shell叉高管的外部命令每一个;假设我上面说的是真的,得出的结论是,没有这些外部命令,也没有任何其他的执行在Linux shell内部发生的可以做正常的回报!
As I know, Linux shell fork-execs every one of the external commands; assuming what I said above is true, the conclusion is that none of these external commands nor any other execution happening inside the Linux shell can do normal return!
&百科放大器;其他一些网页声称我们必须使用 _exit()
只是prevent一个子进程引起父母的临时文件删除,而标准输入输出缓冲区的一个可能的二次冲洗可能发生。虽然我理解前者,我没有线索缓冲区的二次冲洗怎么可能是有害的Linux系统。
Wikipedia & some other webpages claim we've got to use _exit()
just to prevent a child process causing deletion of parent's temporary files while a probable double flushing of stdio buffers may happen. though I understand the former, I have no clues how a double flushing of buffers could be harmful to a Linux system.
我已经花了我整整一天在这...
感谢您的任何澄清。
I've spent my whole day on this...Thanks for any clarification.
推荐答案
您应该使用(或它的同义词 _Exit
)中止子程序时 EXEC
失败,因为在这种情况下,子进程可以通过调用其 atexit对
处理程序,调用其信号处理程序的父进程的外部数据(文件)干扰,和/或刷新缓冲区。
You should use _exit
(or its synonym _Exit
) to abort the child program when the exec
fails, because in this situation, the child process may interfere with the parent process' external data (files) by calling its atexit
handlers, calling its signal handlers, and/or flushing buffers.
出于同样的原因,你也应该使用 _exit
在任何子进程,这并不做一个 EXEC
,但这些都是罕见的。
For the same reason, you should also use _exit
in any child process that does not do an exec
, but those are rare.
在其他情况下,只需要使用退出
。正如你部分指出的自己,的在Unix / Linux操作系统(除一人外,的init
)每个的过程是另一个进程的孩子,所以使用 _exit
在每一个孩子的过程就意味着退出
是初始化无用之外>
In all other cases, just use exit
. As you partially noted yourself, every process in Unix/Linux (except one, init
) is the child of another process, so using _exit
in every child process would mean that exit
is useless outside of init
.
switch (fork()) {
case 0:
// we're the child
execlp("some", "program", NULL);
_exit(1); // <-- HERE
case -1:
// error, no fork done ...
default:
// we're the parent ...
}
这篇关于是什么使用_exit()及之间的差出口()在传统的Linux叉EXEC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!