📄 semop.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>semop</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_008_375"> </a>NAME</h4><blockquote>semop - semaphore operations</blockquote><h4><a name = "tag_000_008_376"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="syssem.h.html">sys/sem.h</a>>int semop(int <i>semid</i>, struct sembuf *<i>sops</i>, size_t <i>nsops</i>);</code></pre></blockquote><h4><a name = "tag_000_008_377"> </a>DESCRIPTION</h4><blockquote>The<i>semop()</i>functionis used to perform atomically a user-defined arrayof semaphore operations on the set of semaphoresassociated with the semaphore identifier specified by theargument<i>semid</i>.<p>The argument<i>sops</i>is a pointer to a user-defined array of semaphore operation structures.The implementation will not modify elements of this array unlessthe application uses implementation-dependent extensions.<p>The argument<i>nsops</i>is the number of such structures in the array.<p>Each structure,<b>sembuf</b>,includes the following members:<p><table bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>Member Type</b><th align=center><b>Member Name</b><th align=center><b>Description</b><tr valign=top><td align=left>short<td align=left>sem_num<td align=left>semaphore number<tr valign=top><td align=left>short<td align=left>sem_op<td align=left>semaphore operation<tr valign=top><td align=left>short<td align=left>sem_flg<td align=left>operation flags</table><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>The variable<i>sem_op</i>specifies one of three semaphore operations:<ol><p><li>If<i>sem_op</i>is a negative integer and the calling processhas alter permission, one of the following will occur:<ul><p><li>If<i>semval</i>,see<i><a href="syssem.h.html"><sys/sem.h></a></i>,is greaterthan 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>is added to the calling process'<i>semadj</i>value for the specified semaphore.<p><li>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>will return immediately.<br><p><li>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>will increment the<i>semncnt</i>associated with the specified semaphore and suspend executionof the calling thread until one of the following conditions occurs:<ul><p><li>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 is decremented, theabsolute value of<i>sem_op</i>is subtracted from<i>semval</i>and, if (<i>sem_flg</i>&SEM_UNDO) is non-zero, the absolute value of<i>sem_op</i>is added to the calling process'<i>semadj</i>value for the specified semaphore.<br><p><li>The<i>semid</i>for which the calling thread is awaiting action is removed from the system.When this occurs,<i>errno</i>is set equal to [EIDRM] and -1 is returned.<br><p><li>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 is decremented,and the calling thread resumes execution in the mannerprescribed in<i><a href="sigaction.html">sigaction()</a></i>.<p></ul><p></ul><p><li>If<i>sem_op</i>is a positive integer and the calling process has alter permission,the value of<i>sem_op</i>is added to<i>semval</i>and, if (<i>sem_flg</i>&SEM_UNDO) is non-zero, the value of<i>sem_op</i>is subtracted from the calling process'<i>semadj</i>value for the specified semaphore.<p><li>If<i>sem_op</i>is 0 and the calling process has read permission,one of the following will occur:<ul><p><li>If<i>semval</i>is 0,<i>semop()</i>will return immediately.<p><li>If<i>semval</i>is non-zero and (<i>sem_flg</i>&IPC_NOWAIT) is non-zero,<i>semop()</i>will return immediately.<p><li>If<i>semval</i>is non-zero and (<i>sem_flg</i>&IPC_NOWAIT) is 0,<i>semop()</i>will increment the<i>semzcnt</i>associated with the specified semaphore and suspend executionof the calling thread until one of the following occurs:<ul><p><li>The value of<i>semval</i>becomes 0, at which time the value of<i>semzcnt</i>associated with the specified semaphore is decremented.<p><li>The<i>semid</i>for which the calling thread is awaiting action isremoved from the system.When this occurs,<i>errno</i>is set equal to [EIDRM] and -1 is returned.<p><li>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 is decremented,and the calling thread resumes execution in the mannerprescribed in<i><a href="sigaction.html">sigaction()</a></i>.<p></ul><p></ul><p></ol><p>Upon successful completion, the value of<i>sempid</i>for each semaphore specified in the array pointed to by<i>sops</i>is set equal to the process ID of the calling process.</blockquote><h4><a name = "tag_000_008_378"> </a>RETURN VALUE</h4><blockquote>Upon successful completion,<i>semop()</i>returns 0. Otherwise, it returns -1 and<i>errno</i>will be set to indicate the error.<br></blockquote><h4><a name = "tag_000_008_379"> </a>ERRORS</h4><blockquote>The<i>semop()</i>function will fail if:<dl compact><dt>[E2BIG]<dd>The value of<i>nsops</i>is greater than the system-imposed maximum.<dt>[EACCES]<dd>Operation permission is denied to the calling process, see<a href="ipc.html">IPC</a>.<dt>[EAGAIN]<dd>The operation would result in suspension of thecalling process but (<i>sem_flg</i>&IPC_NOWAIT) is non-zero.<dt>[EFBIG]<dd>The value of<i>sem_num</i>is less than 0 or greater than or equal to thenumber of semaphores in the set associated with<i>semid</i>.<dt>[EIDRM]<dd>The semaphore identifier<i>semid</i>is removed from the system.<dt>[EINTR]<dd>The<i>semop()</i>function was interrupted by a signal.<dt>[EINVAL]<dd>The value of<i>semid</i>is not a valid semaphore identifier,or the number of individual semaphores for whichthe calling process requests a SEM_UNDOwould exceed the system-imposed limit.<dt>[ENOSPC]<dd>The limit on the number of individual processes requesting a SEM_UNDOwould be exceeded.<dt>[ERANGE]<dd>An operation would cause a<i>semval</i>to overflow the system-imposed limit, or an operationwould cause a<i>semadj</i>value to overflow the system-imposed limit.</dl></blockquote><h4><a name = "tag_000_008_380"> </a>EXAMPLES</h4><blockquote>None.</blockquote><h4><a name = "tag_000_008_381"> </a>APPLICATION USAGE</h4><blockquote>The POSIX Realtime Extension defines alternative interfaces for interprocess communication.Application developers who need to use IPC should design theirapplications so that modules using the IPC routines described in<a href="ipc.html">IPC</a>can be easily modified to use the alternative interfaces.</blockquote><h4><a name = "tag_000_008_382"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_008_383"> </a>SEE ALSO</h4><blockquote><i><a href="exec.html">exec</a></i>,<i><a href="exit.html">exit()</a></i>,<i><a href="fork.html">fork()</a></i>,<i><a href="semctl.html">semctl()</a></i>,<i><a href="semget.html">semget()</a></i>,<i><a href="sem_close.html">sem_close()</a></i>,<i><a href="sem_destroy.html">sem_destroy()</a></i>,<i><a href="sem_getvalue.html">sem_getvalue()</a></i>,<i><a href="sem_init.html">sem_init()</a></i>,<i><a href="sem_open.html">sem_open()</a></i>,<i><a href="sem_post.html">sem_post()</a></i>,<i><a href="sem_unlink.html">sem_unlink()</a></i>,<i><a href="sem_wait.html">sem_wait()</a></i>,<i><a href="sysipc.h.html"><sys/ipc.h></a></i>,<i><a href="syssem.h.html"><sys/sem.h></a></i>,<i><a href="systypes.h.html"><sys/types.h></a></i>,<a href="ipc.html">IPC</a>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from Issue 2 of the SVID.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -