1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Modified Tue Oct 22 17:55:06 1996 by Eric S. Raymond <esr@thyrsus.com>
.TH SEMOP 2 "November 1, 1993" "Linux 0.99.13" "Linux Programmer's Manual"
.SH NAME
semop \- semaphore operations
.SH SYNOPSIS
.nf
.B
# include <sys/types.h>
.B
# include <sys/ipc.h>
.B
# include <sys/sem.h>
.fi
.sp
.BI "int semop ( int " semid ,
.BI "struct sembuf *" sops ,
.BI "unsigned " nsops " )"
.SH DESCRIPTION
The function performs operations on selected members of the semaphore set indicated by
.IR semid .
Each of the
.I nsops
elements in the array pointed to by
.I sops
specify an operation to be performed on a semaphore by a
.B "struct sembuf"
including the following members:
.sp
.B
.br
.B
short sem_num;
/* semaphore number: 0 = first */
.br
.B
short sem_op;
/* semaphore operation */
.br
.B
short sem_flg;
/* operation flags */
.sp
Flags recognized in
.B sem_flg
are
.B IPC_NOWAIT
and
.BR SEM_UNDO .
If an operation asserts
.BR SEM_UNDO ,
it will be undone when the process exits.
.PP
The system call semantic assures that the operations will be
performed if and only if all of them will succeed.
Each operation is performed on the
.BR sem_num \-th
semaphore of the semaphore set \- where the first semaphore of the set
is semaphore
.B 0
\- and is one among the following three.
.PP
If
.B sem_op
is a positive integer, the operation adds this value to
.BR semval .
Furthermore, if
.B SEM_UNDO
is asserted for this operation, the system updates the process undo count
for this semaphore.
The operation always goes through, so no process sleeping can happen.
The calling process must have alter permissions on the semaphore set.
.PP
If
.B sem_op
is zero, the process must have read access permissions on the semaphore
set.
If
.B semval
is zero, the operation goes through.
Otherwise, if
.B IPC_NOWAIT
is asserted in
.BR sem_flg ,
the system call fails (undoing all previous actions
performed) with
.B errno
set to
.BR EAGAIN .
Otherwise
.B semzcnt
is incremented by one and the process sleeps until
one of the following occurs:
.IP
.RS
.IP \(bu
.B semval
becomes 0, at which time the value of
.B semzcnt
is decremented.
.IP \(bu
The semaphore set
is removed: the system call fails with
.B errno
set to
.BR EIDRM .
.IP \(bu
The calling process receives a signal that has to be caught:
the value of
.B semzcnt
is decremented and the system call fails with
.B errno
set to
.BR EINTR .
.RE
.PP
If
.B sem_op
is less than zero, the process must have alter permissions on the
semaphore set.
If
.B semval
is greater than or equal to the absolute value of
.BR sem_op ,
the absolute value of
.B sem_op
is subtracted by
.BR semval .
Furthermore, if
.B SEM_UNDO
is asserted for this operation, the system updates the process undo count
for this semaphore.
Then the operation goes through.
Otherwise, if
.B IPC_NOWAIT
is asserted in
.BR sem_flg ,
the system call fails (undoing all previous actions
performed) with
.B errno
set to
.BR EAGAIN .
Otherwise
.B semncnt
is incremented by one and the process sleeps until
one of the following occurs:
.IP
.RS
.IP \(bu
.B semval
becomes greater or equal to the absolute value of
.BR sem_op ,
at which time the value of
.B semncnt
is decremented, the absolute value of
.B sem_op
is subtracted from
.B semval
and, if
.B SEM_UNDO
is asserted for this operation, the system updates the process undo count
for this semaphore.
.IP \(bu
The semaphore set is removed from the system: the system call fails with
.B errno
set to
.BR EIDRM .
.IP \(bu
The calling process receives a signal that has to be caught:
the value of
.B semncnt
is decremented and the system call fails with
.B errno
set to
.BR EINTR .
.RE
.PP
In case of success, the
.B sempid
member of the structure
.B sem
for each semaphore specified in the array pointed to by
.I sops
is set to the process\-ID of the calling process.
Furthermore both
.B sem_otime
and
.B sem_ctime
are set to the current time.
.SH "RETURN VALUE"
If successful the system call returns
.BR 0 ,
otherwise it returns
.B \-1
with
.B errno
indicating the error.
.SH ERRORS
For a failing return,
.B errno
will be set to one among the following values:
.TP 11
.B E2BIG
The argument
.I nsops
is greater than
.BR SEMOPM ,
the maximum number of operations allowed per system
call.
.TP
.B EACCES
The calling process has no access permissions on the
semaphore set as required by one of the specified operations.
.TP
.B EAGAIN
An operation could not go through and
.BR IPC_NOWAIT
was asserted in its
.IB sem_flg .
.TP
.B EFAULT
The address pointed to by
.I sops
isn't accessible.
.TP
.B EFBIG
For some operation the value of
.B sem_num
is less than 0 or greater than or equal to the number
of semaphores in the set.
.TP
.B EIDRM
The semaphore set was removed.
.TP
.B EINTR
Sleeping on a wait queue, the process received a signal
that had to be caught.
.TP
.B EINVAL
The semaphore set doesn't exist, or
.I semid
is less than zero, or
.I nsops
has a non-positive value.
.TP
.B ENOMEM
The
.B sem_flg
of some operation asserted
.B SEM_UNDO
and the system has not enough memory to allocate the undo
structure.
.TP
.B ERANGE
For some operation
.B semop+semval
is greater than
.BR SEMVMX ,
the implementation dependent maximum value for
.BR semval .
.SH NOTES
The
.B sem_undo
structures of a process aren't inherited by its child
on execution of a
.BR fork (2)
system call.
They are instead inherited by the substituting process resulting
by the execution of the
.BR execve (2)
system call.
.PP
The followings are limits on semaphore set resources affecting a
.B semop
call:
.TP 11
.B SEMOPM
Maximum number of operations allowed for one
.B semop
call: policy dependent.
.TP
.B SEMVMX
Maximum allowable value for
.BR semval :
implementation dependent (32767).
.PP
The implementation has no intrinsic limits for
the adjust on exit maximum value
.RB ( SEMAEM ),
the system wide maximum number of undo structures
.RB ( SEMMNU )
and the per process maximum number of undo entries system parameters.
.SH BUGS
The system maintains a per process
.B sem_undo
structure for each semaphore altered by the process with undo requests.
Those structures are free at process exit.
One major cause for unhappiness with the undo mechanism is that it
does not fit in with the notion of having an atomic set of operations
an array of semaphores.
The undo requests for an array and each semaphore therein may have been
accumulated over many
.B semopt
calls.
Should the process sleep when
exiting, or should all undo operations be applied with the
.B IPC_NOWAIT
flag in effect?
Currently those undo operations which go through immediately are applied,
and those that require a wait are ignored silently.
Thus harmless undo usage is guaranteed with private semaphores only.
.SH "CONFORMING TO"
SVr4, SVID. SVr4 documents additional error conditions EINVAL, EFBIG,
ENOSPC.
.SH "SEE ALSO"
.BR ipc (5),
.BR semctl (2),
.BR semget (2)
|