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

230 lines
7.2 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_kill()--Send Signal to Thread</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_kill()--Send Signal to Thread</h2>
<div class="box" style="width: 60%;">
<br>
&nbsp;&nbsp;Syntax:
<pre> #include &lt;pthread.h&gt;
#include &lt;signal.h&gt;
int pthread_kill(pthread_t thread, int sig); </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_kill</strong>() function requests that the signal <em>sig</em> be delivered to the specified <em>thread</em>. The signal to be sent is specified by <em>sig</em> and is either zero or one of the signals from the list of defined signals in the <strong>&lt;signal.h></strong> header file. If <em>sig</em> is zero, error checking is performed, but no signal is sent to the target <em>thread</em>.</p>
<p>A thread can use <strong>pthread_kill</strong>() to send a signal to itself. If the signal is not blocked or ignored, at least one pending unblocked signal is delivered to the sender before <strong>pthread_kill</strong>() returns. If there are no other pending unblocked signals, the delivered signal is <em>sig</em>.</p>
<p>The <strong>pthread_kill</strong>() API in no way changes the effect or scope of a signal. Even though a signal can be sent to a specific thread using the <strong>pthread_kill</strong>() API, the behavior that occurs when the signal is delivered is unchanged.</p>
<p>For example, sending a <strong>SIGKILL</strong> signal to a thread using <strong>pthread_kill</strong>() ends the entire process, not simply the target thread. <strong>SIGKILL</strong> is defined to end the entire process, regardless of the thread it is delivered to, or how it is sent.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>thread</strong></dt>
<dd>(Input) Pthread handle of the target thread</dd>
<dt><strong>sig</strong></dt>
<dd>(Input) The signal number to be delivered or zero to validate the pthread_t</dd>
</dl>
<br>
<h3>Return Value</h3>
<dl>
<dt><strong>0</strong></dt>
<dd><strong>pthread_kill</strong>() was successful.</dd>
<dt><strong>value</strong></dt>
<dd><strong>pthread_kill</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_kill</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>[ESRCH]</em></dt>
<dd><p>No thread could be found that matched the thread ID specified.</p></dd>
<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>> header file. See <a href="rzah4hed.htm">Header files for Pthread functions</a>.<br><br></li>
<li><a href="users_96.htm">pthread_sigmask()</a>--Set or Get Signal Mask<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;
}
int main(int argc, char **argv)
{
int rc;
int i;
struct sigaction actions;
pthread_t threads[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);
for(i=0; i&lt;NUMTHREADS; ++i) {
rc = pthread_create(&amp;threads[i], NULL, threadfunc, NULL);
checkResults(&quot;pthread_create()\n&quot;, rc);
}
sleep(3);
for(i=0; i&lt;NUMTHREADS; ++i) {
rc = pthread_kill(threads[i], SIGALRM);
checkResults(&quot;pthread_kill()\n&quot;, rc);
}
for(i=0; i&lt;NUMTHREADS; ++i) {
rc = pthread_join(threads[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>Enter Testcase - QP0WTEST/TPKILL0
Set up the alarm handler for the process
Thread 0x00000000 0000000c entered
Thread 0x00000000 0000000d entered
Thread 0x00000000 0000000e entered
Thread 0x00000000 0000000c in signal handler
Thread 0x00000000 0000000c got a signal delivered to it
Thread 0x00000000 0000000d in signal handler
Thread 0x00000000 0000000d got a signal delivered to it
Thread 0x00000000 0000000e in signal handler
Thread 0x00000000 0000000e got a signal delivered to it
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>