ccplusplus.com
Learn C, C++ Concepts
Wednesday, October 26, 2011
abort example c
/**************************************************************** * Filename : abort.c * Author : Saurabh Gupta * Source : http://saurabhgupta0527.blogspot.com/2011/08/posix-signal-tutorial.html * Desc : Implementation of POSIX.1 abort * abort function in unix * abort function example * Created : AM 07:37 26 October 2011 * Note : ****************************************************************/ #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* * POSIX-style abort() function */ void abort(void) { sigset_t mask; struct sigaction action; /* * Caller can't ignore SIGABRT, if so reset to default. */ sigaction(SIGABRT, NULL, &action); if (action.sa_handler == SIG_IGN) { action.sa_handler = SIG_DFL; sigaction(SIGABRT, &action, NULL); } if (action.sa_handler == SIG_DFL) { fflush(NULL); /* flush all open stdio streams */ } /* * Caller can't block SIGABRT; make sure it's unblocked. */ sigfillset(&mask); sigdelset(&mask, SIGABRT); /* mask has only SIGABRT turned off */ sigprocmask(SIG_SETMASK, &mask, NULL); kill(getpid(), SIGABRT); /* send the signal */ /* * If we're here, process caught SIGABRT and returned. */ fflush(NULL); /* flush all open stdio streams */ action.sa_handler = SIG_DFL; sigaction(SIGABRT, &action, NULL); /* reset to default */ sigprocmask(SIG_SETMASK, &mask, NULL); /* just in case ... */ kill(getpid(), SIGABRT); /* and one more time */ exit(1); /* this should never be executed ... */ } int main () { abort (); return 0; } /** * OUTPUT * [sgupta@rhel55x86 signal]$ gcc -o abort abort.c [sgupta@rhel55x86 signal]$ ./abort Aborted [sgupta@rhel55x86 signal]$ */
See Also
POSIX Signal Tutorial and example codes
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment