⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 semop.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<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 InterprocessCommunication</i></a> .</dd><dt>[EAGAIN]</dt><dd>The operation would result in suspension of the calling process but (<i>sem_flg</i> &amp;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 callingprocess 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 containedin the <i>sb</i> array.</p><pre><tt>#include &lt;sys/sem.h&gt;...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, thengets a semaphore ID associated with that key using the <a href="../functions/semget.html"><i>semget</i>()</a> function (the firstcall also tests to make sure the semaphore exists). If the semaphore does not exist, the program creates it, as shown by the secondcall to <a href="../functions/semget.html"><i>semget</i>()</a>. In creating the semaphore for the queuing process, the programattempts 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 semaphorewhen the process exits, waiting until there are less than two processes running concurrently.</p><pre><tt>#include &lt;sys/types.h&gt;#include &lt;stdio.h&gt;#include &lt;sys/ipc.h&gt;#include &lt;sys/sem.h&gt;#include &lt;sys/stat.h&gt;#include &lt;errno.h&gt;#include &lt;unistd.h&gt;#include &lt;stdlib.h&gt;#include &lt;pwd.h&gt;#include &lt;fcntl.h&gt;#include &lt;limits.h&gt;...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, &amp;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, &amp;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 touse 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 alternativeinterfaces.</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> , <ahref="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&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/sys/ipc.h.html"><i>&lt;sys/ipc.h&gt;</i></a>, <a href="../basedefs/sys/sem.h.html"><i>&lt;sys/sem.h&gt;</i></a>, <ahref="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</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 USAGEsection.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; 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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -