433 lines
15 KiB
HTML
433 lines
15 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>semop</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="semop"></a> <a name="tag_03_640"></a><!-- semop -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_03_640_01"></a>NAME</h4>
|
|
|
|
<blockquote>semop - XSI semaphore operations</blockquote>
|
|
|
|
<h4><a name="tag_03_640_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt=
|
|
"[Option Start]" border="0"> #include <<a href="../basedefs/sys/sem.h.html">sys/sem.h</a>><br>
|
|
<br>
|
|
int semop(int</tt> <i>semid</i><tt>, struct sembuf *</tt><i>sops</i><tt>, size_t</tt> <i>nsops</i><tt>); <img src=
|
|
"../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div>
|
|
|
|
<tt><br>
|
|
</tt></blockquote>
|
|
|
|
<h4><a name="tag_03_640_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>semop</i>() function operates on XSI semaphores (see the Base Definitions volume of IEEE Std 1003.1-2001, <a
|
|
href="../basedefs/xbd_chap04.html#tag_04_15">Section 4.15, Semaphore</a>). It is unspecified whether this function interoperates
|
|
with the realtime interprocess communication facilities defined in <a href="xsh_chap02_08.html#tag_02_08"><i>Realtime</i></a> .</p>
|
|
|
|
<p>The <i>semop</i>() function shall perform atomically a user-defined array of semaphore operations on the set of semaphores
|
|
associated with the semaphore identifier specified by the argument <i>semid</i>.</p>
|
|
|
|
<p>The argument <i>sops</i> is a pointer to a user-defined array of semaphore operation structures. The implementation shall not
|
|
modify elements of this array unless the application uses implementation-defined extensions.</p>
|
|
|
|
<p>The argument <i>nsops</i> is the number of such structures in the array.</p>
|
|
|
|
<p>Each structure, <b>sembuf</b>, includes the following members:</p>
|
|
|
|
<center>
|
|
<table border="1" cellpadding="3" align="center">
|
|
<tr valign="top">
|
|
<th align="center">
|
|
<p class="tent"><b>Member Type</b></p>
|
|
</th>
|
|
<th align="center">
|
|
<p class="tent"><b>Member Name</b></p>
|
|
</th>
|
|
<th align="center">
|
|
<p class="tent"><b>Description</b></p>
|
|
</th>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><b>short</b></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent"><i>sem_num</i></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Semaphore number.</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><b>short</b></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent"><i>sem_op</i></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Semaphore operation.</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent"><b>short</b></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent"><i>sem_flg</i></p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Operation flags.</p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
|
|
<p>Each semaphore operation specified by <i>sem_op</i> is performed on the corresponding semaphore specified by <i>semid</i> and
|
|
<i>sem_num</i>.</p>
|
|
|
|
<p>The variable <i>sem_op</i> specifies one of three semaphore operations:</p>
|
|
|
|
<ol>
|
|
<li>
|
|
<p>If <i>sem_op</i> is a negative integer and the calling process has alter permission, one of the following shall occur:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>If <i>semval</i>(see <a href="../basedefs/sys/sem.h.html"><i><sys/sem.h></i></a>) is greater than or equal to the absolute
|
|
value of <i>sem_op</i>, the absolute value of <i>sem_op</i> is subtracted from <i>semval</i>. Also, if (<i>sem_flg</i>
|
|
&SEM_UNDO) is non-zero, the absolute value of <i>sem_op</i> shall be added to the calling process' <i>semadj</i> value for the
|
|
specified semaphore.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If <i>semval</i> is less than the absolute value of <i>sem_op</i> and (<i>sem_flg</i> &IPC_NOWAIT) is non-zero,
|
|
<i>semop</i>() shall return immediately.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If <i>semval</i> is less than the absolute value of <i>sem_op</i> and (<i>sem_flg</i> &IPC_NOWAIT) is 0, <i>semop</i>()
|
|
shall increment the <i>semncnt</i> associated with the specified semaphore and suspend execution of the calling thread until one of
|
|
the following conditions occurs:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>The value of <i>semval</i> becomes greater than or equal to the absolute value of <i>sem_op</i>. When this occurs, the value of
|
|
<i>semncnt</i> associated with the specified semaphore shall be decremented, the absolute value of <i>sem_op</i> shall be
|
|
subtracted from <i>semval</i> and, if (<i>sem_flg</i> &SEM_UNDO) is non-zero, the absolute value of <i>sem_op</i> shall be
|
|
added to the calling process' <i>semadj</i> value for the specified semaphore.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The <i>semid</i> for which the calling thread is awaiting action is removed from the system. When this occurs, <i>errno</i>
|
|
shall be set equal to [EIDRM] and -1 shall be returned.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The calling thread receives a signal that is to be caught. When this occurs, the value of <i>semncnt</i> associated with the
|
|
specified semaphore shall be decremented, and the calling thread shall resume execution in the manner prescribed in <a href=
|
|
"sigaction.html"><i>sigaction</i>()</a> .</p>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If <i>sem_op</i> is a positive integer and the calling process has alter permission, the value of <i>sem_op</i> shall be added
|
|
to <i>semval</i> and, if (<i>sem_flg</i> &SEM_UNDO) is non-zero, the value of <i>sem_op</i> shall be subtracted from the
|
|
calling process' <i>semadj</i> value for the specified semaphore.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If <i>sem_op</i> is 0 and the calling process has read permission, one of the following shall occur:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>If <i>semval</i> is 0, <i>semop</i>() shall return immediately.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If <i>semval</i> is non-zero and (<i>sem_flg</i> &IPC_NOWAIT) is non-zero, <i>semop</i>() shall return immediately.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If <i>semval</i> is non-zero and (<i>sem_flg</i> &IPC_NOWAIT) is 0, <i>semop</i>() shall increment the <i>semzcnt</i>
|
|
associated with the specified semaphore and suspend execution of the calling thread until one of the following occurs:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>The value of <i>semval</i> becomes 0, at which time the value of <i>semzcnt</i> associated with the specified semaphore shall be
|
|
decremented.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The <i>semid</i> for which the calling thread is awaiting action is removed from the system. When this occurs, <i>errno</i>
|
|
shall be set equal to [EIDRM] and -1 shall be returned.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The calling thread receives a signal that is to be caught. When this occurs, the value of <i>semzcnt</i> associated with the
|
|
specified semaphore shall be decremented, and the calling thread shall resume execution in the manner prescribed in <a href=
|
|
"sigaction.html"><i>sigaction</i>()</a> .</p>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ol>
|
|
|
|
<p>Upon successful completion, the value of <i>sempid</i> for each semaphore specified in the array pointed to by <i>sops</i> shall
|
|
be set equal to the process ID of the calling process.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_640_04"></a>RETURN VALUE</h4>
|
|
|
|
<blockquote>
|
|
<p>Upon successful completion, <i>semop</i>() shall return 0; otherwise, it shall return -1 and set <i>errno</i> to indicate the
|
|
error.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_640_05"></a>ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>semop</i>() function shall fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[E2BIG]</dt>
|
|
|
|
<dd>The value of <i>nsops</i> is greater than the system-imposed maximum.</dd>
|
|
|
|
<dt>[EACCES]</dt>
|
|
|
|
<dd>Operation permission is denied to the calling process; see <a href="xsh_chap02_07.html#tag_02_07"><i>XSI Interprocess
|
|
Communication</i></a> .</dd>
|
|
|
|
<dt>[EAGAIN]</dt>
|
|
|
|
<dd>The operation would result in suspension of the calling process but (<i>sem_flg</i> &IPC_NOWAIT) is non-zero.</dd>
|
|
|
|
<dt>[EFBIG]</dt>
|
|
|
|
<dd>The value of <i>sem_num</i> is less than 0 or greater than or equal to the number of semaphores in the set associated with
|
|
<i>semid</i>.</dd>
|
|
|
|
<dt>[EIDRM]</dt>
|
|
|
|
<dd>The semaphore identifier <i>semid</i> is removed from the system.</dd>
|
|
|
|
<dt>[EINTR]</dt>
|
|
|
|
<dd>The <i>semop</i>() function was interrupted by a signal.</dd>
|
|
|
|
<dt>[EINVAL]</dt>
|
|
|
|
<dd>The value of <i>semid</i> is not a valid semaphore identifier, or the number of individual semaphores for which the calling
|
|
process requests a SEM_UNDO would exceed the system-imposed limit.</dd>
|
|
|
|
<dt>[ENOSPC]</dt>
|
|
|
|
<dd>The limit on the number of individual processes requesting a SEM_UNDO would be exceeded.</dd>
|
|
|
|
<dt>[ERANGE]</dt>
|
|
|
|
<dd>An operation would cause a <i>semval</i> to overflow the system-imposed limit, or an operation would cause a <i>semadj</i>
|
|
value to overflow the system-imposed limit.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_03_640_06"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<h5><a name="tag_03_640_06_01"></a>Setting Values in Semaphores</h5>
|
|
|
|
<p>The following example sets the values of the two semaphores associated with the <i>semid</i> identifier to the values contained
|
|
in the <i>sb</i> array.</p>
|
|
|
|
<pre>
|
|
<tt>#include <sys/sem.h>
|
|
...
|
|
int semid;
|
|
struct sembuf sb[2];
|
|
int nsops = 2;
|
|
int result;
|
|
<br>
|
|
/* Adjust value of semaphore in the semaphore array semid. */
|
|
sb[0].sem_num = 0;
|
|
sb[0].sem_op = -1;
|
|
sb[0].sem_flg = SEM_UNDO | IPC_NOWAIT;
|
|
sb[1].sem_num = 1;
|
|
sb[1].sem_op = 1;
|
|
sb[1].sem_flg = 0;
|
|
<br>
|
|
result = semop(semid, sb, nsops);
|
|
</tt>
|
|
</pre>
|
|
|
|
<h5><a name="tag_03_640_06_02"></a>Creating a Semaphore Identifier</h5>
|
|
|
|
<p>The following example gets a unique semaphore key using the <a href="../functions/ftok.html"><i>ftok</i>()</a> function, then
|
|
gets a semaphore ID associated with that key using the <a href="../functions/semget.html"><i>semget</i>()</a> function (the first
|
|
call also tests to make sure the semaphore exists). If the semaphore does not exist, the program creates it, as shown by the second
|
|
call to <a href="../functions/semget.html"><i>semget</i>()</a>. In creating the semaphore for the queuing process, the program
|
|
attempts to create one semaphore with read/write permission for all. It also uses the IPC_EXCL flag, which forces <a href=
|
|
"../functions/semget.html"><i>semget</i>()</a> to fail if the semaphore already exists.</p>
|
|
|
|
<p>After creating the semaphore, the program uses a call to <i>semop</i>() to initialize it to the values in the <i>sbuf</i> array.
|
|
The number of processes that can execute concurrently without queuing is initially set to 2. The final call to <a href=
|
|
"../functions/semget.html"><i>semget</i>()</a> creates a semaphore identifier that can be used later in the program.</p>
|
|
|
|
<p>The final call to <i>semop</i>() acquires the semaphore and waits until it is free; the SEM_UNDO option releases the semaphore
|
|
when the process exits, waiting until there are less than two processes running concurrently.</p>
|
|
|
|
<pre>
|
|
<tt>#include <sys/types.h>
|
|
#include <stdio.h>
|
|
#include <sys/ipc.h>
|
|
#include <sys/sem.h>
|
|
#include <sys/stat.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <pwd.h>
|
|
#include <fcntl.h>
|
|
#include <limits.h>
|
|
...
|
|
key_t semkey;
|
|
int semid, pfd, fv;
|
|
struct sembuf sbuf;
|
|
char *lgn;
|
|
char filename[PATH_MAX+1];
|
|
struct stat outstat;
|
|
struct passwd *pw;
|
|
...
|
|
/* Get unique key for semaphore. */
|
|
if ((semkey = ftok("/tmp", 'a')) == (key_t) -1) {
|
|
perror("IPC error: ftok"); exit(1);
|
|
}
|
|
<br>
|
|
/* Get semaphore ID associated with this key. */
|
|
if ((semid = semget(semkey, 0, 0)) == -1) {
|
|
<br>
|
|
/* Semaphore does not exist - Create. */
|
|
if ((semid = semget(semkey, 1, IPC_CREAT | IPC_EXCL | S_IRUSR |
|
|
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) != -1)
|
|
{
|
|
/* Initialize the semaphore. */
|
|
sbuf.sem_num = 0;
|
|
sbuf.sem_op = 2; /* This is the number of runs without queuing. */
|
|
sbuf.sem_flg = 0;
|
|
if (semop(semid, &sbuf, 1) == -1) {
|
|
perror("IPC error: semop"); exit(1);
|
|
}
|
|
}
|
|
else if (errno == EEXIST) {
|
|
if ((semid = semget(semkey, 0, 0)) == -1) {
|
|
perror("IPC error 1: semget"); exit(1);
|
|
}
|
|
}
|
|
else {
|
|
perror("IPC error 2: semget"); exit(1);
|
|
}
|
|
}
|
|
...
|
|
sbuf.sem_num = 0;
|
|
sbuf.sem_op = -1;
|
|
sbuf.sem_flg = SEM_UNDO;
|
|
if (semop(semid, &sbuf, 1) == -1) {
|
|
perror("IPC Error: semop"); exit(1);
|
|
}
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_640_07"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>The POSIX Realtime Extension defines alternative interfaces for interprocess communication. Application developers who need to
|
|
use IPC should design their applications so that modules using the IPC routines described in <a href=
|
|
"xsh_chap02_07.html#tag_02_07"><i>XSI Interprocess Communication</i></a> can be easily modified to use the alternative
|
|
interfaces.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_640_08"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_640_09"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_640_10"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="xsh_chap02_07.html#tag_02_07"><i>XSI Interprocess Communication</i></a> , <a href=
|
|
"xsh_chap02_08.html#tag_02_08"><i>Realtime</i></a> , <a href="exec.html"><i><a href="../functions/exec.html">exec</a></i>()</a> ,
|
|
<a href="exit.html"><i>exit</i>()</a> , <a href="fork.html"><i>fork</i>()</a> , <a href="semctl.html"><i>semctl</i>()</a> , <a
|
|
href="semget.html"><i>semget</i>()</a> , <a href="sem_close.html"><i>sem_close</i>()</a> , <a href=
|
|
"sem_destroy.html"><i>sem_destroy</i>()</a> , <a href="sem_getvalue.html"><i>sem_getvalue</i>()</a> , <a href=
|
|
"sem_init.html"><i>sem_init</i>()</a> , <a href="sem_open.html"><i>sem_open</i>()</a> , <a href=
|
|
"sem_post.html"><i>sem_post</i>()</a> , <a href="sem_unlink.html"><i>sem_unlink</i>()</a> , <a href=
|
|
"sem_wait.html"><i>sem_wait</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href=
|
|
"../basedefs/sys/ipc.h.html"><i><sys/ipc.h></i></a>, <a href="../basedefs/sys/sem.h.html"><i><sys/sem.h></i></a>, <a
|
|
href="../basedefs/sys/types.h.html"><i><sys/types.h></i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_640_11"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 2. Derived from Issue 2 of the SVID.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_640_12"></a>Issue 5</h4>
|
|
|
|
<blockquote>
|
|
<p>The note about use of POSIX Realtime Extension IPC routines has been moved from FUTURE DIRECTIONS to a new APPLICATION USAGE
|
|
section.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|