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.
For the same reason, you should also use _exit
in any child process that does not do an exec
, but those are rare.
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 ...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…