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

289 lines
8.3 KiB
HTML
Raw 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_signal_to_cancel_np()--Convert Signals to Cancel
Requests</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_signal_to_cancel_np()--Convert Signals to Cancel Requests</h2>
<div class="box" style="width: 70%;">
<br>
&nbsp;&nbsp;Syntax:
<pre>
#include &lt;pthread.h&gt;
int pthread_signal_to_cancel_np(sigset_t *set, pthread_t *thread);
</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: No<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>pthread_signal_to_cancel_np</strong>() function causes a
<strong>pthread_cancel</strong>() to be delivered to the target thread when the
first signal specified in set arrives.</p>
<p>All threads in the process should have the signals specified by <em>set</em>
blocked from the time of the call to
<strong>pthread_signal_to_cancel_np</strong>() until the time when the
<strong>pthread_cancel</strong>() is delivered to the target thread.</p>
<p>If <strong>pthread_signal_to_cancel_np</strong>() has been called, but a
signal has not yet been converted to a <strong>pthread_cancel</strong>(), a
subsequent call to <strong>pthread_signal_to_cancel_np</strong>() overrides the
first call.</p>
<p>The <strong>pthread_signal_to_cancel_np</strong>() function creates a
service thread (called the SignalToCancel thread) to perform the signal to
cancel conversion. This conversion occurs asynchronously to the thread that
called <strong>pthread_signal_to_cancel_np</strong>().</p>
<p>The SignalToCancel thread blocks all signals and performs a
<strong>sigwait</strong>() on the <em>set</em> of signals specified by set.
When <strong>sigwait</strong>() returns, indicating that one of the signals in
set was synchronously received, the SignalToCancel thread calls
<strong>pthread_cancel</strong>() using the <em>thread</em> specified as the
target.</p>
<p>Since the SignalToCancel thread processing occurs asynchronously, the caller
of <strong>pthread_signal_to_cancel_np</strong>() is not notified of errors
that may occur during the processing of the SignalToCancel thread. If the
target thread has terminated or the signals specified by set are not valid, the
caller of <strong>pthread_signal_to_cancel_np</strong>() is not notified.</p>
<p><strong>Note:</strong> This function is not portable.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>set</strong></dt>
<dd>(Input) The set of signals that will be converted to pthread_cancel()
requests</dd>
<dt><strong>thread</strong></dt>
<dd>(Input) The thread that will be canceled when a signal in set arrives</dd>
</dl>
<br>
<h3>Return Value</h3>
<dl>
<dt><strong>0</strong></dt>
<dd><strong>pthread_signal_to_cancel_np</strong>() was successful.</dd>
<dt><strong>value</strong></dt>
<dd><strong>pthread_signal_to_cancel_np</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_signal_to_cancel_np</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>
</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_39.htm">pthread_cancel()</a>--Cancel Thread<br><br></li>
<li><a href="users_95.htm">pthread_kill()</a>--Send Signal to Thread<br><br></li>
<li><a href="users_96.htm">pthread_sigmask()</a>--Set or Get Signal Mask</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 "check.h"
void sighand(int signo);
void cancellationCleanup(void *parm) { printf("Thread was canceled\n"); }
void *threadfunc(void *parm)
{
pthread_t self = pthread_self();
pthread_id_np_t tid;
int rc;
int i=5;
pthread_getunique_np(&amp;self, &amp;tid);
printf("Thread 0x%.8x %.8x entered\n", tid);
while (i--) {
printf("Thread 0x%.8x %.8x looping\n",
tid, rc, errno);
sleep(2);
pthread_testcancel();
}
printf("Thread 0x%.8x %.8x did not expect to get here\n",
tid);
return NULL;
}
int main(int argc, char **argv)
{
int rc;
int i;
pthread_t thread;
struct sigaction actions;
sigset_t mask;
void *status;
pthread_t self;
pthread_id_np_t tid;
printf("Enter Testcase - %s\n", argv[0]);
printf("Set up the alarm handler for the process\n");
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("sigaction\n", rc);
printf("Block all signals in the parent so they can be inherited\n");
sigfillset(&amp;mask); /* Mask all allowed signals */
rc = pthread_sigmask(SIG_BLOCK, &amp;mask, NULL);
checkResults("pthread_sigmask()\n", rc);
printf("Create thread that inherits blocking mask\n");
/* Thread will inherit blocking mask */
rc = pthread_create(&amp;thread, NULL, threadfunc, NULL);
checkResults("pthread_create()\n", rc);
/* Convert signals to cancels */
rc = pthread_signal_to_cancel_np(&amp;mask, &amp;thread);
checkResults("pthread_signal_to_cancel()\n", rc);
sleep(3);
self = pthread_self();
pthread_getunique_np(&amp;self, &amp;tid);
printf("Thread 0x%.8x %.8x sending a signal to the process\n", tid);
kill(getpid(), SIGALRM);
checkResults("kill()\n", rc);
printf("Wait for masked and unmasked threads to complete\n");
rc = pthread_join(thread, &amp;status);
checkResults("pthread_join()\n", rc);
if (status != PTHREAD_CANCELED) {
printf("Got an incorrect thread status\n");
return 1;
}
printf("The target thread was canceled\n");
printf("Main completed\n");
return 0;
}
void sighand(int signo)
{
pthread_t self = pthread_self();
pthread_id_np_t tid;
pthread_getunique_np(&amp;self, &amp;tid);
printf("Thread 0x%.8x %.8x in signal handler\n",
tid);
return;
}
</pre>
<p><strong>Output:</strong></p>
<pre>
Enter Testcase - QP0WTEST/TPSIG2C0
Set up the alarm handler for the process
Block all signals in the parent so they can be inherited
Create thread that inherits blocking mask
Thread 0x00000000 00000007 entered
Thread 0x00000000 00000007 looping
Thread 0x00000000 00000007 looping
Thread 0x00000000 00000006 sending a signal to the process
Wait for masked and unmasked threads to complete
The target thread was canceled
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>