tags:

views:

180

answers:

4

What is the difference between exit(), _exit() and _Exit() in C?

How do I decide which to use?

On bash,

man 2 exit

gave me the page _EXIT(2), whereas

man 3 exit

gave the page EXIT(3).

+6  A: 

exit() terminating after cleanup.

_exit() terminating immediately after call.

If you have some stack corrupted while exit() function was called program may close with Segmentation Fault, if you are use _exit(), program exit in quick mode.

From http://msdn.microsoft.com/en-us/library/6wdz5232.aspx you have

exit() - Performs complete C library termination procedures, terminates the process, and exits with the supplied status code.

_exit() - Performs quick C library termination procedures, terminates the process, and exits with the supplied status code.

_cexit() - Performs complete C library termination procedures and returns to the caller, but does not terminate the process.

_c_exit() - Performs quick C library termination procedures and returns to the caller, but does not terminate the process.

Svisstack
OK, but what's the difference between complete and quick termination⸮
adf88
@Svisstack, please elaborate what should be used when.
Kedar Soparkar
Call `exit()`. The others are implementation details, and are generally not as useful outside of certain rare conditions when building frameworks.
RBerteig
+1  A: 

From man:

exit:
All functions registered with atexit(3) and on_exit(3) are called, in the reverse order of their registration ... All open stdio(3) streams are flushed and closed. Files created by tmpfile(3) are removed.

_exit:
The function _exit() is like exit(3), but does not call any functions registered with atexit(3) or on_exit(3). Whether it flushes standard I/O buffers and removes temporary files created with tmpfile(3) is implementation-dependent. On the other hand, _exit() does close open file descriptors ...

adf88
A: 

1.exit() : it's cleanup the work like closing file descriptor, file stream and so on, 2._exit() : it's not cleanup the work like closing the file descriptor,file stream and so on

These are the major difference of exit() and _exit().

am i rectified ur answer

AdalArasan
_exit does close file descriptors.
adf88
+4  A: 

Normative in C99 are exit and _Exit.

The difference between the two is that exit also executes the handlers that may be registered with atexit and closes streams etc whereas _Exit doesn't call the atexit routines and may or may not close streams properly.

_exit is from POSIX and has similar properties as _Exit with the difference that it is guaranteed to close streams properly.

In summary, whenever you can you should use exit, this is the cleanest way to terminate.

Jens Gustedt