atexit()

Discussion in 'C Programming' started by Steve Lambert, Sep 30, 2004.

  1. Hi,

    Is it possible for the functions registered with atexit() to have access to
    parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE?

    I'd like to register a set of routines to be executed on exit(EXIT_FAILURE)
    and a different set of routines on exit(EXIT_SUCCESS).

    Any way of doing this?

    Cheers

    Steve
    Steve Lambert, Sep 30, 2004
    #1
    1. Advertising

  2. Steve Lambert

    Mike Wahler Guest

    "Steve Lambert" <> wrote in message
    news:TmZ6d.740$...
    > Hi,
    >
    > Is it possible for the functions registered with atexit() to have access

    to
    > parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE?
    >
    > I'd like to register a set of routines to be executed on

    exit(EXIT_FAILURE)
    > and a different set of routines on exit(EXIT_SUCCESS).
    >
    > Any way of doing this?


    My first thought was to pass this info ('failure' or 'success')
    via an argument to the registered function. BUT:

    ==========================================
    From the C standard:

    7.20.4.2 The atexit function

    Synopsis

    1 #include <stdlib.h>
    int atexit(void (*func)(void));

    Description

    2 The atexit function registers the function pointed to by func,
    to be called without arguments at normal program termination.

    Environmental limits

    3 The implementation shall support the registration of at least
    32 functions.

    Returns

    4 The atexit function returns zero if the registration succeeds,
    nonzero if it fails.

    ==========================================


    So, as much as I dislike using globals, it seems that would
    be the way to pass this info: set a global to EXIT_SUCCESS
    or EXIT_FAILURE (or whatever other value you find useful),
    and have a 'controlling' registered function inspect that
    value, and invoke one of a set of other functions which
    performs the desired actions.

    -Mike
    Mike Wahler, Oct 1, 2004
    #2
    1. Advertising

  3. Groovy hepcat Mike Wahler was jivin' on Thu, 30 Sep 2004 23:21:34 GMT
    in comp.lang.c.
    Re: atexit()'s a cool scene! Dig it!

    >"Steve Lambert" <> wrote in message
    >news:TmZ6d.740$...
    >>
    >> Is it possible for the functions registered with atexit() to have access

    >to
    >> parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE?
    >>
    >> I'd like to register a set of routines to be executed on

    >exit(EXIT_FAILURE)
    >> and a different set of routines on exit(EXIT_SUCCESS).
    >>
    >> Any way of doing this?


    [Snip.]

    >So, as much as I dislike using globals, it seems that would
    >be the way to pass this info: set a global to EXIT_SUCCESS
    >or EXIT_FAILURE (or whatever other value you find useful),
    >and have a 'controlling' registered function inspect that
    >value, and invoke one of a set of other functions which
    >performs the desired actions.


    That's what I would have suggested if you hadn't beaten me to it. :)
    But I would add this.
    Make the so called global variable static and put it in its own
    translation unit. Also put in this translation unit an exit() wrapper
    that sets this variable and a function to read its value. When exiting
    the program, call the wrapper instead of exit() directly, and call
    the other function from the function registered with atexit().

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Oct 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Serve Laurijssen

    atexit and global destructors

    Serve Laurijssen, Jan 15, 2004, in forum: C++
    Replies:
    3
    Views:
    6,950
    tom_usenet
    Jan 15, 2004
  2. JKop
    Replies:
    8
    Views:
    448
  3. Bengt Richter
    Replies:
    0
    Views:
    280
    Bengt Richter
    Aug 25, 2004
  4. Chris Gorton

    atexit not being executed

    Chris Gorton, May 3, 2005, in forum: Python
    Replies:
    0
    Views:
    387
    Chris Gorton
    May 3, 2005
  5. David Rushby

    atexit + threads = bug?

    David Rushby, Jan 12, 2006, in forum: Python
    Replies:
    7
    Views:
    454
    David Rushby
    Jan 13, 2006
Loading...

Share This Page