summaryrefslogtreecommitdiff
path: root/static/netbsd/man2/semop.2
blob: 6661d877cdd815629246968ab2f9dcaf4b9bf5ac (plain)
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
.\"	$NetBSD: semop.2,v 1.19 2026/01/02 22:22:22 nia Exp $
.\"
.\" Copyright (c) 1995 Frank van der Linden
.\" 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.
.\" 3. The name of the author may not be used to endorse or promote products
.\"    derived from this software without specific prior written permission
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
.\"
.Dd October 3, 2024
.Dt SEMOP 2
.Os
.Sh NAME
.Nm semop, semtimedop
.Nd semaphore operations
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/sem.h
.Ft int
.Fn semop "int semid" "struct sembuf *sops" "size_t nsops"
.Ft int
.Fn semtimedop "int semid" "struct sembuf *sops" "size_t nsops" "struct timespec *timeout"
.Sh DESCRIPTION
.Fn semop
provides a number of atomic operations on a set of semaphores.
The semaphore set is specified by
.Fa semid ,
.Fa sops
is an array of semaphore operations, and
.Fa nsops
is the number of operations in this array.
The
.Va sembuf
structures in the array contain the following members:
.Bd -literal
    unsigned short sem_num; /* semaphore # */
    short          sem_op;  /* semaphore operation */
    short          sem_flg; /* operation flags */
.Ed
.Pp
Each operation (specified in
.Va sem_op )
is applied to semaphore number
.Va sem_num
in the set of semaphores specified by
.Fa semid .
The value of
.Va sem_op
determines the action taken in the following way:
.Bl -bullet
.It
.Va sem_op
is less than 0.
The current process is blocked until the value of the
semaphore is greater than or equal to the absolute value of
.Va sem_op .
The absolute value of
.Va sem_op
is then subtracted from the value of the semaphore, and the calling
process continues.
Negative values of
.Va sem_op
are thus used to enter critical regions.
.It
.Va sem_op
is greater than 0.
Its value is added to the value of the specified semaphore.
This is used to leave critical regions.
.It
.Va sem_op
is equal to 0.
The calling process is blocked until the value of the
specified semaphore reaches 0.
.El
.Pp
The behaviour of each operation is influenced by the flags set in
.Va sem_flg
in the following way:
.Bl -tag -width IPC_NOWAITX
.It Dv IPC_NOWAIT
In the case where the calling process would normally block, waiting
for a semaphore to reach a certain value,
.Dv IPC_NOWAIT
makes the
call return immediately, returning a value of \-1 and setting
.Va errno
to
.Er EAGAIN .
.It SEM_UNDO
Keep track of the changes that this call makes to the value of a semaphore,
so that they can be undone when the calling process terminates.
This is useful to prevent other processes waiting on a semaphore to block
forever, should the process that has the semaphore locked terminate in a
critical section.
.El
.Pp
.Fn semtimedop
is similar to
.Fn semop ,
but it also allows specifying a timeout.
When the semaphore is not available,
the thread typically sleeps until the semaphore is available.
.Fn semtimedop
allows specifying a maximum amount of time in
.Fa timeout
argument that a thread should sleep while waiting for the semaphore to be available.
If the specified time limit has been reached,
.Fn semtimedop
fails with
.Er EAGAIN
(and none of the operations in
.Fa sops
are performed).
If
.Fa timeout
is
.Dv NULL ,
.Fn semtimedop
behaves exactly like
.Fn semop .
.Sh RETURN VALUES
Upon successful completion both
.Fn semop
and
.Fn semtimedop
return a value of 0. Otherwise, \-1 is returned and the global variable
.Va errno
is set to indicate the error.
.Sh ERRORS
.Fn semop
will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
There is no semaphore associated with
.Fa semid .
.It Bq Er EIDRM
The semaphore set was removed while the process was waiting for one of
its semaphores to reach a certain value.
.It Bq Er EACCES
The calling process has no permission to access the specified semaphore set.
.It Bq Er E2BIG
The value of
.Fa nsops
is too big.
The maximum is defined as
.Dv MAX_SOPS
in
.In sys/sem.h .
.It Bq Er EFBIG
.Va sem_num
in one of the sem_buf structures is less than 0, or greater than the actual
number of semaphores in the set specified by
.Fa semid .
.It Bq Er ENOSPC
.Dv SEM_UNDO
was requested, and there is not enough space left in the kernel to
store the undo information.
.It Bq Er EAGAIN
The requested operation can not immediately be performed, and
.Dv IPC_NOWAIT
was set in
.Va sem_flg .
.It Bq Er EFAULT
.Fa sops
points to an illegal address.
.It Bq Er EINTR
While blocked in this system call, the thread caught a signal.
.El
.Pp
In addition,
.Fn semtimedop
will fail if:
.Bl -tag -width Er
.It Bq Er EAGAIN
An operation could not proceed immediately and either
.Dv IPC_NOWAIT
was specified in
.Va sem_flg
or the time limit specified in
.Fa timeout
expired.
.It Bq Er EFAULT
An address specified in the
.Fa timeout
argument isn't accessible.
.El
.Sh EXAMPLES
The following example shows how to perform a semaphore operation with a timeout:
.Bd -literal -offset indent

/* Performs a semaphore operation with a 5 sec timeout*/

struct sembuf sops[1];        /* Semaphore operation structure */
struct timespec timeout;      /* Timeout structure */

/* Create semaphore set with 1 semaphore */
int semid = semget(key, 1, 0666 | IPC_CREAT);

/* Initialize semaphore to 0 */
if (semctl(semid, 0, SETVAL, 0) == -1) {
    warn("semctl SETVAL");
    exit(EXIT_FAILURE);
}

sops[0].sem_num = 0;          /* Operation on semaphore 0 */
sops[0].sem_op = -1;          /* Decrement semaphore by 1 */
sops[0].sem_flg = 0;          /* No flags */

timeout.tv_sec = 5;           /* 5 seconds */
timeout.tv_nsec = 0;          /* 0 nanoseconds */

if (semtimedop(semid, sops, 1, &timeout) == -1) {
    warn("semtimedop");       /* Print error message */
}
.Ed
.Sh SEE ALSO
.Xr semctl 2 ,
.Xr semget 2
.Sh STANDARDS
The
.Nm
system call conforms to
.St -xsh5 .
.Sh HISTORY
Semaphores appeared in the first release of
.At V .