]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/semget.2
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man2 / semget.2
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright (C) 2020 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" Modified Tue Oct 22 17:54:56 1996 by Eric S. Raymond <esr@thyrsus.com>
7 .\" Modified 1 Jan 2002, Martin Schulze <joey@infodrom.org>
8 .\" Modified 4 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
9 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
10 .\" Added notes on capability requirements
11 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
12 .\" Language and formatting clean-ups
13 .\" Added notes on /proc files
14 .\" Rewrote BUGS note about semget()'s failure to initialize
15 .\" semaphore values
16 .\"
17 .TH SEMGET 2 2021-03-22 "Linux man-pages (unreleased)"
18 .SH NAME
19 semget \- get a System V semaphore set identifier
20 .SH LIBRARY
21 Standard C library
22 .RI ( libc ", " \-lc )
23 .SH SYNOPSIS
24 .nf
25 .B #include <sys/sem.h>
26 .fi
27 .PP
28 .BI "int semget(key_t " key ,
29 .BI "int " nsems ,
30 .BI "int " semflg );
31 .SH DESCRIPTION
32 The
33 .BR semget ()
34 system call returns the System\ V semaphore set identifier
35 associated with the argument
36 .IR key .
37 It may be used either to obtain the identifier of a previously created
38 semaphore set (when
39 .I semflg
40 is zero and
41 .I key
42 does not have the value
43 .BR IPC_PRIVATE ),
44 or to create a new set.
45 .PP
46 A new set of
47 .I nsems
48 semaphores is created if
49 .I key
50 has the value
51 .B IPC_PRIVATE
52 or if no existing semaphore set is associated with
53 .I key
54 and
55 .B IPC_CREAT
56 is specified in
57 .IR semflg .
58 .PP
59 If
60 .I semflg
61 specifies both
62 .B IPC_CREAT
63 and
64 .B IPC_EXCL
65 and a semaphore set already exists for
66 .IR key ,
67 then
68 .BR semget ()
69 fails with
70 .I errno
71 set to
72 .BR EEXIST .
73 (This is analogous to the effect of the combination
74 .B O_CREAT | O_EXCL
75 for
76 .BR open (2).)
77 .PP
78 Upon creation, the least significant 9 bits of the argument
79 .I semflg
80 define the permissions (for owner, group, and others)
81 for the semaphore set.
82 These bits have the same format, and the same
83 meaning, as the
84 .I mode
85 argument of
86 .BR open (2)
87 (though the execute permissions are
88 not meaningful for semaphores, and write permissions mean permission
89 to alter semaphore values).
90 .PP
91 When creating a new semaphore set,
92 .BR semget ()
93 initializes the set's associated data structure,
94 .I semid_ds
95 (see
96 .BR semctl (2)),
97 as follows:
98 .IP \(bu 2
99 .I sem_perm.cuid
100 and
101 .I sem_perm.uid
102 are set to the effective user ID of the calling process.
103 .IP \(bu
104 .I sem_perm.cgid
105 and
106 .I sem_perm.gid
107 are set to the effective group ID of the calling process.
108 .IP \(bu
109 The least significant 9 bits of
110 .I sem_perm.mode
111 are set to the least significant 9 bits of
112 .IR semflg .
113 .IP \(bu
114 .I sem_nsems
115 is set to the value of
116 .IR nsems .
117 .IP \(bu
118 .I sem_otime
119 is set to 0.
120 .IP \(bu
121 .I sem_ctime
122 is set to the current time.
123 .PP
124 The argument
125 .I nsems
126 can be 0
127 (a don't care)
128 when a semaphore set is not being created.
129 Otherwise,
130 .I nsems
131 must be greater than 0
132 and less than or equal to the maximum number of semaphores per semaphore set
133 .RB ( SEMMSL ).
134 .PP
135 If the semaphore set already exists, the permissions are
136 verified.
137 .\" and a check is made to see if it is marked for destruction.
138 .SH RETURN VALUE
139 On success,
140 .BR semget ()
141 returns the semaphore set identifier (a nonnegative integer).
142 On failure, \-1 is returned, and
143 .I errno
144 is set to indicate the error.
145 .SH ERRORS
146 .TP
147 .B EACCES
148 A semaphore set exists for
149 .IR key ,
150 but the calling process does not have permission to access the set,
151 and does not have the
152 .B CAP_IPC_OWNER
153 capability in the user namespace that governs its IPC namespace.
154 .TP
155 .B EEXIST
156 .B IPC_CREAT
157 and
158 .B IPC_EXCL
159 were specified in
160 .IR semflg ,
161 but a semaphore set already exists for
162 .IR key .
163 .\" .TP
164 .\" .B EIDRM
165 .\" The semaphore set is marked to be deleted.
166 .TP
167 .B EINVAL
168 .I nsems
169 is less than 0 or greater than the limit on the number
170 of semaphores per semaphore set
171 .RB ( SEMMSL ).
172 .TP
173 .B EINVAL
174 A semaphore set corresponding to
175 .I key
176 already exists, but
177 .I nsems
178 is larger than the number of semaphores in that set.
179 .TP
180 .B ENOENT
181 No semaphore set exists for
182 .I key
183 and
184 .I semflg
185 did not specify
186 .BR IPC_CREAT .
187 .TP
188 .B ENOMEM
189 A semaphore set has to be created but the system does not have
190 enough memory for the new data structure.
191 .TP
192 .B ENOSPC
193 A semaphore set has to be created but the system limit for the maximum
194 number of semaphore sets
195 .RB ( SEMMNI ),
196 or the system wide maximum number of semaphores
197 .RB ( SEMMNS ),
198 would be exceeded.
199 .SH STANDARDS
200 SVr4, POSIX.1-2001.
201 .\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
202 .\" ERANGE, EFAULT.
203 .SH NOTES
204 .B IPC_PRIVATE
205 isn't a flag field but a
206 .I key_t
207 type.
208 If this special value is used for
209 .IR key ,
210 the system call ignores all but the least significant 9 bits of
211 .I semflg
212 and creates a new semaphore set (on success).
213 .\"
214 .SS Semaphore initialization
215 The values of the semaphores in a newly created set are indeterminate.
216 (POSIX.1-2001 and POSIX.1-2008 are explicit on this point,
217 although POSIX.1-2008 notes that a future version of the standard
218 may require an implementation to initialize the semaphores to 0.)
219 Although Linux, like many other implementations,
220 initializes the semaphore values to 0,
221 a portable application cannot rely on this:
222 it should explicitly initialize the semaphores to the desired values.
223 .\" In truth, every one of the many implementations that I've tested sets
224 .\" the values to zero, but I suppose there is/was some obscure
225 .\" implementation out there that does not.
226 .PP
227 Initialization can be done using
228 .BR semctl (2)
229 .B SETVAL
230 or
231 .B SETALL
232 operation.
233 Where multiple peers do not know who will be the first to
234 initialize the set, checking for a nonzero
235 .I sem_otime
236 in the associated data structure retrieved by a
237 .BR semctl (2)
238 .B IPC_STAT
239 operation can be used to avoid races.
240 .\"
241 .SS Semaphore limits
242 The following limits on semaphore set resources affect the
243 .BR semget ()
244 call:
245 .TP
246 .B SEMMNI
247 System-wide limit on the number of semaphore sets.
248 On Linux systems before version 3.19,
249 the default value for this limit was 128.
250 Since Linux 3.19,
251 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
252 the default value is 32,000.
253 On Linux, this limit can be read and modified via the fourth field of
254 .IR /proc/sys/kernel/sem .
255 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
256 .TP
257 .B SEMMSL
258 Maximum number of semaphores per semaphore ID.
259 On Linux systems before version 3.19,
260 the default value for this limit was 250.
261 Since Linux 3.19,
262 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
263 the default value is 32,000.
264 On Linux, this limit can be read and modified via the first field of
265 .IR /proc/sys/kernel/sem .
266 .TP
267 .B SEMMNS
268 System-wide limit on the number of semaphores: policy dependent
269 (on Linux, this limit can be read and modified via the second field of
270 .IR /proc/sys/kernel/sem ).
271 Note that the number of semaphores system-wide
272 is also limited by the product of
273 .B SEMMSL
274 and
275 .BR SEMMNI .
276 .SH BUGS
277 The name choice
278 .B IPC_PRIVATE
279 was perhaps unfortunate,
280 .B IPC_NEW
281 would more clearly show its function.
282 .SH EXAMPLES
283 The program shown below uses
284 .BR semget ()
285 to create a new semaphore set or retrieve the ID of an existing set.
286 It generates the
287 .I key
288 for
289 .BR semget ()
290 using
291 .BR ftok (3).
292 The first two command-line arguments are used as the
293 .I pathname
294 and
295 .I proj_id
296 arguments for
297 .BR ftok (3).
298 The third command-line argument is an integer that specifies the
299 .I nsems
300 argument for
301 .BR semget ().
302 Command-line options can be used to specify the
303 .B IPC_CREAT
304 .RI ( \-c )
305 and
306 .B IPC_EXCL
307 .RI ( \-x )
308 flags for the call to
309 .BR semget ().
310 The usage of this program is demonstrated below.
311 .PP
312 We first create two files that will be used to generate keys using
313 .BR ftok (3),
314 create two semaphore sets using those files, and then list the sets using
315 .BR ipcs (1):
316 .PP
317 .in +4n
318 .EX
319 $ \fBtouch mykey mykey2\fP
320 $ \fB./t_semget \-c mykey p 1\fP
321 ID = 9
322 $ \fB./t_semget \-c mykey2 p 2\fP
323 ID = 10
324 $ \fBipcs \-s\fP
325
326 \-\-\-\-\-\- Semaphore Arrays \-\-\-\-\-\-\-\-
327 key semid owner perms nsems
328 0x7004136d 9 mtk 600 1
329 0x70041368 10 mtk 600 2
330 .EE
331 .in
332 .PP
333 Next, we demonstrate that when
334 .BR semctl (2)
335 is given the same
336 .I key
337 (as generated by the same arguments to
338 .BR ftok (3)),
339 it returns the ID of the already existing semaphore set:
340 .PP
341 .in +4n
342 .EX
343 $ \fB./t_semget \-c mykey p 1\fP
344 ID = 9
345 .EE
346 .in
347 .PP
348 Finally, we demonstrate the kind of collision that can occur when
349 .BR ftok (3)
350 is given different
351 .I pathname
352 arguments that have the same inode number:
353 .PP
354 .in +4n
355 .EX
356 $ \fBln mykey link\fP
357 $ \fBls \-i1 link mykey\fP
358 2233197 link
359 2233197 mykey
360 $ \fB./t_semget link p 1\fP # Generates same key as \(aqmykey\(aq
361 ID = 9
362 .EE
363 .in
364 .SS Program source
365 \&
366 .\" SRC BEGIN (t_semget.c)
367 .EX
368 /* t_semget.c
369
370 Licensed under GNU General Public License v2 or later.
371 */
372 #include <stdio.h>
373 #include <stdlib.h>
374 #include <sys/ipc.h>
375 #include <sys/sem.h>
376 #include <unistd.h>
377
378 static void
379 usage(const char *pname)
380 {
381 fprintf(stderr, "Usage: %s [\-cx] pathname proj\-id num\-sems\en",
382 pname);
383 fprintf(stderr, " \-c Use IPC_CREAT flag\en");
384 fprintf(stderr, " \-x Use IPC_EXCL flag\en");
385 exit(EXIT_FAILURE);
386 }
387
388 int
389 main(int argc, char *argv[])
390 {
391 int semid, nsems, flags, opt;
392 key_t key;
393
394 flags = 0;
395 while ((opt = getopt(argc, argv, "cx")) != \-1) {
396 switch (opt) {
397 case \(aqc\(aq: flags |= IPC_CREAT; break;
398 case \(aqx\(aq: flags |= IPC_EXCL; break;
399 default: usage(argv[0]);
400 }
401 }
402
403 if (argc != optind + 3)
404 usage(argv[0]);
405
406 key = ftok(argv[optind], argv[optind + 1][0]);
407 if (key == \-1) {
408 perror("ftok");
409 exit(EXIT_FAILURE);
410 }
411
412 nsems = atoi(argv[optind + 2]);
413
414 semid = semget(key, nsems, flags | 0600);
415 if (semid == \-1) {
416 perror("semget");
417 exit(EXIT_FAILURE);
418 }
419
420 printf("ID = %d\en", semid);
421
422 exit(EXIT_SUCCESS);
423 }
424 .EE
425 .\" SRC END
426 .SH SEE ALSO
427 .BR semctl (2),
428 .BR semop (2),
429 .BR ftok (3),
430 .BR capabilities (7),
431 .BR sem_overview (7),
432 .BR sysvipc (7)