]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/semop.2
This commit was generated by cvs2svn to compensate for changes in r52874,
[FreeBSD/FreeBSD.git] / lib / libc / sys / semop.2
1 .\"
2 .\" Copyright (c) 1995 David Hovemeyer <daveho@infocom.com>
3 .\"
4 .\" All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\"
15 .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD$
27 .\"
28 .Dd September 22, 1995
29 .Dt SEMOP 2
30 .Os FreeBSD
31 .Sh NAME
32 .Nm semop
33 .Nd atomic array of operations on a semaphore set
34 .Sh SYNOPSIS
35 .Fd #include <sys/types.h>
36 .Fd #include <sys/ipc.h>
37 .Fd #include <sys/sem.h>
38 .Ft int
39 .Fn semop "int semid" "struct sembuf array[]" "unsigned nops"
40 .Sh DESCRIPTION
41 .Fn Semop
42 atomically performs the array of operations indicated by
43 .Fa array
44 on the semaphore set indicated by
45 .Fa semid .
46 The length of
47 .Fa array
48 is indicated by
49 .Fa nops .
50 Each operation is encoded in a
51 .Fa "struct sembuf" ,
52 which is defined as follows:
53 .Bd -literal
54 .\"
55 .\" From <sys/sem.h>
56 .\"
57 struct sembuf {
58         u_short sem_num;        /* semaphore # */
59         short   sem_op;         /* semaphore operation */
60         short   sem_flg;        /* operation flags */
61 };
62 .Ed
63 .Pp
64 For each element in
65 .Fa array ,
66 .Fa sem_op
67 and
68 .Fa sem_flg
69 determine an operation to be performed on semaphore number
70 .Fa sem_num
71 in the set.  The values SEM_UNDO and IPC_NOWAIT may be
72 .Em OR Ns 'ed
73 into the
74 .Fa sem_flg
75 member in order to modify the behavior of the given operation.
76 .Pp
77 The operation performed depends as follows on the value of
78 .Fa sem_op :
79 .\"
80 .\" This section is based on the description of semop() in
81 .\" Stevens, _Advanced Programming in the UNIX Environment_.
82 .\"
83 .Bl -bullet
84 .It
85 When
86 .Fa sem_op
87 is positive, the semaphore's value is incremented by
88 .Fa sem_op Ns 's
89 value.  If SEM_UNDO is specified, the semaphore's adjust on exit
90 value is decremented by
91 .Fa sem_op Ns 's
92 value.  A positive value for
93 .Fa sem_op
94 generally corresponds to a process releasing a resource
95 associated with the semaphore.
96 .It
97 The behavior when
98 .Fa sem_op
99 is negative depends on the current value of the semaphore:
100 .Bl -bullet
101 .It
102 If the current value of the semaphore is greater than or equal to
103 the absolute value of
104 .Fa sem_op ,
105 then the value is decremented by the absolute value of
106 .Fa sem_op .
107 If SEM_UNDO is specified, the semaphore's adjust on exit
108 value is incremented by the absolute value of
109 .Fa sem_op .
110 .It
111 If the current value of the semaphore is less than
112 .Fa sem_op Ns 's
113 value, one of the following happens:
114 .\" XXX a *second* sublist?
115 .Bl -bullet
116 .It
117 If IPC_NOWAIT was specified, then
118 .Fn semop
119 returns immediately with a return value of EAGAIN.
120 .It
121 If some other process has removed the semaphore with the IPC_RMID
122 option of
123 .Fn semctl ,
124 then
125 .Fn semop
126 returns immediately with a return value of EINVAL.
127 .It
128 Otherwise, the calling process is put to sleep until the semaphore's
129 value is greater than or equal to the absolute value of
130 .Fa sem_op .
131 When this condition becomes true, the semaphore's value is decremented
132 by the absolute value of
133 .Fa sem_op ,
134 and the semaphore's adjust on exit value is incremented by the
135 absolute value of
136 .Fa sem_op .
137 .El
138 .Pp
139 A negative value for
140 .Fa sem_op
141 generally means that a process is waiting for a resource to become
142 available.
143 .El
144 .Pp
145 .It
146 When
147 .Fa sem_op
148 is zero, the process waits for the semaphore's value to become zero.
149 If it is already zero, the call to
150 .Fn semop
151 can return immediately.  Otherwise, the calling process is put to 
152 sleep until the semaphore's value becomes zero.
153 .El
154 .Pp
155 For each semaphore a process has in use, the kernel maintains an
156 `adjust on exit' value, as alluded to earlier.  When a process
157 exits, either voluntarily or involuntarily, the adjust on exit value
158 for each semaphore is added to the semaphore's value.  This can
159 be used to insure that a resource is released if a process terminates
160 unexpectedly.
161 .Sh RETURN VALUES
162 On success,
163 .Fn semop
164 returns 0; otherwise, -1 is returned and
165 .Va errno
166 is set to indicate the error.
167 .Sh ERRORS
168 .Fn Semop
169 will fail if:
170 .Bl -tag -width Er
171 .It Bq Er EINVAL
172 No semaphore set corresponds to
173 .Fa semid .
174 .It Bq Er EACCES
175 Permission denied due to mismatch between operation and mode of
176 semaphore set.
177 .It Bq Er EAGAIN
178 The semaphore's value was less than
179 .Fa sem_op ,
180 and IPC_NOWAIT was specified.
181 .It Bq Er E2BIG
182 Too many operations were specified.
183 .It Bq Er EFBIG
184 .\"
185 .\" I'd have thought this would be EINVAL, but the source says
186 .\" EFBIG.
187 .\"
188 .Fa sem_num
189 was not in the range of valid semaphores for the set.
190 .Sh SEE ALSO
191 .Xr semctl 2 ,
192 .Xr semget 2