Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between the on_exit() and atexit() functions?

Tags:

Is there any difference between

   int on_exit(void (*function)(int , void *), void *arg); 

and

   int atexit(void (*function)(void)); 

other than the fact that the function used by on_exit gets the exit status?

That is, if I don't care about the exit status, is there any reason to use one or the other?

Edit: Many of the answers warned against on_exit because it's non-standard. If I'm developing an app that is for internal corporate use and guaranteed to run on specific configurations, should I worry about this?

like image 759
Nathan Fellman Avatar asked Aug 14 '08 04:08

Nathan Fellman


People also ask

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.

What is the use of atexit () function?

The atexit() function registers the given function to be called at normal process termination, either via exit(3) or via return from the program's main(). Functions so registered are called in the reverse order of their registration; no arguments are passed.

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 atexit function in C?

The C library function int atexit(void (*func)(void)) causes the specified function func to be called when the program terminates. You can register your termination function anywhere you like, but it will be called at the time of the program termination.


1 Answers

You should use atexit() if possible. on_exit() is nonstandard and less common. For example, it's not available on OS X.

Kernel.org - on_exit():

This function comes from SunOS 4, but is also present in libc4, libc5 and glibc. It no longer occurs in Solaris (SunOS 5). Avoid this function, and use the standard atexit(3) instead.

like image 133
Derek Park Avatar answered Oct 20 '22 05:10

Derek Park