Aborting

Discussion in 'C Programming' started by Stefan Ram, Jan 18, 2010.

  1. Stefan Ram

    Stefan Ram Guest

    Assuming that a program is in such a desperate situation,
    that one wants to abort immediatly (for example, no more
    memory is available), when should one use

    exit( 99 )

    (where »99« is a placeholder for any possible error code) and when

    abort()

    ?
     
    Stefan Ram, Jan 18, 2010
    #1
    1. Advertising

  2. Stefan Ram

    Lew Pitcher Guest

    On January 18, 2010 15:27, in comp.lang.c, -berlin.de wrote:

    > Assuming that a program is in such a desperate situation,
    > that one wants to abort immediatly (for example, no more
    > memory is available), when should one use
    >
    > exit( 99 )
    >
    > (where »99« is a placeholder for any possible error code) and when
    >
    > abort()


    I would suspect that the decision to use abort() or exit() would depend a
    lot on the circumstances of the error, the goals of the application, and
    the QoI of the C implementation.

    abort()
    - does not guarantee to flush open buffered output streams,
    - does not guarantee to close open streams of any sort,
    - does not guarantee to remove temporary files, and
    - returns an implementation-defined "unsuccessful termination" status.

    OTOH, exit()
    - executes the functions registered through the atexit() function
    - flushes open buffered output streams
    - closes all open streams
    - removes temporary files, and
    - returns a program-specified exit status to the host environment

    I'd use abort() only in the direst of circumstances, where the program logic
    cannot guarantee the sanity of /any/ of it's data. For all other
    terminations, I'd use exit().

    And, I'd document the use carefully, including expected behaviour and
    recovery options.

    HTH
    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Jan 18, 2010
    #2
    1. Advertising

  3. Stefan Ram

    Ben Pfaff Guest

    -berlin.de (Stefan Ram) writes:

    > Assuming that a program is in such a desperate situation,
    > that one wants to abort immediatly (for example, no more
    > memory is available), when should one use
    >
    > exit( 99 )
    >
    > (where »99« is a placeholder for any possible error code) and when
    >
    > abort()


    On Unix-like systems, abort() will often cause a core dump, but
    exit() will not. It is usually much easier to find a difficult
    bug with a core dump than without one, so that's part of the
    criteria that I use.

    Another way to look at it is that the assert macro exits by
    calling abort(), so that it is appropriate to call abort() in
    circumstances comparable to assertion failures.
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Jan 18, 2010
    #3
  4. Stefan Ram

    frank Guest

    Lew Pitcher wrote:
    > On January 18, 2010 15:27, in comp.lang.c, -berlin.de wrote:
    >
    >> Assuming that a program is in such a desperate situation,
    >> that one wants to abort immediatly (for example, no more
    >> memory is available), when should one use
    >>
    >> exit( 99 )
    >>
    >> (where »99« is a placeholder for any possible error code) and when
    >>
    >> abort()

    >
    > I would suspect that the decision to use abort() or exit() would depend a
    > lot on the circumstances of the error, the goals of the application, and
    > the QoI of the C implementation.
    >
    > abort()
    > - does not guarantee to flush open buffered output streams,
    > - does not guarantee to close open streams of any sort,
    > - does not guarantee to remove temporary files, and
    > - returns an implementation-defined "unsuccessful termination" status.


    Is this last one synonymous with the requirement that __FILE__ and
    __LINE__ be given?
    >
    > OTOH, exit()
    > - executes the functions registered through the atexit() function
    > - flushes open buffered output streams
    > - closes all open streams
    > - removes temporary files, and
    > - returns a program-specified exit status to the host environment
    >
    > I'd use abort() only in the direst of circumstances, where the program logic
    > cannot guarantee the sanity of /any/ of it's data. For all other
    > terminations, I'd use exit().
    >
    > And, I'd document the use carefully, including expected behaviour and
    > recovery options.


    An interesting contrast, Lew. Which temporary files do you mean?
    --
    frank
     
    frank, Jan 19, 2010
    #4
  5. frank <> writes:
    > Lew Pitcher wrote:

    [...]
    >> abort()
    >> - does not guarantee to flush open buffered output streams,
    >> - does not guarantee to close open streams of any sort,
    >> - does not guarantee to remove temporary files, and
    >> - returns an implementation-defined "unsuccessful termination" status.

    >
    > Is this last one synonymous with the requirement that __FILE__ and
    > __LINE__ be given?


    What? No, it's completely unrelated.

    The assert() macro writes the values of__FILE__ and __LINE__ to
    stderr. There is no such requirement for abort(). And even if there
    were, it would have nothing to do with the termination status.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 19, 2010
    #5
  6. On 19 Jan, 05:33, frank <> wrote:
    > Lew Pitcher wrote:
    > > On January 18, 2010 15:27, in comp.lang.c, -berlin.de wrote:


    <snip>

    > > abort()

    [...]
    > >  - returns an implementation-defined "unsuccessful termination" status.

    >
    > Is this last one synonymous with the requirement that __FILE__ and
    > __LINE__ be given?


    no. The "unsuccessful termination" status is some sort of indication
    to the underlying OS. Typically an integer indicating failure. abort()
    is not required to give file and line number. Typically it doesn't.

    <snip>

    > > OTOH, exit()

    [...]
    > >  - removes temporary files

    [...]
    > [...]  Which temporary files do you mean?


    ones generated by the tmpfile() function (maybe others?)
     
    Nick Keighley, Jan 19, 2010
    #6
  7. In article <>, frank <> writes:
    > Lew Pitcher wrote:


    >> abort()
    >> - returns an implementation-defined "unsuccessful termination" status.

    >
    > Is this last one synonymous with the requirement that __FILE__ and
    > __LINE__ be given?


    As others have pointed out, no. If you care about the SUS: see
    WIFSIGNALED() / WTERMSIG(). Links to assert(), abort() and waitpid() in
    SUSv2:

    http://www.opengroup.org/onlinepubs/007908775/xsh/assert.html
    http://www.opengroup.org/onlinepubs/007908775/xsh/abort.html
    http://www.opengroup.org/onlinepubs/007908775/xsh/wait.html

    Cheers,
    lacos
     
    Ersek, Laszlo, Jan 19, 2010
    #7
    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. Willem Oosthuizen

    Aborting Fucntions

    Willem Oosthuizen, Jul 10, 2003, in forum: VHDL
    Replies:
    2
    Views:
    1,050
  2. Wayne MJ

    Aborting a _Click event?

    Wayne MJ, Jul 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    384
    Wayne MJ
    Jul 23, 2003
  3. Wayne MJ

    Re: Aborting a _Click event?

    Wayne MJ, Jul 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    505
    Wayne MJ
    Jul 29, 2003
  4. Jeff Greenland

    .NET Timers aborting in web application

    Jeff Greenland, Dec 18, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    566
    Jeff Greenland
    Dec 18, 2003
  5. Jeffrey A. Voigt

    Aborting a Request?

    Jeffrey A. Voigt, Feb 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    849
    Jeffrey A. Voigt
    Feb 23, 2004
Loading...

Share This Page