abort() — Stop a program

Standards

Standards / Extensions C or C++ Dependencies
ISO C
POSIX.1
XPG4
XPG4.2
C99
C11
Single UNIX Specification, Version 3
both  

Format

#include <stdlib.h>

__noreturn__ void abort(void);

General description

Causes an abnormal program termination and returns control to the host environment. The abort() function flushes all buffers and closes all open files. Be aware that abnormal termination will not run the atexit() list functions.

If the abort() function is called and the user has a handler for SIGABRT, then SIGABRT is raised; however, SIGABRT is raised again when the handler associated with the default action is returned. The code path only passes through the user's handler once, even if the handler is reset. The same thing occurs if SIGABRT is ignored; abnormal termination occurs.

The abort() function will not result in program termination if SIGABRT is caught by a signal handler, and the signal handler does not return. You can avoid returning by “jumping” out of the handler with setjmp() and longjmp(). In z/OS® XL C programs, you can jump out of the handler with sigsetjmp() and siglongjmp().

For more information see the process termination information in the topic “Using Runtime User Exits” in z/OS XL C/C++ Programming Guide.

Special behavior for POSIX C: To obtain access to the special POSIX behavior for abort(), the POSIX runtime option must be set ON.

Calls to abort() raise the SIGABRT signal, using pthread_kill(), so that the signal is directed to the same thread. A SIGABRT signal generated by abort() cannot be blocked.

Under POSIX, the handler can use siglongjmp() to restore the environment at a place in the code where a sigsetjmp() was previously issued. In this way, an application can avoid the process for termination.

Special behavior for C++: If abort() is called from a z/OS XL C++ program, the program will be terminated immediately, without leaving the current block. Functions passed to atexit(), and destructors for static and local (automatic) objects, will not be called.

By default, the z/OS XL C++ function terminate() calls abort().

Returned value

The abnormal termination return code for z/OS is 2000.

Example

CELEBA01
/* CELEBA01

   This example tests for successful opening of the file myfile.
   If an error occurs, an error message is printed and the program
   ends with a call to the abort() function.

 */
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

   FILE *stream;
   unlink("myfile.dat");
   if ((stream = fopen("myfile.dat", "r")) == NULL)
   {
      printf("Could not open data file\n");
      abort();

      printf("Should not see this message\n");
   }
}

Related information