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

402 lines
12 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>semop()-Perform Semaphore Operations on Semaphore Set</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 -->
<!-- This file has undergone html/error condition 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>semop()--Perform Semaphore Operations on Semaphore Set</h2>
<div class="box" style="width: 70%;">
<br>
&nbsp;&nbsp;Syntax<br>
<pre>
#include &lt;sys/sem.h&gt;
int semop(int <em>semid</em>, struct sembuf <em>*sops</em>,
size_t <em>nsops</em>);
</pre>
<br>
&nbsp;&nbsp;Service Program Name: QP0ZCPA<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>semop()</strong> function performs operations on semaphores in a
semaphore set. These operations are supplied in a user-defined array of
operations.</p>
<p>Each semaphore operation specified by the <em>sops</em> array is
performed on the semaphore set specified by <em>semid</em>. The entire array
of operations is performed atomically; no other thread will operate on the
semaphore set until all of the operations are done or it is determined that
they cannot be done. If the entire set of operations cannot be performed, none
of the operations are done, and the thread waits until all of the operations
can be done.</p>
<p>The members of the <samp>sembuf</samp> structure are as follows:</p>
<table cellpadding="5">
<!-- cols="15 85" -->
<tr>
<td align="left" valign="top"><em>unsigned short sem_num</em> </td>
<td align="left" valign="top">The number of the semaphore in the semaphore set.</td>
</tr>
<tr>
<td align="left" valign="top"><em>short sem_op</em> </td>
<td align="left" valign="top">The operation to perform on the semaphore.</td>
</tr>
<tr>
<td align="left" valign="top"><em>short sem_flg</em> </td>
<td align="left" valign="top">The operation flags.</td>
</tr>
</table>
<p>The <strong>semop()</strong> function changes each semaphore specified by <em>
sem_num</em> according to the value of <em>sem_op</em> as follows:</p>
<br>
<ul>
<li>If <em>sem_op</em> is positive, <strong>semop()</strong> increments the
value of the semaphore and wakes up any threads waiting for the semaphore to
increase. This corresponds to releasing resources controlled by the
semaphore.<br>
<br>
</li>
<li>If <em>sem_op</em> is negative, <strong>semop()</strong> attempts to
decrement the value of the semaphore. If the result would be negative, it waits
for the semaphore value to increase. If the result would be positive, it
decrements the semaphore. If the result would be zero, it decrements the
semaphore and wakes up any threads waiting for the semaphore to be zero. This
corresponds to the allocation of resources.<br>
<br>
</li>
<li>If <em>sem_op</em> is zero, the thread waits for the semaphore's value to
be zero.</li>
</ul>
<p>If IPC_NOWAIT is set in <em>sem_flg</em> and the operation cannot be completed, <strong>
semop()</strong> returns with a return value of -1 and <em>errno</em> set to EAGAIN
instead of causing the thread to wait.</p>
<p>If SEM_UNDO is set in <em>sem_flg</em>, <strong>semop()</strong> causes IPC to reverse the
effect of this semaphore operation when the thread ends, effectively releasing
the resources or request for resources controlled by the semaphore. This value
is known as the semaphore adjustment value.</p>
<p>If the thread waits for the semaphore value to change, the calling thread suspends
processing until one of the following occurs:</p>
<ul>
<li>The semaphore reaches the specified value.</li>
<li>The semaphore set identifier <em>semid</em> is removed from the system.
When this occurs, the <strong>semop()</strong> function returns with a return value of -1 and
<em>errno</em> set to EIDRM.</li>
<li>A signal is delivered to the calling thread. When this occurs, the <strong>semop</strong>
function returns with a return value of -1 and <em>errno</em> set to EINTER.
</li>
</ul>
<p>The system maintains status information about a semaphore set which can
be retrieved with the <a href="ipcsemct.htm">semctl()</a> function. When a
semaphore operation is successfully completed, the system sets the members of the
<samp>semid_ds</samp> structure as follows:</p>
<ul>
<li><samp>shm_otime</samp> is set to the current time.</li>
</ul>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong><em>semid</em></strong></dt>
<dd>(Input) Semaphore set identifier, a positive integer. It is returned by the
<a href="ipcsemgt.htm">semget()</a> function and used to identify the semaphore set on
which to perform the control operation.<br>
<br>
</dd>
<dt><strong><em>sops</em></strong></dt>
<dd>(Input) Pointer to array of semaphore operation structures.<br>
<br>
</dd>
<dt><strong><em>nsops</em></strong></dt>
<dd>(Input) Number of <samp>sembuf</samp> structures in <em>sops</em> array.</dd>
</dl>
<br>
<h3>Authorities</h3>
<p><strong><a name="TBLASOP1">Authorization Required for semop()</a></strong></p>
<table border cellpadding="5">
<!-- cols="60 20 20" -->
<tr>
<th align="left" valign="bottom">Object Referred to</th>
<th align="left" valign="bottom">Authority Required</th>
<th align="left" valign="bottom">errno</th>
</tr>
<tr>
<td align="left" valign="top">Semaphore, <em>sem_op</em> is negative</td>
<td align="left" valign="top">Write</td>
<td align="left" valign="top">EACCES</td>
</tr>
<tr>
<td align="left" valign="top">Semaphore, <em>sem_op</em> is positive</td>
<td align="left" valign="top">Write</td>
<td align="left" valign="top">EACCES</td>
</tr>
<tr>
<td align="left" valign="top">Semaphore, <em>sem_op</em> is zero</td>
<td align="left" valign="top">Read</td>
<td align="left" valign="top">EACCES</td>
</tr>
</table>
<br>
<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>semop()</strong> was successful.</td>
</tr>
<tr>
<td align="left" valign="top" nowrap><em>-1</em></td>
<td align="left" valign="top"><strong>semop()</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>semop()</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>[EACCES]</em></dt>
<dd>
<p>Permission denied.</p>
<p>An attempt was made to access an object in a way forbidden by its object
access permissions.</p>
<p>The thread does not have access to the specified file, directory, component,
or path.</p>
<p>The <em>sem_op</em> value is negative or positive and the calling thread does
not have write permisstion to the semaphore set.</p>
<p>The <em>sem_op</em> value is zero and the calling thread does
not have read permisstion to the semaphore set.</p>
</dd>
<dt><em>[EAGAIN]</em></dt>
<dd>
<p>Operation would have caused the process to be suspended.</p>
<p>The operation would result in the calling thread waiting and
the IPC_NOWAIT flag is set in the <em>sem_flg</em> member.</p>
</dd>
<dt><em>[EDAMAGE]</em></dt>
<dd>
<p>A damaged object was encountered.</p>
<p>A referenced object is damaged. The object cannot be used.</p>
<p>The semaphore set has been damaged by a previous semaphore operation.</p>
</dd>
<dt><em>[EFAULT]</em></dt>
<dd>
<p>The address used for an argument is not correct.</p>
<p>In attempting to use an argument in a call, the system detected an address
that is not valid.</p>
<p>While attempting to access a parameter passed to this function, the system
detected an address that is not valid.</p>
</dd>
<dt><em>[EFBIG]</em></dt>
<dd>
<p>Object is too large.</p>
<p>The size of the object would exceed the system allowed maximum size.</p>
<p>The <em>sem_num</em> parameter is less than zero or greater than or equal to the
number of semaphores in the set associated with <em>semid</em>.</p>
</dd>
<dt><em>[EIDRM]</em></dt>
<dd>
<p>ID has been removed.</p>
<p>The semaphore identifier <em>semid</em> has been removed from the
system.</p>
</dd>
<dt><em>[EINTR]</em></dt>
<dd>
<p>Interrupted function call.</p>
<p>The <em>semop()</em> function was interrupted by a signal.</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>
<p>The <em>semid</em> parameter is not a valid semaphore identifier.</p>
</dd>
<dt><em>[ENOSPC]</em></dt>
<dd>
<p>No space available.</p>
<p>The requested operations required additional space on the device and there
is no space left. This could also be caused by exceeding the user profile
storage limit when creating or transferring ownership of an object.</p>
<p>Insufficient space remains to hold the intended file, directory, or
link.</p>
<p>The limit on the number of individual threads requesting a SEM_UNDO would be
exceeded.</p>
</dd>
<dt><em>[ERANGE]</em></dt>
<dd>
<p>A range error occurred.</p>
<p>The value of an argument is too small, or a result too large.</p>
<p>An operation would cause a <em>semval</em> to overflow the system-imposed
limit, or an operation would cause a semaphore adjustment value to overflow the
system-imposed limit.</p>
</dd>
<dt><em>[EUNKNOWN]</em></dt>
<dd>
<p>Unknown system state.</p>
<p>The operation failed because of an unknown system state. See any messages in
the job log and correct any errors that are indicated, then retry the
operation.</p>
</dd>
</dl>
<br>
<h3>Error Messages</h3>
<p>None.</p>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>sys/sem.h</strong>&gt; file (see <a href="unix13.htm">
Header Files for UNIX-Type Functions</a>)<br>
<br>
</li>
<li><a href="ipcsemgt.htm">semget()</a>--Get Semaphore Set with Key<br>
<br>
</li>
<li><a href="ipcsemct.htm">semctl()</a>--Perform Semaphore Control
Operations</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>For an example of using this function, see <a href="../apiref/apiexusmem.htm">
Using Semaphores and Shared Memory</a> in Examples: APIs.</p>
<br>
<hr>
API introduced: V3R6
<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>