1PTHREAD_SIGMASK(3P)        POSIX Programmer's Manual       PTHREAD_SIGMASK(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       pthread_sigmask, sigprocmask — examine and change blocked signals
13

SYNOPSIS

15       #include <signal.h>
16
17       int pthread_sigmask(int how, const sigset_t *restrict set,
18           sigset_t *restrict oset);
19       int sigprocmask(int how, const sigset_t *restrict set,
20           sigset_t *restrict oset);
21

DESCRIPTION

23       The pthread_sigmask() function shall examine or change  (or  both)  the
24       calling  thread's  signal  mask, regardless of the number of threads in
25       the process. The function shall be equivalent to sigprocmask(), without
26       the restriction that the call be made in a single-threaded process.
27
28       In  a single-threaded process, the sigprocmask() function shall examine
29       or change (or both) the signal mask of the calling thread.
30
31       If the argument set is not a null pointer, it points to a set  of  sig‐
32       nals to be used to change the currently blocked set.
33
34       The argument how indicates the way in which the set is changed, and the
35       application shall ensure it consists of one of the following values:
36
37       SIG_BLOCK   The resulting set shall be the union of the current set and
38                   the signal set pointed to by set.
39
40       SIG_SETMASK The  resulting  set  shall  be the signal set pointed to by
41                   set.
42
43       SIG_UNBLOCK The resulting set shall be the intersection of the  current
44                   set and the complement of the signal set pointed to by set.
45
46       If  the argument oset is not a null pointer, the previous mask shall be
47       stored in the location pointed to by oset.  If set is a  null  pointer,
48       the  value of the argument how is not significant and the thread's sig‐
49       nal mask shall be unchanged; thus the call can be used to enquire about
50       currently blocked signals.
51
52       If  there  are any pending unblocked signals after the call to sigproc‐
53       mask(), at least one of those signals shall  be  delivered  before  the
54       call to sigprocmask() returns.
55
56       It  is  not  possible  to  block those signals which cannot be ignored.
57       This shall be enforced by the system without causing  an  error  to  be
58       indicated.
59
60       If  any of the SIGFPE, SIGILL, SIGSEGV, or SIGBUS signals are generated
61       while they are blocked, the result is undefined, unless the signal  was
62       generated  by the action of another process, or by one of the functions
63       kill(), pthread_kill(), raise(), or sigqueue().
64
65       If sigprocmask() fails, the thread's signal mask shall not be changed.
66
67       The use of the  sigprocmask()  function  is  unspecified  in  a  multi-
68       threaded process.
69

RETURN VALUE

71       Upon successful completion pthread_sigmask() shall return 0; otherwise,
72       it shall return the corresponding error number.
73
74       Upon successful completion, sigprocmask() shall return 0; otherwise, -1
75       shall  be  returned,  errno shall be set to indicate the error, and the
76       signal mask of the process shall be unchanged.
77

ERRORS

79       The pthread_sigmask() and sigprocmask() functions shall fail if:
80
81       EINVAL The value of the how argument is not equal to one of the defined
82              values.
83
84       The  pthread_sigmask()  function  shall  not  return  an  error code of
85       [EINTR].
86
87       The following sections are informative.
88

EXAMPLES

90   Signaling in a Multi-Threaded Process
91       This example shows the use of pthread_sigmask() in order to  deal  with
92       signals  in  a  multi-threaded  process.  It  provides a fairly general
93       framework that could be easily adapted/extended.
94
95
96           #include <stdio.h>
97           #include <stdlib.h>
98           #include <pthread.h>
99           #include <signal.h>
100           #include <string.h>
101           #include <errno.h>
102           ...
103
104           static sigset_t   signal_mask;  /* signals to block         */
105
106           int main (int argc, char *argv[])
107           {
108               pthread_t  sig_thr_id;      /* signal handler thread ID */
109               int        rc;              /* return code              */
110
111               sigemptyset (&signal_mask);
112               sigaddset (&signal_mask, SIGINT);
113               sigaddset (&signal_mask, SIGTERM);
114               rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);
115               if (rc != 0) {
116                   /* handle error */
117                   ...
118               }
119               /* any newly created threads inherit the signal mask */
120
121               rc = pthread_create (&sig_thr_id, NULL, signal_thread, NULL);
122               if (rc != 0) {
123                   /* handle error */
124                   ...
125               }
126
127               /* APPLICATION CODE */
128               ...
129           }
130
131           void *signal_thread (void *arg)
132           {
133               int       sig_caught;    /* signal caught       */
134               int       rc;            /* returned code       */
135
136               rc = sigwait (&signal_mask, &sig_caught);
137               if (rc != 0) {
138                   /* handle error */
139               }
140               switch (sig_caught)
141               {
142               case SIGINT:     /* process SIGINT  */
143                   ...
144                   break;
145               case SIGTERM:    /* process SIGTERM */
146                   ...
147                   break;
148               default:         /* should normally not happen */
149                   fprintf (stderr, "\nUnexpected signal %d\n", sig_caught);
150                   break;
151               }
152           }
153

APPLICATION USAGE

155       None.
156

RATIONALE

158       When a thread's signal mask is changed in  a  signal-catching  function
159       that is installed by sigaction(), the restoration of the signal mask on
160       return from the signal-catching function  overrides  that  change  (see
161       sigaction()).   If the signal-catching function was installed with sig‐
162       nal(), it is unspecified whether this occurs.
163
164       See kill() for a discussion of the requirement on delivery of signals.
165

FUTURE DIRECTIONS

167       None.
168

SEE ALSO

170       exec, kill(),  sigaction(),  sigaddset(),  sigdelset(),  sigemptyset(),
171       sigfillset(), sigismember(), sigpending(), sigqueue(), sigsuspend()
172
173       The Base Definitions volume of POSIX.1‐2017, <signal.h>
174
176       Portions  of  this text are reprinted and reproduced in electronic form
177       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
178       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
179       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
180       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
181       event of any discrepancy between this version and the original IEEE and
182       The  Open Group Standard, the original IEEE and The Open Group Standard
183       is the referee document. The original Standard can be  obtained  online
184       at http://www.opengroup.org/unix/online.html .
185
186       Any  typographical  or  formatting  errors that appear in this page are
187       most likely to have been introduced during the conversion of the source
188       files  to  man page format. To report such errors, see https://www.ker
189       nel.org/doc/man-pages/reporting_bugs.html .
190
191
192
193IEEE/The Open Group                  2017                  PTHREAD_SIGMASK(3P)
Impressum