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

312 lines
8.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>sem_wait_np()-Wait for Semaphore with Timeout</title>
<!-- Begin Header Records ========================================== -->
<!-- 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. -->
<!-- Change History: -->
<!-- YYMMDD USERID Change description -->
<!-- Direct1 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
<!-- Edited by Kersten Feb 02 -->
<!-- This file has undergone html cleanup on 05/01/02 by JET -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!--End Header Records -->
<!-- Java sync-link -->
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
</script>
<a name="Top_Of_Page"></a>
<h2>sem_wait_np()--Wait for Semaphore with Timeout</h2>
<div class="box" style="width: 60%;">
<br>
&nbsp;&nbsp;Syntax<br>
<pre>
#include &lt;semaphore.h&gt;
int sem_wait_np(sem_t * <em>sem</em>,
sem_wait_options_np_t * <em>options</em>);
</pre>
<br>
&nbsp;&nbsp;Service Program Name: QP0ZPSEM<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>sem_wait_np()</strong> function attempts to decrement by one the
value of the semaphore. The semaphore will be decremented by one when its value
is greater than zero. If the value of the semaphore is zero, then the current
thread will block until the semaphore's value becomes greater than zero or
until the timeout period specified on the options parameter has ended. If the
semaphore is not decremented before the timeout ends, <strong>
sem_wait_np()</strong> will return with an error, setting errno to
[ETIMEDOUT].</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>sem</strong></dt>
<dd>(Input) A pointer to an initialized unnamed semaphore or opened named
semaphore.<br>
<br>
</dd>
<dt><strong>options</strong></dt>
<dd>(Input) A pointer to a semaphore wait (<samp>sem_wait_options_np_t</samp>)
structure. The members of the sem_wait_options_np_t structure are as follows:
<table cellpadding="5">
<!-- cols="15 85" -->
<tr>
<td align="left" valign="top"><em>unsigned int reserved1[2]</em></td>
<td align="left" valign="top">A reserved field that must be set to zero.</td>
</tr>
<tr>
<td align="left" valign="top" nowrap><em>struct sem_timeout_t timeout</em></td>
<td align="left" valign="top">The time, in MI time, that <strong>
sem_wait_np()</strong> should wait for the semaphore. If the timeout is zero,
<strong>sem_wait_np()</strong> will return immediately with errno set to
[ETIMEDOUT] if the semaphore cannot be decremented. If a timeout value of
0xFFFFFFFF&nbsp;FFFFFFFF is specified, then sem_wait_np() will wait
indefinitely. The maximum timeout that may be specified is
281&nbsp;272&nbsp;976&nbsp;710&nbsp;655 (2 ** 48 -1) microseconds. Any value
larger than this, other than 0xFFFFFFFF&nbsp;FFFFFFFF, will cause <strong>
sem_wait_np()</strong> to wait for the maximum timeout
(281&nbsp;272&nbsp;976&nbsp;710&nbsp;655 microseconds). The Qp0zCvtToMITime()
may be used to convert a timeval structure to the corresponding MI time.</td>
</tr>
</table>
</dd>
</dl>
<br>
<h3>Authorities</h3>
<p>None</p>
<br>
<h3>Return Value</h3>
<table cellpadding="5">
<!-- cols="5 95" -->
<tr>
<td align="left" valign="top"><em>0</em></td>
<td align="left" valign="top"><strong>sem_wait_np()</strong> was
successful.</td>
</tr>
<tr>
<td align="left" valign="top"><em>-1</em></td>
<td align="left" valign="top"><strong>sem_wait_np()</strong> was not
successful. The <em>errno</em> variable is set to indicate the error.</td>
</tr>
</table>
<br>
<br>
<h3>Error Conditions</h3>
<p>If <strong>sem_wait_np()</strong> is not successful, <em>errno</em>
usually indicates one of the following errors. Under some conditions, <em>
errno</em> could indicate an error other than those listed here.</p>
<dl>
<dt><em>[ECANCEL]</em></dt>
<dd>
<p>Operation canceled.</p>
</dd>
<dt><em>[EDESTROYED]</em></dt>
<dd>
<p>The semaphore was destroyed.</p>
</dd>
<dt><em>[EINTR]</em></dt>
<dd>
<p>Interrupted function call.</p>
</dd>
<dt><em>[EINVAL]</em></dt>
<dd>
<p>The value specified for the argument is not correct.</p>
<p>A function was passed incorrect argument values, or an operation was
attempted on an object and the operation specified is not supported for that
type of object.</p>
<p>An argument value is not valid, out of range, or NULL.</p>
</dd>
<dt><em>[ETIMEDOUT]</em></dt>
<dd>
<p>A remote host did not respond within the timeout period.</p>
</dd>
</dl>
<br>
<h3>Error Messages</h3>
<p>None.</p>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>semaphore.h</strong>&gt; file (see <a href="unix13.htm">
Header Files for UNIX-Type Functions</a>)<br>
<br>
</li>
<li><a href="ipcsemcl.htm">sem_close()</a>--Close Named Semaphore<br>
<br>
</li>
<li><a href="ipcsemde.htm">sem_destroy()</a>--Destroy Unnamed Semaphore<br>
<br>
</li>
<li><a href="ipcsemge.htm">sem_getvalue()</a>--Get Semaphore Value<br>
<br>
</li>
<li><a href="ipcsemi.htm">sem_init()</a>--Initialize Unnamed Semaphore<br>
<br>
</li>
<li><a href="ipcsemin.htm">sem_init_np()</a>--Initialize Unnamed Semaphore with
Maximum Value<br>
<br>
</li>
<li><a href="ipcsemo.htm">sem_open()</a>--Open Named Semaphore<br>
<br>
</li>
<li><a href="ipcsemon.htm">sem_open_np()</a>--Open Named Semaphore with Maximum
Value<br>
<br>
</li>
<li><a href="ipcsemp.htm">sem_post()</a>--Post to Semaphore<br>
<br>
</li>
<li><a href="ipcsempn.htm">sem_post_np()</a>--Post Value to Semaphore<br>
<br>
</li>
<li><a href="ipcsemtr.htm">sem_trywait()</a>--Try to Decrement Semaphore<br>
<br>
</li>
<li><a href="ipcsemun.htm">sem_unlink()</a>--Unlink Named Semaphore<br>
<br>
</li>
<li><a href="ipcsemwn.htm">sem_wait_np()</a>--Wait for Semaphore with
Timeout</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>
<p>The following example creates a semaphore with an initial value of 1. The
value is decremented using sem_wait(). The program then attempts to decrement
the semaphore using sem_wait_np() with a timeout of 2 seconds. This will fail
with ETIMEDOUT because the semaphore's value is currently zero.</p>
<pre>
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;semaphore.h&gt;
#include &lt;time.h&gt;
#include &lt;qp0z1170.h&gt;
main() {
sem_t my_semaphore;
int value;
sem_wait_options_np_t options;
int rc;
struct timeval waittime;
time_t start_time;
time_t end_time;
sem_init(&amp;my_semaphore, 0, 1);
sem_getvalue(&amp;my_semaphore, &amp;value);
printf("The initial value of the semaphore is %d\n", value);
sem_wait(&amp;my_semaphore);
sem_getvalue(&amp;my_semaphore, &amp;value);
printf("The value of the semaphore after the wait is %d\n", value);
memset(&amp;options, 0, sizeof(options));
waittime.tv_sec = 2;
waittime.tv_usec = 0;
Qp0zCvtToMITime((unsigned char *) &amp;options.timeout,
<em>wait_time</em>,
QP0Z_CVTTIME_TO_OFFSET);
time(&amp;start_time);
rc = sem_wait_np(&amp;my_semaphore, &amp;options);
time(&amp;end_time);
if ((rc == -1) &amp;&amp; (errno == ETIMEDOUT)) {
printf("sem_wait_np timed out after %d seconds\n",
end_time - start_time);
}
}
</pre>
<br>
<h3>Output:</h3>
<pre>
The initial value of the semaphore is 1
The value of the semaphore after the wait is 0
sem_wait_np timed out after 2 seconds
</pre>
<br>
<hr>
API introduced: V4R4
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
</tr>
</table>
</center>
</body>
</html>