Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using _exit() & exit() in a conventional Linux fork-exec?

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.

It is said that a child process should strictly use _exit() instead of a simple exit() or a normal return from main().

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!

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.

like image 737
ned1986zha Avatar asked Mar 24 '11 17:03

ned1986zha


People also ask

What is the difference between exit () and _exit () functions?

_exit() won't flushes the stdio buffer while exit() flushes the stdio buffer prior to exit. _exit() can not perform clean-up process while exit() can be registered with some function ( i.e on_exit or at_exit) to perform some clean-up process if anything is required before existing the program.

What is difference between exit () atexit ()?

exit() vs _Exit() function in C and C++ So what is the feature of this function? The exit() function performs some cleaning before terminating the program. It clears the connection termination, buffer flushes etc. This _Exit() function does not clean anything.

When can we use _exit?

The _Exit() function in C/C++ gives normal termination of a program without performing any cleanup tasks. For example, it does not execute functions registered with atexit.

What is difference between return and exit in C?

return is a statement that returns the control of the flow of execution to the function which is calling. Exit statement terminates the program at the point it is used.


2 Answers

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 ... } 
like image 57
Fred Foo Avatar answered Oct 11 '22 10:10

Fred Foo


exit() flushes io buffers and does some other things like run functions registered by atexit(). exit() invokes _end( )

_exit() just ends the process without doing that. You call _exit() from the parent process when creating a daemon for example.

Ever notice that main() is a function? Ever wonder what called it in the first place? When a c program runs the shell you are running in provides the executable path to 'exec' system call and the control is passed to kernel which in turn calls the startup function of every executable _start(), calls your main(), when main() returns it then calls _end() Some implementations of C use slightly different names for _end() & _start() ...

exit() and _exit() invoke _end()

Normally - for every main() there should be one & only one exit() call. (or return at the end of main() )

like image 32
Nandan Bharadwaj Avatar answered Oct 11 '22 12:10

Nandan Bharadwaj