tags:

views:

832

answers:

6

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?

+10  A: 

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.

Derek Park
+3  A: 

According to this link I found, it seems there are a few differences. on_exit will let you pass in an argument that is passed in to the on_exit function when it is called... which might let you set up some pointers to do some cleanup work on when it is time to exit.

Furthermore, it appears that on_exit was a SunOS specific function that may not be compatible on all platforms... so you may want to stick with atexit, despite it being more restrictive.

Mike Stone
A: 

if that is the case, is there any good way for the function called by atexit to know the exit status?

For instance, I'd use atexit for all the stuff that needs to be done anytime the program ends, but onexit for all the stuff that needs to be done only if the exit status is 0 (successful) or !=0. If onexit isn't recommended, how can I do things based on the exit status?

Nathan Fellman
A: 

@Nathan

First, see if there is another API call to determine exit status... a quick glance and I don't see one, but I am not well versed in the standard C API.

An easy alternative is to have a global variable that stores the exit status... the default being an unknown error cause (for if the program terminates abnormally). Then, when you call exit, you can store the exit status in the global and retrieve it from any atexit functions. This requires storing the exit status diligently before every exit call, and clearly is not ideal, but if there is no API and you don't want to risk on_exit not being on the platform... it might be the only option.

Mike Stone
+1  A: 

@Nathan, I can't find any function that will return the exit code for the current running process. I expect that it isn't set yet at the point when atexit() is called, anyway. By this I mean that the runtime knows what it is, but probably hasn't reported it to the OS. This is pretty much just conjecture, though.

It looks like you will either need to use on_exit() or structure your program so that the exit code doesn't matter. It would not be unreasonable to have the last statement in your main function flip a global exited_cleanly variable to true. In the function you register with atexit(), you could check this variable to determine how the program exited. This will only give you two states, but I expect that would be sufficient for most needs. You could also expand this type of scheme to support more exit states if necessary.

Derek Park
+1  A: 

The difference is that atexit is C and on_exit is some weird extension available on GNU and who-knows-what-other Unixy systems (but NOT part of POSIX).

R..