1PTHREAD_SIGMASK(3) Linux Programmer's Manual PTHREAD_SIGMASK(3)
2
3
4
6 pthread_sigmask - examine and change mask of blocked signals
7
9 #include <signal.h>
10
11 int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
12
13 Compile and link with -pthread.
14
16 The pthread_sigmask() function is just like sigprocmask(2), with the
17 difference that its use in multithreaded programs is explicitly speci‐
18 fied by POSIX.1-2001. Other differences are noted in this page.
19
20 For a description of the arguments and operation of this function, see
21 sigprocmask(2).
22
24 On success, pthread_sigmask() returns 0; on error, it returns an error
25 number.
26
28 See sigprocmask(2).
29
31 POSIX.1-2001.
32
34 A new thread inherits a copy of its creator's signal mask.
35
37 The program below blocks some signals in the main thread, and then cre‐
38 ates a dedicated thread to fetch those signals via sigwait(3). The
39 following shell session demonstrates its use:
40
41 $ ./a.out &
42 [1] 5423
43 $ kill -QUIT %1
44 Signal handling thread got signal 3
45 $ kill -USR1 %1
46 Signal handling thread got signal 10
47 $ kill -TERM %1
48 [1]+ Terminated ./a.out
49
50 Program source
51
52 #include <pthread.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <signal.h>
57 #include <errno.h>
58
59 /* Simple error handling functions */
60
61 #define handle_error_en(en, msg) \
62 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
63
64 static void *
65 sig_thread(void *arg)
66 {
67 sigset_t *set = (sigset_t *) arg;
68 int s, sig;
69
70 for (;;) {
71 s = sigwait(set, &sig);
72 if (s != 0)
73 handle_error_en(s, "sigwait");
74 printf("Signal handling thread got signal %d\n", sig);
75 }
76 }
77
78 int
79 main(int argc, char *argv[])
80 {
81 pthread_t thread;
82 sigset_t set;
83 int s;
84
85 /* Block SIGINT; other threads created by main() will inherit
86 a copy of the signal mask. */
87
88 sigemptyset(&set);
89 sigaddset(&set, SIGQUIT);
90 sigaddset(&set, SIGUSR1);
91 s = pthread_sigmask(SIG_BLOCK, &set, NULL);
92 if (s != 0)
93 handle_error_en(s, "pthread_sigmask");
94
95 s = pthread_create(&thread, NULL, &sig_thread, (void *) &set);
96 if (s != 0)
97 handle_error_en(s, "pthread_create");
98
99 /* Main thread carries on to create other threads and/or do
100 other work */
101
102 pause(); /* Dummy pause so we can test program */
103 }
104
106 sigaction(2), sigpending(2), sigprocmask(2) pthread_create(3),
107 pthread_kill(3), sigsetops(3), pthreads(7), signal(7)
108
110 This page is part of release 3.25 of the Linux man-pages project. A
111 description of the project, and information about reporting bugs, can
112 be found at http://www.kernel.org/doc/man-pages/.
113
114
115
116Linux 2009-01-25 PTHREAD_SIGMASK(3)