1SEMGET(2) Linux Programmer's Manual SEMGET(2)
2
3
4
6 semget - get a System V semaphore set identifier
7
9 #include <sys/types.h>
10 #include <sys/ipc.h>
11 #include <sys/sem.h>
12
13 int semget(key_t key, int nsems, int semflg);
14
16 The semget() system call returns the System V semaphore set identifier
17 associated with the argument key. A new set of nsems semaphores is
18 created if key has the value IPC_PRIVATE or if no existing semaphore
19 set is associated with key and IPC_CREAT is specified in semflg.
20
21 If semflg specifies both IPC_CREAT and IPC_EXCL and a semaphore set
22 already exists for key, then semget() fails with errno set to EEXIST.
23 (This is analogous to the effect of the combination O_CREAT | O_EXCL
24 for open(2).)
25
26 Upon creation, the least significant 9 bits of the argument semflg
27 define the permissions (for owner, group and others) for the semaphore
28 set. These bits have the same format, and the same meaning, as the
29 mode argument of open(2) (though the execute permissions are not mean‐
30 ingful for semaphores, and write permissions mean permission to alter
31 semaphore values).
32
33 When creating a new semaphore set, semget() initializes the set's asso‐
34 ciated data structure, semid_ds (see semctl(2)), as follows:
35
36 sem_perm.cuid and sem_perm.uid are set to the effective user ID
37 of the calling process.
38
39 sem_perm.cgid and sem_perm.gid are set to the effective group ID
40 of the calling process.
41
42 The least significant 9 bits of sem_perm.mode are set to the
43 least significant 9 bits of semflg.
44
45 sem_nsems is set to the value of nsems.
46
47 sem_otime is set to 0.
48
49 sem_ctime is set to the current time.
50
51 The argument nsems can be 0 (a don't care) when a semaphore set is not
52 being created. Otherwise, nsems must be greater than 0 and less than
53 or equal to the maximum number of semaphores per semaphore set
54 (SEMMSL).
55
56 If the semaphore set already exists, the permissions are verified.
57
59 If successful, the return value will be the semaphore set identifier (a
60 nonnegative integer), otherwise, -1 is returned, with errno indicating
61 the error.
62
64 On failure, errno will be set to one of the following:
65
66 EACCES A semaphore set exists for key, but the calling process does not
67 have permission to access the set, and does not have the
68 CAP_IPC_OWNER capability in the user namespace that governs its
69 IPC namespace.
70
71 EEXIST IPC_CREAT and IPC_EXCL were specified in semflg, but a semaphore
72 set already exists for key.
73
74 EINVAL nsems is less than 0 or greater than the limit on the number of
75 semaphores per semaphore set (SEMMSL).
76
77 EINVAL A semaphore set corresponding to key already exists, but nsems
78 is larger than the number of semaphores in that set.
79
80 ENOENT No semaphore set exists for key and semflg did not specify
81 IPC_CREAT.
82
83 ENOMEM A semaphore set has to be created but the system does not have
84 enough memory for the new data structure.
85
86 ENOSPC A semaphore set has to be created but the system limit for the
87 maximum number of semaphore sets (SEMMNI), or the system wide
88 maximum number of semaphores (SEMMNS), would be exceeded.
89
91 SVr4, POSIX.1-2001.
92
94 The inclusion of <sys/types.h> and <sys/ipc.h> isn't required on Linux
95 or by any version of POSIX. However, some old implementations required
96 the inclusion of these header files, and the SVID also documented their
97 inclusion. Applications intended to be portable to such old systems
98 may need to include these header files.
99
100 IPC_PRIVATE isn't a flag field but a key_t type. If this special value
101 is used for key, the system call ignores all but the least significant
102 9 bits of semflg and creates a new semaphore set (on success).
103
104 Semaphore initialization
105 The values of the semaphores in a newly created set are indeterminate.
106 (POSIX.1-2001 and POSIX.1-2008 are explicit on this point, although
107 POSIX.1-2008 notes that a future version of the standard may require an
108 implementation to initialize the semaphores to 0.) Although Linux,
109 like many other implementations, initializes the semaphore values to 0,
110 a portable application cannot rely on this: it should explicitly ini‐
111 tialize the semaphores to the desired values.
112
113 Initialization can be done using semctl(2) SETVAL or SETALL operation.
114 Where multiple peers do not know who will be the first to initialize
115 the set, checking for a nonzero sem_otime in the associated data struc‐
116 ture retrieved by a semctl(2) IPC_STAT operation can be used to avoid
117 races.
118
119 Semaphore limits
120 The following limits on semaphore set resources affect the semget()
121 call:
122
123 SEMMNI System-wide limit on the number of semaphore sets. On Linux
124 systems before version 3.19, the default value for this limit
125 was 128. Since Linux 3.19, the default value is 32,000. On
126 Linux, this limit can be read and modified via the fourth field
127 of /proc/sys/kernel/sem.
128
129 SEMMSL Maximum number of semaphores per semaphore ID. On Linux systems
130 before version 3.19, the default value for this limit was 250.
131 Since Linux 3.19, the default value is 32,000. On Linux, this
132 limit can be read and modified via the first field of
133 /proc/sys/kernel/sem.
134
135 SEMMNS System-wide limit on the number of semaphores: policy dependent
136 (on Linux, this limit can be read and modified via the second
137 field of /proc/sys/kernel/sem). Note that the number of sema‐
138 phores system-wide is also limited by the product of SEMMSL and
139 SEMMNI.
140
142 The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
143 clearly show its function.
144
146 semctl(2), semop(2), ftok(3), capabilities(7), sem_overview(7),
147 svipc(7)
148
150 This page is part of release 4.15 of the Linux man-pages project. A
151 description of the project, information about reporting bugs, and the
152 latest version of this page, can be found at
153 https://www.kernel.org/doc/man-pages/.
154
155
156
157Linux 2017-09-15 SEMGET(2)