Re: is atexit required?

Discussion in 'C Programming' started by James Kuyper, Oct 8, 2013.

  1. James Kuyper

    James Kuyper Guest

    On 10/08/2013 02:20 PM, mhnbtngr wrote:
    > hi group,
    > I am trying to understand atexit.
    > If I am not wrong,upon exit from our programm,OS automagically flushes
    > the buffers,cleansup not free'd memory.closes the files we had opened.
    > In that case why do we require atexit handlers?

    To handle other kinds of problems than buffer flushing, memory freeing,
    and file closing. For instance, imagine a program that keeps track of
    various statistics about what it did: how many packets of each type that
    it processed, for instance. When it's done, it's supposed to print out a
    report containing those statistics. It might be important to print out
    that report even if the program exits by calling exit(), in which case
    the report-writing code should be registered with atexit(). In fact, the
    summary report might even be more important, for debugging reasons, if
    the program exits by calling exit().

    I've never actually done this - my code normally responds to error
    conditions by returning an error code to the calling routine, not by
    calling exit(). However, many people are more fond of exit() than I am.

    > --
    > C is like universe.More you go deeper,more your ignorance becomes
    > apparent.

    Unlike the universe, C is definitely finite. It isn't even very large,
    not compared with some of the other languages I've learned. If you're
    still feeling the way described by that sig after having studied C for
    several years, you've got a problem; you should begin developing a
    feeling of mastery over the language by that time.
    James Kuyper, Oct 8, 2013
    1. Advertisements

  2. Seebs

    Seebs Guest

    On 2013-10-08, James Kuyper <> wrote:
    > To handle other kinds of problems than buffer flushing, memory freeing,
    > and file closing.

    It's a highly unportable example, but I had to use atexit() recently for
    such a thing. I maintain a package which does some really, really, highly
    unportable magic -- it intercepts filesystem operations and does things
    to change their reported results.

    As an optimization, it will send information about things it wants done
    to a server without necessarily waiting for a response, in cases where
    it wouldn't use the response.

    This, it turns out, creates a race condition: Consider a simple shell
    script, like:
    chmod 555 a
    chmod 755 a

    It is possible that, depending on timing, the database update from the second
    chmod command will end up being processed before the database update from
    the first. If this weren't a shell script, but rather, a program in some
    language which could directly invoke chmod, that would be not merely possible,
    but would occasionally happen.

    Solution: An atexit() handler which does a round-trip ping to the server,
    ensuring that, at least in the case of a clean exit, the process won't
    exit until the server has processed all of its outstanding queries. This
    actually works.

    But no amount of flushing buffers, etcetera, would have that effect.

    Copyright 2013, all wrongs reversed. Peter Seebach / <-- lawsuits, religion, and funny pictures
    Autism Speaks does not speak for me.
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Oct 8, 2013
    1. Advertisements

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++
    Jan 15, 2004
  2. JKop
  3. Bengt Richter
    Bengt Richter
    Aug 25, 2004
  4. Chris Gorton

    atexit not being executed

    Chris Gorton, May 3, 2005, in forum: Python
    Chris Gorton
    May 3, 2005
  5. Eric Sosman

    Re: is atexit required?

    Eric Sosman, Oct 8, 2013, in forum: C Programming
    Eric Sosman
    Oct 8, 2013

Share This Page