ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/users_96.htm

303 lines
10 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>pthread_sigmask()--Set or Get Signal Mask</title>
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Begin Header Records ========================================== -->
<!-- NETMG2 SCRIPT A converted by B2H R4.1 (346) (CMS) by HOLTJM at -->
<!-- RCHVMW2 on 29 Jan 1999 at 10:01:37 -->
<!--File Edited November 2001 -->
<!--End Header Records -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!-- Java sync-link -->
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript">
</script>
<a name="Top_Of_Page"></a>
<h2>pthread_sigmask()--Set or Get Signal Mask</h2>
<div class="box" style="width: 60%;">
<br>
&nbsp;&nbsp;Syntax:
<pre> #include &lt;pthread.h&gt;
#include &lt;signal.h&gt;
int pthread_sigmask(int how, const sigset_t *set,
sigset_t *oset);</pre>
&nbsp;&nbsp;Service Program Name: QP0WPTHR<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE <br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Signal Safe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>pthread_sigmask</strong>() function examines or modifies the signal blocking mask for the current thread.</p>
<p>The signals <strong>SIGKILL</strong> or <strong>SIGStop</strong> cannot be blocked. Any attempt to use <strong>pthread_sigmask</strong>() to block these signals is simply ignored, and no error is returned.</p>
<p><strong>SIGFPE</strong>, <strong>SIGILL</strong>, and <strong>SIGSEGV</strong> signals that are not artificially generated by <strong>kill</strong>(), <strong>pthread_kill</strong>() or <strong>raise</strong>() (that is, were generated by the system as a result of a hardware or software exception) are not blocked.</p>
<p>If there are any pending unblocked signals after <strong>pthread_sigmask</strong>() has changed the signal mask, at least one of those signals is delivered to the process before <strong>pthread_sigmask</strong>() returns.</p>
<p>If <strong>pthread_sigmask</strong>() fails, the signal mask of the thread is not changed.</p>
<p>The possible values for <em>how</em>, which are defined in the <strong>&lt;signal.h&gt;</strong> header file, are as follows:</p>
<dl>
<dt><strong>SIG_BLOCK</strong></dt>
<dd><p>Indicates that the set of signals given by <em>set</em> should be blocked, in addition to the set currently being blocked</p></dd>
<dt><strong>SIG_UNBLOCK</strong></dt>
<dd><p>Indicates that the set of signals given by <em>set</em> should not be blocked. These signals are removed from the current set
of signals being blocked</p></dd>
<dt><strong>SIG_SETMASK</strong></dt>
<dd><p>Indicates that the set of signals given by <em>set</em> should replace the old set of signals being blocked</p></dd>
</dl>
<p>The <em>set</em> parameter points to a signal set that contains the new signals that should be blocked or unblocked (depending on the value of <em>how</em>), or it points to the new signal mask if the value of <em>how</em> is <strong>SIG_SETMASK</strong>. If <em>set</em> is a <strong>NULL</strong> pointer, the set of blocked signals is not changed. If <em>set</em> is <strong>NULL</strong>, the value of <em>how</em> is ignored.</p>
<p>The signal set manipulation functions (<strong>sigemptyset</strong>(), <strong>sigfillset</strong>(), <strong>sigaddset</strong>(), and <strong>sigdelset</strong>()) must be used to establish the new signal set pointed to by <em>set</em>.</p>
<p>The <strong>pthread_sigmask</strong>() function determines the current signal set and returns this information in *<em>oset</em>. If <em>set</em> is <strong>NULL</strong>, <em>oset</em> returns the current set of signals being blocked. When <em>set</em> is not <strong>NULL</strong>, the set of signals pointed to by <em>oset</em> is the previous set.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>how</strong></dt>
<dd>(Input) The way in which the signal set is changed</dd>
<dt><strong>set</strong></dt>
<dd>(Input) A pointer to a set of signals to be used to change the currently blocked set. This value can be NULL</dd>
<dt><strong>oset</strong></dt>
<dd>(Output) A pointer to the space where the previous signal mask is stored. This value can be NULL</dd>
</dl>
<br>
<h3>Return Value</h3>
<dl>
<dt><strong>0</strong></dt>
<dd><strong>pthread_sigmask</strong>() was successful.</dd>
<dt><strong>value</strong></dt>
<dd><strong>pthread_sigmask</strong>() was not successful. <em>value</em> is set to indicate the error condition.</dd>
</dl>
<br>
<h3>Error Conditions</h3>
<p>If <strong>pthread_sigmask</strong>() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.</p>
<dl>
<dt><em>[EINVAL]</em></dt>
<dd><p>The value specified for the argument is not correct.</p></dd>
<dt><em>[ENOTSIGINIT]</em></dt>
<dd><p>The process is not enabled for signals.</p></dd>
</dl>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>pthread.h</strong>&gt; header file. See <a href="rzah4hed.htm">Header files for Pthread functions</a>.<br><br></li>
<li>
<a href="users_95.htm">pthread_kill()</a>--Send Signal to Thread<br><br></li>
<li>
<a href="users_97.htm">pthread_signal_to_cancel_np()</a>--Convert Signals to Cancel Requests</li>
</ul>
<br>
<h3>Example</h3>
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<pre>#define _MULTI_THREADED
#include &lt;pthread.h&gt;
#include &lt;stdio.h&gt;
#include &lt;signal.h&gt;
#include &quot;check.h&quot;
#define NUMTHREADS 3
void sighand(int signo);
void *threadfunc(void *parm)
{
pthread_t self = pthread_self();
pthread_id_np_t tid;
int rc;
pthread_getunique_np(&amp;self, &amp;tid);
printf(&quot;Thread 0x%.8x %.8x entered\n&quot;, tid);
errno = 0;
rc = sleep(30);
if (rc != 0 &amp;&amp; errno == EINTR) {
printf(&quot;Thread 0x%.8x %.8x got a signal delivered to it\n&quot;,
tid);
return NULL;
}
printf(&quot;Thread 0x%.8x %.8x did not get expected results! rc=%d, errno=%d\n&quot;,
tid, rc, errno);
return NULL;
}
void *threadmasked(void *parm)
{
pthread_t self = pthread_self();
pthread_id_np_t tid;
sigset_t mask;
int rc;
pthread_getunique_np(&amp;self, &amp;tid);
printf(&quot;Masked thread 0x%.8x %.8x entered\n&quot;, tid);
sigfillset(&amp;mask); /* Mask all allowed signals */
rc = pthread_sigmask(SIG_BLOCK, &amp;mask, NULL);
checkResults(&quot;pthread_sigmask()\n&quot;, rc);
errno = 0;
rc = sleep(15);
if (rc != 0) {
printf(&quot;Masked thread 0x%.8x %.8x did not get expected results! &quot;
&quot;rc=%d, errno=%d\n&quot;,
tid, rc, errno);
return NULL;
}
printf(&quot;Masked thread 0x%.8x %.8x completed masked work\n&quot;,
tid);
return NULL;
}
int main(int argc, char **argv)
{
int rc;
int i;
struct sigaction actions;
pthread_t threads[NUMTHREADS];
pthread_t maskedthreads[NUMTHREADS];
printf(&quot;Enter Testcase - %s\n&quot;, argv[0]);
printf(&quot;Set up the alarm handler for the process\n&quot;);
memset(&amp;actions, 0, sizeof(actions));
sigemptyset(&amp;actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = sighand;
rc = sigaction(SIGALRM,&amp;actions,NULL);
checkResults(&quot;sigaction\n&quot;, rc);
printf(&quot;Create masked and unmasked threads\n&quot;);
for(i=0; i&lt;NUMTHREADS; ++i) {
rc = pthread_create(&amp;threads[i], NULL, threadfunc, NULL);
checkResults(&quot;pthread_create()\n&quot;, rc);
rc = pthread_create(&amp;maskedthreads[i], NULL, threadmasked, NULL);
checkResults(&quot;pthread_create()\n&quot;, rc);
}
sleep(3);
printf(&quot;Send a signal to masked and unmasked threads\n&quot;);
for(i=0; i&lt;NUMTHREADS; ++i) {
rc = pthread_kill(threads[i], SIGALRM);
checkResults(&quot;pthread_kill()\n&quot;, rc);
rc = pthread_kill(maskedthreads[i], SIGALRM);
checkResults(&quot;pthread_kill()\n&quot;, rc);
}
printf(&quot;Wait for masked and unmasked threads to complete\n&quot;);
for(i=0; i&lt;NUMTHREADS; ++i) {
rc = pthread_join(threads[i], NULL);
checkResults(&quot;pthread_join()\n&quot;, rc);
rc = pthread_join(maskedthreads[i], NULL);
checkResults(&quot;pthread_join()\n&quot;, rc);
}
printf(&quot;Main completed\n&quot;);
return 0;
}
void sighand(int signo)
{
pthread_t self = pthread_self();
pthread_id_np_t tid;
pthread_getunique_np(&amp;self, &amp;tid);
printf(&quot;Thread 0x%.8x %.8x in signal handler\n&quot;,
tid);
return;
}</pre>
<p><strong>Output:</strong></p>
<pre>Thread 0x00000000 0000000d entered
Masked thread 0x00000000 0000000a entered
Thread 0x00000000 00000009 entered
Thread 0x00000000 0000000b entered
Masked thread 0x00000000 0000000e entered
Masked thread 0x00000000 0000000c entered
Send a signal to masked and unmasked threads
Wait for masked and unmasked threads to complete
Thread 0x00000000 00000009 in signal handler
Thread 0x00000000 00000009 got a signal delivered to it
Thread 0x00000000 0000000b in signal handler
Thread 0x00000000 0000000b got a signal delivered to it
Thread 0x00000000 0000000d in signal handler
Thread 0x00000000 0000000d got a signal delivered to it
Masked thread 0x00000000 0000000a completed masked work
Masked thread 0x00000000 0000000e completed masked work
Masked thread 0x00000000 0000000c completed masked work
Main completed</pre>
<hr>
API introduced: V4R3
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> |
<a href="rzah4mst.htm">Pthread APIs</a> |
<a href="aplist.htm">APIs by category</a></td>
</tr>
</table>
</center>
</body>
</html>