73

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.

jschmier
  • 15,458
  • 6
  • 54
  • 72
ned1986zha
  • 775
  • 1
  • 6
  • 5
  • Duplicate of http://stackoverflow.com/questions/2329640/how-to-exit-a-child-process-exit-vs-exit . Also related: http://stackoverflow.com/questions/3657667/exit-functions-in-c – Adam Rosenfield Mar 25 '11 at 05:26

4 Answers4

63

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 ...
}
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • **TONS** of Thanks. I feel much better now; though still twice flushing of a buffer being problematic, has no meaning for me. – ned1986zha Mar 24 '11 at 18:11
  • 5
    @ned1986zha: At the time of the `fork()`, there may be data in the stdio buffers. If both the parent and the child flush those buffers, that data will appear twice in the output. The problem doesn't exist if `exec()` succeeds, because in that case the newly exec'd process starts with fresh stdio buffers. – caf Mar 24 '11 at 22:10
  • @caf: OK, now it makes sense to me. here, conclusion is that buffering is an internal C language mechanism rather than being some OS provided facility. so far, I mistakingly believed that a single open file may have a shared buffer for all the processes! Thank you so much; – ned1986zha Mar 25 '11 at 07:46
  • 3
    @ned1986zha: Yes - the `stdio` buffer is a C library mechanism. There may be OS-provided buffering as well - but that doesn't matter, because the `fork()` won't duplicate that. – caf Mar 25 '11 at 08:30
  • @caf: why no duplicated output when use `_exit` ? Although `_exit` won't flush I/O buffer, but when the child process terminated, kernel should flush any data in the buffer and close the standard I/O streams for the terminated process... – Bin Jul 25 '16 at 15:57
  • 3
    @Bin: As the comments above say, `_exit()` ensures that the C-library-level (userspace stdio) buffers aren't flushed - because those buffers get duplicated by `fork()` (since they're userspace, the kernel isn't aware of them). Any kernel-level buffers would still get flushed, but that's OK, because kernel-level buffers aren't duplicated by `fork()`. – caf Jul 26 '16 at 00:13
  • @caf: Got your point, I just wrote a small code snippet to demonstrate what you said: `printf("abc")` without new line character within (to make sure the characters will be buffered), then following `_exit(0)`, result showed that nothing get flushed out. – Bin Jul 26 '16 at 07:53
  • @caf: There's still one small detail not so clear to me, when we call `_exit()` to terminate the process, let's say there're some buffer exist in `stdout`, it will not be flushed by `_exit()`... after process terminated, will `stdout` be closed ? if yes, won't the i/o stream close operation flush the buffer ? ( as far as I know, `fclose()` will do flushing ), thanks! – Bin Jul 26 '16 at 08:38
  • 2
    @Bin: When the process is terminated by the kernel, the file descriptors get closed but the stdio buffers aren't flushed - because the kernel doesn't know anything about those buffers, they're entirely a creation of the userspace C library. – caf Jul 26 '16 at 13:56
  • IMPORTANT: you MUST NOT use `exit` in signal handler, you can use `abort`, `_exit` or `_Exit`. http://stackoverflow.com/questions/8833394/can-exit-fail-to-terminate-process – yanpas Aug 04 '16 at 12:18
  • `fork()` without `exec*()` are very useful for multithreading, if you want avoid race conditions, at least there are not rare in me programs. – 12431234123412341234123 Aug 10 '17 at 16:26
19

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() )

user2314737
  • 27,088
  • 20
  • 102
  • 114
9

exit() is on the top of _exit(), using conventional C library.

There are the differences:

  1. _exit() won't flushes the stdio buffer while exit() flushes the stdio buffer prior to exit.

  2. _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.

exit(status) simply passes the exit status to _exit(status). It is recommended that whenever to perform fork(), one of them between child and parent, one use _exit() and another use exit().

Sandeep_black
  • 1,352
  • 17
  • 18
0

In the child branch of a fork(), it is normally incorrect to use exit(), because that can lead to stdio buffers being flushed twice, and temporary files being unexpectedly removed.

Excerpted from: http://www.unixguide.net/unix/programming/1.1.3.shtml