ccplusplus.com
Learn C, C++ Concepts
Thursday, October 20, 2011
sigpending example in c
/*********************************************************************** * File : sigpending.c * Author : Saurabh Gupta * Desc : sigpending linux * sigpending example in c * Source : http://saurabhgupta0527.blogspot.com/2011/08/posix-signal-tutorial.html * Created : PM 10:10 20 October 2011 * Note : ************************************************************************/ #define _XOPEN_SOURCE 600 /* Single UNIX Specification, Version 3 */ #include <sys/types.h> /* some systems still require this */ #include <sys/stat.h> #include <sys/termios.h> /* for winsize */ #ifndef TIOCGWINSZ #include <sys/ioctl.h> #endif #include <stdio.h> /* for convenience */ #include <stdlib.h> /* for convenience */ #include <stddef.h> /* for offsetof */ #include <string.h> /* for convenience */ #include <unistd.h> /* for convenience */ #include <signal.h> /* for SIG_ERR */ #include <errno.h> /* for definition of errno */ #include <stdarg.h> /* ISO C variable aruments */ #define MAXLINE 4096 /* max line length */ static void sig_quit(int); void err_sys(const char *, ...); static void err_doit(int, int, const char *, va_list); int main(void) { printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXXXXX sigpending example XXXXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("\n"); sigset_t newmask, oldmask, pendmask; if (signal(SIGQUIT, sig_quit) == SIG_ERR) { err_sys("can't catch SIGQUIT"); } /* * Block SIGQUIT and save current signal mask. */ sigemptyset(&newmask); sigaddset(&newmask, SIGQUIT); if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) { err_sys("SIG_BLOCK error"); } sleep(5); /* * SIGQUIT here will remain pending */ if (sigpending(&pendmask) < 0) { err_sys("sigpending error"); } if (sigismember(&pendmask, SIGQUIT)) printf("\nSIGQUIT pending\n"); /* * Reset signal mask which unblocks SIGQUIT. */ if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) { err_sys("SIG_SETMASK error"); } printf("SIGQUIT unblocked\n"); sleep(5); /* * SIGQUIT here will terminate with core file */ printf ("\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); exit(0); } static void sig_quit(int signo) { printf("caught SIGQUIT\n"); if (signal(SIGQUIT, SIG_DFL) == SIG_ERR) err_sys("can't reset SIGQUIT"); } /* * Fatal error related to a system call. * Print a message and terminate. */ void err_sys(const char *fmt, ...) { va_list ap; va_start(ap, fmt); err_doit(1, errno, fmt, ap); va_end(ap); exit(1); } /* * Print a message and return to caller. * Caller specifies "errnoflag". */ static void err_doit(int errnoflag, int error, const char *fmt, va_list ap) { char buf[MAXLINE]; vsnprintf(buf, MAXLINE, fmt, ap); if (errnoflag) { snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s", strerror(error)); } strcat(buf, "\n"); fflush(stdout); /* in case stdout and stderr are the same */ fputs(buf, stderr); fflush(NULL); /* flushes all stdio output streams */ } /* * OUTPUT * [sgupta@rhel55x86 signal]$ gcc sigpending.c -o sigpending [sgupta@rhel55x86 signal]$ ./sigpending XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX sigprocmask example XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX SIGQUIT unblocked XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel55x86 signal]$ */
See Also
Sigprocmask
POSIX Tutorial and Siganl Sample C codes
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment