User:Vcdhanesh/sandbox

From Wikibooks, open books for an open world
Jump to navigation Jump to search

C programming standard library function .It is used in programming process or environment to stop the program or process abnormally.This function is required in the program when wrong condition gets encountered in program execution then to come out of process this function is used.

Introduction[edit | edit source]

It deletes buffers and closes all open files before ending the program. The function abort() terminates the current program. Depending on the implementation, the return from the function can indicate a canceled (e.g. you used the signal() function to catch SIGABRT) or failed abort. SIGABRT is sent by the process to itself when it calls the abort libc function, defined in cstdlib. The SIGABRT signal can be caught, but it cannot be blocked; if the signal handler returns then all open streams are closed and flushed and the program terminates (dumping core if appropriate) , then it returns control to host environment.This means that the abort call never returns. Because of this characteristic, it is often used to signal fatal conditions in support libraries, situations where the current operation cannot be completed but the main program can perform cleanup before exiting. It is also used if an assertion fails.

Header files & Syntax[edit | edit source]

#include<stdio.h> 
#include <cstdlib.h>
void abort( void );

Return Value[edit | edit source]

This function does not return any value i.e. it is of void datatype.

Thread safety[edit | edit source]

It is one of the thread safe functions from standard c library.i.e. function can be called by different threads without any problem.

Example[edit | edit source]

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;
     if ((stream = fopen("mylib/myfile", "r")) == NULL)
     {      perror("Could not open data file");
           abort();
     }
}

System code for UNIX[edit | edit source]

#include <sys/cdefs.h>
#include "namespace.h"
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

extern void (*__cleanup) __P((void));
static int aborting = 0;

void abort()
{
	sigset_t mask;

	sigfillset(&mask); /*
	                     don't block SIGABRT to give any handler a chance; we ignore
	                     any errors that doesn't allow abort to return anyway.
	                     */
	sigdelset(&mask, SIGABRT);
	(void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
         
        /* POSIX.1 requires that stdio buffers be flushed on abort.
	  cleanup routines should be called only once in
	  case the user calls abort() in a SIGABRT handler.
	 */

        if (!aborting) 
        {
		aborting = 1;

		if (__cleanup)
			(*__cleanup)();
	}

	(void)raise(SIGABRT);
	(void)signal(SIGABRT, SIG_DFL);
	(void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
	(void)raise(SIGABRT);
	_exit(1);
}
Related topics
assert - atexit - exit

[[Category:C Programming|Template:Abort]]