ccplusplus.com
Learn C, C++ Concepts
Wednesday, October 19, 2011
sigprocmask example
/*********************************************************************** * File : sigprocmask.c * Author : Saurabh Gupta * Desc : sigprocmask linux * sigprocmask example in C * Source : http://saurabhgupta0527.blogspot.com/2011/08/posix-signal-tutorial.html * Created : PM 10:17 19 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 */ void err_sys(const char *, ...); void pr_mask(const char *); static void err_doit(int, int, const char *, va_list); int main () { printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXXXXX sigprocmask example XXXXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("\n"); pr_mask ("http://saurabhgupta0527.blogspot.com"); printf ("\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /* * 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); } void pr_mask(const char *str) { sigset_t sigset; int errno_save; errno_save = errno; /* we can be called by signal handlers */ if (sigprocmask(0, NULL, &sigset) < 0) { err_sys("sigprocmask error"); } printf("%s", str); if (sigismember(&sigset, SIGINT)) { printf("SIGINT "); } if (sigismember(&sigset, SIGQUIT)) { printf("SIGQUIT "); } if (sigismember(&sigset, SIGUSR1)) { printf("SIGUSR1 "); } if (sigismember(&sigset, SIGALRM)) { printf("SIGALRM "); } /* * remaining signals can go here */ printf("\n"); errno = errno_save; } /* * 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 sigprocmask.c -o sigprocmask [sgupta@rhel55x86 signal]$ ./sigprocmask XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX sigprocmask example XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX http://saurabhgupta0527.blogspot.com XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel55x86 signal]$ */
See Also
POSIX Signal tutorial and example codes
1 comment:
Unknown
December 14, 2020 at 9:31 AM
n
Reply
Delete
Replies
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
n
ReplyDelete