1SEMGET(3P) POSIX Programmer's Manual SEMGET(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 semget — get set of XSI semaphores
14
16 #include <sys/sem.h>
17
18 int semget(key_t key, int nsems, int semflg);
19
21 The semget() function operates on XSI semaphores (see the Base Defini‐
22 tions volume of POSIX.1‐2008, Section 4.16, Semaphore). It is unspeci‐
23 fied whether this function interoperates with the realtime interprocess
24 communication facilities defined in Section 2.8, Realtime.
25
26 The semget() function shall return the semaphore identifier associated
27 with key.
28
29 A semaphore identifier with its associated semid_ds data structure and
30 its associated set of nsems semaphores (see <sys/sem.h>) is created for
31 key if one of the following is true:
32
33 * The argument key is equal to IPC_PRIVATE.
34
35 * The argument key does not already have a semaphore identifier asso‐
36 ciated with it and (semflg &IPC_CREAT) is non-zero.
37
38 Upon creation, the semid_ds data structure associated with the new sem‐
39 aphore identifier is initialized as follows:
40
41 * In the operation permissions structure sem_perm.cuid, sem_perm.uid,
42 sem_perm.cgid, and sem_perm.gid shall be set to the effective user
43 ID and effective group ID, respectively, of the calling process.
44
45 * The low-order 9 bits of sem_perm.mode shall be set to the low-order
46 9 bits of semflg.
47
48 * The variable sem_nsems shall be set to the value of nsems.
49
50 * The variable sem_otime shall be set to 0 and sem_ctime shall be set
51 to the current time, as described in Section 2.7.1, IPC General
52 Description.
53
54 * The data structure associated with each semaphore in the set need
55 not be initialized. The semctl() function with the command SETVAL
56 or SETALL can be used to initialize each semaphore.
57
59 Upon successful completion, semget() shall return a non-negative inte‐
60 ger, namely a semaphore identifier; otherwise, it shall return −1 and
61 set errno to indicate the error.
62
64 The semget() function shall fail if:
65
66 EACCES A semaphore identifier exists for key, but operation permission
67 as specified by the low-order 9 bits of semflg would not be
68 granted; see Section 2.7, XSI Interprocess Communication.
69
70 EEXIST A semaphore identifier exists for the argument key but ((semflg
71 &IPC_CREAT) &&(semflg &IPC_EXCL)) is non-zero.
72
73 EINVAL The value of nsems is either less than or equal to 0 or greater
74 than the system-imposed limit, or a semaphore identifier exists
75 for the argument key, but the number of semaphores in the set
76 associated with it is less than nsems and nsems is not equal to
77 0.
78
79 ENOENT A semaphore identifier does not exist for the argument key and
80 (semflg &IPC_CREAT) is equal to 0.
81
82 ENOSPC A semaphore identifier is to be created but the system-imposed
83 limit on the maximum number of allowed semaphores system-wide
84 would be exceeded.
85
86 The following sections are informative.
87
89 Refer to semop().
90
92 The POSIX Realtime Extension defines alternative interfaces for inter‐
93 process communication. Application developers who need to use IPC
94 should design their applications so that modules using the IPC routines
95 described in Section 2.7, XSI Interprocess Communication can be easily
96 modified to use the alternative interfaces.
97
99 None.
100
102 A future version may require that the value of the semval, sempid, sem‐
103 ncnt, and semzcnt members of all semaphores in a semaphore set be ini‐
104 tialized to zero when a call to semget() creates a semaphore set. Many
105 semaphore implementations already do this and it greatly simplifies
106 what an application must do to initialize a semaphore set.
107
109 Section 2.7, XSI Interprocess Communication, Section 2.8, Realtime,
110 ftok(), semctl(), semop(), sem_close(), sem_destroy(), sem_getvalue(),
111 sem_init(), sem_open(), sem_post(), sem_trywait(), sem_unlink()
112
113 The Base Definitions volume of POSIX.1‐2008, Section 4.16, Semaphore,
114 <sys_sem.h>
115
117 Portions of this text are reprinted and reproduced in electronic form
118 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
119 -- Portable Operating System Interface (POSIX), The Open Group Base
120 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
121 cal and Electronics Engineers, Inc and The Open Group. (This is
122 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
123 event of any discrepancy between this version and the original IEEE and
124 The Open Group Standard, the original IEEE and The Open Group Standard
125 is the referee document. The original Standard can be obtained online
126 at http://www.unix.org/online.html .
127
128 Any typographical or formatting errors that appear in this page are
129 most likely to have been introduced during the conversion of the source
130 files to man page format. To report such errors, see https://www.ker‐
131 nel.org/doc/man-pages/reporting_bugs.html .
132
133
134
135IEEE/The Open Group 2013 SEMGET(3P)