1995-10-03 19:17:21 +00:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 1995 David Hovemeyer <daveho@infocom.com>
|
|
|
|
.\"
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
1999-08-28 00:22:10 +00:00
|
|
|
.\" $FreeBSD$
|
1995-10-03 19:17:21 +00:00
|
|
|
.\"
|
|
|
|
.Dd September 22, 1995
|
|
|
|
.Dt SEMOP 2
|
2001-07-10 13:41:46 +00:00
|
|
|
.Os
|
1995-10-03 19:17:21 +00:00
|
|
|
.Sh NAME
|
|
|
|
.Nm semop
|
|
|
|
.Nd atomic array of operations on a semaphore set
|
2000-04-21 09:42:15 +00:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
1995-10-03 19:17:21 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/types.h
|
|
|
|
.In sys/ipc.h
|
|
|
|
.In sys/sem.h
|
1995-10-03 19:17:21 +00:00
|
|
|
.Ft int
|
2003-01-25 21:27:37 +00:00
|
|
|
.Fn semop "int semid" "struct sembuf *array" "size_t nops"
|
1995-10-03 19:17:21 +00:00
|
|
|
.Sh DESCRIPTION
|
2001-11-21 15:41:49 +00:00
|
|
|
The
|
|
|
|
.Fn semop
|
2002-12-18 09:22:32 +00:00
|
|
|
system call
|
1995-10-03 19:17:21 +00:00
|
|
|
atomically performs the array of operations indicated by
|
|
|
|
.Fa array
|
|
|
|
on the semaphore set indicated by
|
|
|
|
.Fa semid .
|
|
|
|
The length of
|
|
|
|
.Fa array
|
|
|
|
is indicated by
|
|
|
|
.Fa nops .
|
|
|
|
Each operation is encoded in a
|
2001-11-21 15:41:49 +00:00
|
|
|
.Vt "struct sembuf" ,
|
1995-10-03 19:17:21 +00:00
|
|
|
which is defined as follows:
|
|
|
|
.\"
|
|
|
|
.\" From <sys/sem.h>
|
|
|
|
.\"
|
2005-01-15 12:28:01 +00:00
|
|
|
.Bd -literal
|
1995-10-03 19:17:21 +00:00
|
|
|
struct sembuf {
|
|
|
|
u_short sem_num; /* semaphore # */
|
|
|
|
short sem_op; /* semaphore operation */
|
|
|
|
short sem_flg; /* operation flags */
|
|
|
|
};
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
For each element in
|
|
|
|
.Fa array ,
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op
|
1995-10-03 19:17:21 +00:00
|
|
|
and
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_flg
|
1995-10-03 19:17:21 +00:00
|
|
|
determine an operation to be performed on semaphore number
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_num
|
|
|
|
in the set.
|
|
|
|
The values
|
2001-10-11 08:15:14 +00:00
|
|
|
.Dv SEM_UNDO
|
|
|
|
and
|
|
|
|
.Dv IPC_NOWAIT
|
|
|
|
may be
|
1995-10-03 19:17:21 +00:00
|
|
|
.Em OR Ns 'ed
|
|
|
|
into the
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_flg
|
1995-10-03 19:17:21 +00:00
|
|
|
member in order to modify the behavior of the given operation.
|
|
|
|
.Pp
|
|
|
|
The operation performed depends as follows on the value of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op :
|
1995-10-03 19:17:21 +00:00
|
|
|
.\"
|
|
|
|
.\" This section is based on the description of semop() in
|
2001-10-11 08:15:14 +00:00
|
|
|
.\" Stevens, _Advanced Programming in the UNIX Environment_,
|
|
|
|
.\" and the semop(2) description in The Open Group Unix2 specification.
|
1995-10-03 19:17:21 +00:00
|
|
|
.\"
|
|
|
|
.Bl -bullet
|
|
|
|
.It
|
|
|
|
When
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op
|
2001-10-11 08:15:14 +00:00
|
|
|
is positive and the process has alter permission,
|
|
|
|
the semaphore's value is incremented by
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op Ns 's
|
|
|
|
value.
|
|
|
|
If
|
2001-10-11 08:15:14 +00:00
|
|
|
.Dv SEM_UNDO
|
|
|
|
is specified, the semaphore's adjust on exit value is decremented by
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op Ns 's
|
|
|
|
value.
|
|
|
|
A positive value for
|
|
|
|
.Va sem_op
|
1995-10-03 19:17:21 +00:00
|
|
|
generally corresponds to a process releasing a resource
|
|
|
|
associated with the semaphore.
|
|
|
|
.It
|
|
|
|
The behavior when
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op
|
2001-10-11 08:15:14 +00:00
|
|
|
is negative and the process has alter permission,
|
|
|
|
depends on the current value of the semaphore:
|
1995-10-03 19:17:21 +00:00
|
|
|
.Bl -bullet
|
|
|
|
.It
|
|
|
|
If the current value of the semaphore is greater than or equal to
|
|
|
|
the absolute value of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op ,
|
1995-10-03 19:17:21 +00:00
|
|
|
then the value is decremented by the absolute value of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op .
|
2001-10-11 08:15:14 +00:00
|
|
|
If
|
|
|
|
.Dv SEM_UNDO
|
|
|
|
is specified, the semaphore's adjust on exit
|
1995-10-03 19:17:21 +00:00
|
|
|
value is incremented by the absolute value of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op .
|
1995-10-03 19:17:21 +00:00
|
|
|
.It
|
2001-10-11 08:15:14 +00:00
|
|
|
If the current value of the semaphore is less than the absolute value of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op ,
|
2001-10-11 08:15:14 +00:00
|
|
|
one of the following happens:
|
1995-10-03 19:17:21 +00:00
|
|
|
.\" XXX a *second* sublist?
|
|
|
|
.Bl -bullet
|
|
|
|
.It
|
2001-10-11 08:15:14 +00:00
|
|
|
If
|
|
|
|
.Dv IPC_NOWAIT
|
|
|
|
was specified, then
|
1995-10-03 19:17:21 +00:00
|
|
|
.Fn semop
|
2000-11-22 16:02:00 +00:00
|
|
|
returns immediately with a return value of
|
|
|
|
.Er EAGAIN .
|
1995-10-03 19:17:21 +00:00
|
|
|
.It
|
2001-10-11 08:15:14 +00:00
|
|
|
Otherwise, the calling process is put to sleep until one of the following
|
|
|
|
conditions is satisfied:
|
|
|
|
.\" XXX We already have two sublists, why not a third?
|
|
|
|
.Bl -bullet
|
|
|
|
.It
|
|
|
|
Some other process removes the semaphore with the
|
|
|
|
.Dv IPC_RMID
|
1995-10-03 19:17:21 +00:00
|
|
|
option of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Xr semctl 2 .
|
2001-10-11 08:15:14 +00:00
|
|
|
In this case,
|
1995-10-03 19:17:21 +00:00
|
|
|
.Fn semop
|
2000-11-22 16:02:00 +00:00
|
|
|
returns immediately with a return value of
|
2001-10-11 08:15:14 +00:00
|
|
|
.Er EIDRM .
|
1995-10-03 19:17:21 +00:00
|
|
|
.It
|
2001-10-11 08:15:14 +00:00
|
|
|
The process receives a signal that is to be caught.
|
|
|
|
In this case, the process will resume execution as defined by
|
2001-11-21 15:41:49 +00:00
|
|
|
.Xr sigaction 2 .
|
2001-10-11 08:15:14 +00:00
|
|
|
.It
|
|
|
|
The semaphore's
|
1995-10-03 19:17:21 +00:00
|
|
|
value is greater than or equal to the absolute value of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op .
|
1995-10-03 19:17:21 +00:00
|
|
|
When this condition becomes true, the semaphore's value is decremented
|
|
|
|
by the absolute value of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op ,
|
2001-10-11 08:15:14 +00:00
|
|
|
the semaphore's adjust on exit value is incremented by the
|
1995-10-03 19:17:21 +00:00
|
|
|
absolute value of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op .
|
|
|
|
.El
|
1995-10-03 19:17:21 +00:00
|
|
|
.El
|
2001-10-11 08:15:14 +00:00
|
|
|
.El
|
1995-10-03 19:17:21 +00:00
|
|
|
.Pp
|
|
|
|
A negative value for
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op
|
1995-10-03 19:17:21 +00:00
|
|
|
generally means that a process is waiting for a resource to become
|
|
|
|
available.
|
|
|
|
.It
|
|
|
|
When
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_op
|
2001-10-11 08:15:14 +00:00
|
|
|
is zero and the process has read permission,
|
|
|
|
one of the following will occur:
|
|
|
|
.Bl -bullet
|
|
|
|
.It
|
|
|
|
If the current value of the semaphore is equal to zero
|
|
|
|
then
|
1995-10-03 19:17:21 +00:00
|
|
|
.Fn semop
|
2001-10-11 08:15:14 +00:00
|
|
|
can return immediately.
|
|
|
|
.It
|
|
|
|
If
|
|
|
|
.Dv IPC_NOWAIT
|
|
|
|
was specified, then
|
|
|
|
.Fn semop
|
|
|
|
returns immediately with a return value of
|
|
|
|
.Er EAGAIN .
|
|
|
|
.It
|
|
|
|
Otherwise, the calling process is put to sleep until one of the following
|
|
|
|
conditions is satisfied:
|
|
|
|
.\" XXX Another nested sublists
|
|
|
|
.Bl -bullet
|
|
|
|
.It
|
|
|
|
Some other process removes the semaphore with the
|
|
|
|
.Dv IPC_RMID
|
|
|
|
option of
|
2001-11-21 15:41:49 +00:00
|
|
|
.Xr semctl 2 .
|
2001-10-11 08:15:14 +00:00
|
|
|
In this case,
|
|
|
|
.Fn semop
|
|
|
|
returns immediately with a return value of
|
|
|
|
.Er EIDRM .
|
|
|
|
.It
|
|
|
|
The process receives a signal that is to be caught.
|
|
|
|
In this case, the process will resume execution as defined by
|
2001-11-21 15:41:49 +00:00
|
|
|
.Xr sigaction 2 .
|
2001-10-11 08:15:14 +00:00
|
|
|
.It
|
|
|
|
The semaphore's value becomes zero.
|
|
|
|
.El
|
1995-10-03 19:17:21 +00:00
|
|
|
.El
|
2001-11-21 15:41:49 +00:00
|
|
|
.El
|
1995-10-03 19:17:21 +00:00
|
|
|
.Pp
|
|
|
|
For each semaphore a process has in use, the kernel maintains an
|
2001-11-21 15:41:49 +00:00
|
|
|
.Dq "adjust on exit"
|
|
|
|
value, as alluded to earlier.
|
|
|
|
When a process
|
1995-10-03 19:17:21 +00:00
|
|
|
exits, either voluntarily or involuntarily, the adjust on exit value
|
2001-11-21 15:41:49 +00:00
|
|
|
for each semaphore is added to the semaphore's value.
|
|
|
|
This can
|
1995-10-03 19:17:21 +00:00
|
|
|
be used to insure that a resource is released if a process terminates
|
|
|
|
unexpectedly.
|
|
|
|
.Sh RETURN VALUES
|
2001-08-09 13:32:13 +00:00
|
|
|
.Rv -std semop
|
1995-10-03 19:17:21 +00:00
|
|
|
.Sh ERRORS
|
2001-11-21 15:41:49 +00:00
|
|
|
The
|
|
|
|
.Fn semop
|
2002-12-18 09:22:32 +00:00
|
|
|
system call will fail if:
|
1995-10-03 19:17:21 +00:00
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EINVAL
|
|
|
|
No semaphore set corresponds to
|
2001-10-11 08:15:14 +00:00
|
|
|
.Fa semid ,
|
|
|
|
or the process would exceed the system-defined limit for the number of
|
2001-11-21 15:41:49 +00:00
|
|
|
per-process
|
|
|
|
.Dv SEM_UNDO
|
|
|
|
structures.
|
1995-10-03 19:17:21 +00:00
|
|
|
.It Bq Er EACCES
|
|
|
|
Permission denied due to mismatch between operation and mode of
|
|
|
|
semaphore set.
|
|
|
|
.It Bq Er EAGAIN
|
2001-10-11 08:15:14 +00:00
|
|
|
The semaphore's value would have resulted in the process being put to sleep
|
|
|
|
and
|
|
|
|
.Dv IPC_NOWAIT
|
|
|
|
was specified.
|
1995-10-03 19:17:21 +00:00
|
|
|
.It Bq Er E2BIG
|
|
|
|
Too many operations were specified.
|
2001-10-11 08:15:14 +00:00
|
|
|
.Bq Dv SEMOPM
|
1995-10-03 19:17:21 +00:00
|
|
|
.It Bq Er EFBIG
|
|
|
|
.\"
|
|
|
|
.\" I'd have thought this would be EINVAL, but the source says
|
|
|
|
.\" EFBIG.
|
|
|
|
.\"
|
2001-11-21 15:41:49 +00:00
|
|
|
.Va sem_num
|
1995-10-03 19:17:21 +00:00
|
|
|
was not in the range of valid semaphores for the set.
|
2001-10-11 08:15:14 +00:00
|
|
|
.It Bq Er EIDRM
|
|
|
|
The semaphore set was removed from the system.
|
|
|
|
.It Bq Er EINTR
|
|
|
|
The
|
|
|
|
.Fn semop
|
2002-12-18 09:22:32 +00:00
|
|
|
system call was interrupted by a signal.
|
2001-10-11 08:15:14 +00:00
|
|
|
.It Bq Er ENOSPC
|
2001-11-21 15:41:49 +00:00
|
|
|
The system
|
|
|
|
.Dv SEM_UNDO
|
|
|
|
pool
|
2001-10-11 08:15:14 +00:00
|
|
|
.Bq Dv SEMMNU
|
|
|
|
is full.
|
|
|
|
.It Bq Er ERANGE
|
|
|
|
The requested operation would cause either
|
|
|
|
the semaphore's current value
|
|
|
|
.Bq Dv SEMVMX
|
|
|
|
or its adjust on exit value
|
|
|
|
.Bq Dv SEMAEM
|
|
|
|
to exceed the system-imposed limits.
|
2000-12-29 14:08:20 +00:00
|
|
|
.El
|
1995-10-03 19:17:21 +00:00
|
|
|
.Sh SEE ALSO
|
1997-01-20 23:23:22 +00:00
|
|
|
.Xr semctl 2 ,
|
2001-10-11 08:15:14 +00:00
|
|
|
.Xr semget 2 ,
|
|
|
|
.Xr sigaction 2
|
|
|
|
.Sh BUGS
|
2001-11-21 15:41:49 +00:00
|
|
|
The
|
|
|
|
.Fn semop
|
2002-12-18 09:22:32 +00:00
|
|
|
system call
|
2001-10-11 08:15:14 +00:00
|
|
|
may block waiting for memory even if
|
|
|
|
.Dv IPC_NOWAIT
|
2001-11-21 15:41:49 +00:00
|
|
|
was specified.
|