]>
Commit | Line | Data |
---|---|---|
b4ed7957 MK |
1 | .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk |
2 | .\" <mtk.manpages@gmail.com> | |
3 | .\" | |
5fbde956 | 4 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
b4ed7957 | 5 | .\" |
4c1c5274 | 6 | .TH pthread_sigmask 3 (date) "Linux man-pages (unreleased)" |
b4ed7957 MK |
7 | .SH NAME |
8 | pthread_sigmask \- examine and change mask of blocked signals | |
0a9455e8 AC |
9 | .SH LIBRARY |
10 | POSIX threads library | |
8fc3b2cf | 11 | .RI ( libpthread ", " \-lpthread ) |
b4ed7957 MK |
12 | .SH SYNOPSIS |
13 | .nf | |
14 | .B #include <signal.h> | |
f90f031e | 15 | .PP |
b4ed7957 MK |
16 | .BI "int pthread_sigmask(int " how ", const sigset_t *" set \ |
17 | ", sigset_t *" oldset ); | |
18 | .fi | |
68e4db0a | 19 | .PP |
d39ad78f | 20 | .RS -4 |
fc1d1e69 MK |
21 | Feature Test Macro Requirements for glibc (see |
22 | .BR feature_test_macros (7)): | |
d39ad78f | 23 | .RE |
68e4db0a | 24 | .PP |
fc1d1e69 | 25 | .BR pthread_sigmask (): |
9d2adbae | 26 | .nf |
5c10d2c5 | 27 | _POSIX_C_SOURCE >= 199506L || _XOPEN_SOURCE >= 500 |
9d2adbae | 28 | .fi |
b4ed7957 MK |
29 | .SH DESCRIPTION |
30 | The | |
31 | .BR pthread_sigmask () | |
32 | function is just like | |
33 | .BR sigprocmask (2), | |
34 | with the difference that its use in multithreaded programs | |
3196395a | 35 | is explicitly specified by POSIX.1. |
b4ed7957 | 36 | Other differences are noted in this page. |
847e0d88 | 37 | .PP |
b4ed7957 MK |
38 | For a description of the arguments and operation of this function, see |
39 | .BR sigprocmask (2). | |
40 | .SH RETURN VALUE | |
41 | On success, | |
42 | .BR pthread_sigmask () | |
43 | returns 0; | |
44 | on error, it returns an error number. | |
45 | .SH ERRORS | |
46 | See | |
47 | .BR sigprocmask (2). | |
21f80449 | 48 | .SH ATTRIBUTES |
f5104723 PH |
49 | For an explanation of the terms used in this section, see |
50 | .BR attributes (7). | |
c466875e MK |
51 | .ad l |
52 | .nh | |
f5104723 PH |
53 | .TS |
54 | allbox; | |
c466875e | 55 | lbx lb lb |
f5104723 PH |
56 | l l l. |
57 | Interface Attribute Value | |
58 | T{ | |
21f80449 | 59 | .BR pthread_sigmask () |
f5104723 PH |
60 | T} Thread safety MT-Safe |
61 | .TE | |
c466875e MK |
62 | .hy |
63 | .ad | |
64 | .sp 1 | |
3113c7f3 | 65 | .SH STANDARDS |
3196395a | 66 | POSIX.1-2001, POSIX.1-2008. |
b4ed7957 MK |
67 | .SH NOTES |
68 | A new thread inherits a copy of its creator's signal mask. | |
847e0d88 | 69 | .PP |
bce38798 MK |
70 | The glibc |
71 | .BR pthread_sigmask () | |
72 | function silently ignores attempts to block the two real-time signals that | |
73 | are used internally by the NPTL threading implementation. | |
74 | See | |
75 | .BR nptl (7) | |
76 | for details. | |
a14af333 | 77 | .SH EXAMPLES |
b4ed7957 MK |
78 | The program below blocks some signals in the main thread, |
79 | and then creates a dedicated thread to fetch those signals via | |
80 | .BR sigwait (3). | |
81 | The following shell session demonstrates its use: | |
847e0d88 | 82 | .PP |
b4ed7957 | 83 | .in +4n |
b8302363 | 84 | .EX |
b4ed7957 MK |
85 | .RB "$" " ./a.out &" |
86 | [1] 5423 | |
87 | .RB "$" " kill \-QUIT %1" | |
88 | Signal handling thread got signal 3 | |
89 | .RB "$" " kill \-USR1 %1" | |
90 | Signal handling thread got signal 10 | |
91 | .RB "$" " kill \-TERM %1" | |
92 | [1]+ Terminated ./a.out | |
b8302363 | 93 | .EE |
b4ed7957 MK |
94 | .in |
95 | .SS Program source | |
96 | \& | |
b0b6ab4e | 97 | .\" SRC BEGIN (pthread_sigmask.c) |
e7d0bb47 | 98 | .EX |
ad3868f0 | 99 | #include <errno.h> |
b4ed7957 | 100 | #include <pthread.h> |
ad3868f0 | 101 | #include <signal.h> |
b4ed7957 MK |
102 | #include <stdio.h> |
103 | #include <stdlib.h> | |
104 | #include <unistd.h> | |
b4ed7957 MK |
105 | |
106 | /* Simple error handling functions */ | |
107 | ||
d1a71985 | 108 | #define handle_error_en(en, msg) \e |
b4ed7957 MK |
109 | do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) |
110 | ||
111 | static void * | |
112 | sig_thread(void *arg) | |
113 | { | |
13f78d96 | 114 | sigset_t *set = arg; |
b4ed7957 MK |
115 | int s, sig; |
116 | ||
117 | for (;;) { | |
118 | s = sigwait(set, &sig); | |
119 | if (s != 0) | |
120 | handle_error_en(s, "sigwait"); | |
d1a71985 | 121 | printf("Signal handling thread got signal %d\en", sig); |
b4ed7957 MK |
122 | } |
123 | } | |
124 | ||
125 | int | |
f1e39c1e | 126 | main(void) |
b4ed7957 MK |
127 | { |
128 | pthread_t thread; | |
129 | sigset_t set; | |
130 | int s; | |
131 | ||
4a905b92 MK |
132 | /* Block SIGQUIT and SIGUSR1; other threads created by main() |
133 | will inherit a copy of the signal mask. */ | |
b4ed7957 MK |
134 | |
135 | sigemptyset(&set); | |
136 | sigaddset(&set, SIGQUIT); | |
137 | sigaddset(&set, SIGUSR1); | |
138 | s = pthread_sigmask(SIG_BLOCK, &set, NULL); | |
139 | if (s != 0) | |
140 | handle_error_en(s, "pthread_sigmask"); | |
141 | ||
a0303c05 | 142 | s = pthread_create(&thread, NULL, &sig_thread, &set); |
b4ed7957 MK |
143 | if (s != 0) |
144 | handle_error_en(s, "pthread_create"); | |
145 | ||
146 | /* Main thread carries on to create other threads and/or do | |
c6beb8a1 | 147 | other work. */ |
b4ed7957 MK |
148 | |
149 | pause(); /* Dummy pause so we can test program */ | |
150 | } | |
e7d0bb47 | 151 | .EE |
b0b6ab4e | 152 | .\" SRC END |
b4ed7957 MK |
153 | .SH SEE ALSO |
154 | .BR sigaction (2), | |
155 | .BR sigpending (2), | |
9e88dbc3 | 156 | .BR sigprocmask (2), |
96841f0e | 157 | .BR pthread_attr_setsigmask_np (3), |
b4ed7957 MK |
158 | .BR pthread_create (3), |
159 | .BR pthread_kill (3), | |
160 | .BR sigsetops (3), | |
161 | .BR pthreads (7), | |
162 | .BR signal (7) |