]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it) |
2 | .\" | |
3 | .\" Permission is granted to make and distribute verbatim copies of this | |
4 | .\" manual provided the copyright notice and this permission notice are | |
5 | .\" preserved on all copies. | |
6 | .\" | |
7 | .\" Permission is granted to copy and distribute modified versions of this | |
8 | .\" manual under the conditions for verbatim copying, provided that the | |
9 | .\" entire resulting derived work is distributed under the terms of a | |
10 | .\" permission notice identical to this one. | |
c13182ef | 11 | .\" |
fea681da MK |
12 | .\" Since the Linux kernel and libraries are constantly changing, this |
13 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
14 | .\" responsibility for errors or omissions, or for damages resulting from | |
15 | .\" the use of the information contained herein. The author(s) may not | |
16 | .\" have taken the same level of care in the production of this manual, | |
17 | .\" which is licensed free of charge, as they might when working | |
18 | .\" professionally. | |
c13182ef | 19 | .\" |
fea681da MK |
20 | .\" Formatted or processed versions of this manual, if unaccompanied by |
21 | .\" the source, must acknowledge the copyright and authors of this work. | |
22 | .\" | |
23 | .\" Modified Tue Oct 22 17:54:56 1996 by Eric S. Raymond <esr@thyrsus.com> | |
24 | .\" Modified 1 Jan 2002, Martin Schulze <joey@infodrom.org> | |
c11b1abf MK |
25 | .\" Modified 4 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com> |
26 | .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com> | |
fea681da | 27 | .\" Added notes on capability requirements |
c11b1abf | 28 | .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com> |
c952e226 MK |
29 | .\" Language and formatting clean-ups |
30 | .\" Added notes on /proc files | |
d9bfdb9c | 31 | .\" Rewrote BUGS note about semget()'s failure to initialize |
c952e226 | 32 | .\" semaphore values |
fea681da | 33 | .\" |
f6652463 | 34 | .TH SEMGET 2 2012-05-31 "Linux" "Linux Programmer's Manual" |
fea681da MK |
35 | .SH NAME |
36 | semget \- get a semaphore set identifier | |
37 | .SH SYNOPSIS | |
38 | .nf | |
2fadbfb5 MK |
39 | .B #include <sys/types.h> |
40 | .B #include <sys/ipc.h> | |
41 | .B #include <sys/sem.h> | |
fea681da MK |
42 | .fi |
43 | .sp | |
44 | .BI "int semget(key_t " key , | |
45 | .BI "int " nsems , | |
46 | .BI "int " semflg ); | |
47 | .SH DESCRIPTION | |
c952e226 MK |
48 | The |
49 | .BR semget () | |
cfadad46 | 50 | system call returns the semaphore set identifier |
fea681da MK |
51 | associated with the argument |
52 | .IR key . | |
53 | A new set of | |
54 | .I nsems | |
55 | semaphores is created if | |
56 | .I key | |
57 | has the value | |
58 | .B IPC_PRIVATE | |
540036b2 | 59 | or if no existing semaphore set is associated with |
fea681da MK |
60 | .I key |
61 | and | |
62 | .B IPC_CREAT | |
c952e226 MK |
63 | is specified in |
64 | .IR semflg . | |
fea681da | 65 | .PP |
c952e226 | 66 | If |
fea681da | 67 | .I semflg |
c952e226 | 68 | specifies both |
fea681da MK |
69 | .B IPC_CREAT |
70 | and | |
71 | .B IPC_EXCL | |
72 | and a semaphore set already exists for | |
c952e226 MK |
73 | .IR key , |
74 | then | |
75 | .BR semget () | |
c13182ef | 76 | fails with |
c952e226 MK |
77 | .I errno |
78 | set to | |
79 | .BR EEXIST . | |
c13182ef | 80 | (This is analogous to the effect of the combination |
c952e226 MK |
81 | .B O_CREAT | O_EXCL |
82 | for | |
83 | .BR open (2).) | |
fea681da | 84 | .PP |
c952e226 | 85 | Upon creation, the least significant 9 bits of the argument |
fea681da | 86 | .I semflg |
c952e226 | 87 | define the permissions (for owner, group and others) |
fea681da MK |
88 | for the semaphore set. |
89 | These bits have the same format, and the same | |
c952e226 MK |
90 | meaning, as the |
91 | .I mode | |
92 | argument of | |
fea681da | 93 | .BR open (2) |
c952e226 | 94 | (though the execute permissions are |
fea681da MK |
95 | not meaningful for semaphores, and write permissions mean permission |
96 | to alter semaphore values). | |
97 | .PP | |
2c5e151c | 98 | The values of the semaphores in a newly created set are indeterminate. |
c13182ef | 99 | (POSIX.1-2001 is explicit on this point.) |
2c5e151c | 100 | Although Linux, like many other implementations, |
d9bfdb9c | 101 | initializes the semaphore values to 0, |
2c5e151c | 102 | a portable application cannot rely on this: |
d9bfdb9c | 103 | it should explicitly initialize the semaphores to the desired values. |
c13182ef MK |
104 | .\" In truth, every one of the many implementations that I've tested sets |
105 | .\" the values to zero, but I suppose there is/was some obscure | |
2c5e151c MK |
106 | .\" implementation out there that does not. |
107 | .PP | |
fea681da | 108 | When creating a new semaphore set, |
c952e226 | 109 | .BR semget () |
d9bfdb9c | 110 | initializes the set's associated data structure, |
c952e226 | 111 | .I semid_ds |
c13182ef | 112 | (see |
2c5e151c | 113 | .BR semctl (2)), |
fea681da MK |
114 | as follows: |
115 | .IP | |
c952e226 | 116 | .I sem_perm.cuid |
fea681da | 117 | and |
c952e226 | 118 | .I sem_perm.uid |
499d62f2 | 119 | are set to the effective user ID of the calling process. |
fea681da | 120 | .IP |
c952e226 | 121 | .I sem_perm.cgid |
fea681da | 122 | and |
c952e226 | 123 | .I sem_perm.gid |
499d62f2 | 124 | are set to the effective group ID of the calling process. |
fea681da | 125 | .IP |
c952e226 MK |
126 | The least significant 9 bits of |
127 | .I sem_perm.mode | |
128 | are set to the least significant 9 bits of | |
fea681da MK |
129 | .IR semflg . |
130 | .IP | |
c952e226 | 131 | .I sem_nsems |
fea681da MK |
132 | is set to the value of |
133 | .IR nsems . | |
134 | .IP | |
c952e226 | 135 | .I sem_otime |
fea681da MK |
136 | is set to 0. |
137 | .IP | |
c952e226 | 138 | .I sem_ctime |
fea681da MK |
139 | is set to the current time. |
140 | .PP | |
141 | The argument | |
142 | .I nsems | |
c952e226 | 143 | can be 0 |
fea681da MK |
144 | (a don't care) |
145 | when a semaphore set is not being created. | |
146 | Otherwise | |
147 | .I nsems | |
c952e226 | 148 | must be greater than 0 |
fea681da MK |
149 | and less than or equal to the maximum number of semaphores per semaphore set |
150 | .RB ( SEMMSL ). | |
151 | .PP | |
c952e226 | 152 | If the semaphore set already exists, the permissions are |
fea681da MK |
153 | verified. |
154 | .\" and a check is made to see if it is marked for destruction. | |
47297adb | 155 | .SH RETURN VALUE |
fea681da | 156 | If successful, the return value will be the semaphore set identifier |
2fda57bd | 157 | (a nonnegative integer), otherwise \-1 |
fea681da MK |
158 | is returned, with |
159 | .I errno | |
160 | indicating the error. | |
161 | .SH ERRORS | |
162 | On failure | |
163 | .I errno | |
164 | will be set to one of the following: | |
89b3c6b8 | 165 | .TP |
fea681da MK |
166 | .B EACCES |
167 | A semaphore set exists for | |
168 | .IR key , | |
169 | but the calling process does not have permission to access the set, | |
170 | and does not have the | |
0daa9e92 | 171 | .B CAP_IPC_OWNER |
fea681da MK |
172 | capability. |
173 | .TP | |
174 | .B EEXIST | |
175 | A semaphore set exists for | |
66ee0c7e | 176 | .I key |
fea681da MK |
177 | and |
178 | .I semflg | |
c952e226 | 179 | specified both |
fea681da MK |
180 | .B IPC_CREAT |
181 | and | |
182 | .BR IPC_EXCL . | |
183 | .\" .TP | |
184 | .\" .B EIDRM | |
185 | .\" The semaphore set is marked to be deleted. | |
186 | .TP | |
187 | .B EINVAL | |
0daa9e92 | 188 | .I nsems |
fea681da MK |
189 | is less than 0 or greater than the limit on the number |
190 | of semaphores per semaphore set | |
191 | .RB ( SEMMSL ), | |
192 | or a semaphore set corresponding to | |
193 | .I key | |
194 | already exists, and | |
195 | .I nsems | |
196 | is larger than the number of semaphores in that set. | |
197 | .TP | |
198 | .B ENOENT | |
199 | No semaphore set exists for | |
200 | .I key | |
201 | and | |
202 | .I semflg | |
c952e226 | 203 | did not specify |
fea681da MK |
204 | .BR IPC_CREAT . |
205 | .TP | |
206 | .B ENOMEM | |
c952e226 MK |
207 | A semaphore set has to be created but the system does not have |
208 | enough memory for the new data structure. | |
fea681da MK |
209 | .TP |
210 | .B ENOSPC | |
211 | A semaphore set has to be created but the system limit for the maximum | |
212 | number of semaphore sets | |
213 | .RB ( SEMMNI ), | |
214 | or the system wide maximum number of semaphores | |
215 | .RB ( SEMMNS ), | |
216 | would be exceeded. | |
47297adb | 217 | .SH CONFORMING TO |
a1d5f77c MK |
218 | SVr4, POSIX.1-2001. |
219 | .\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN, | |
220 | .\" ERANGE, EFAULT. | |
fea681da | 221 | .SH NOTES |
f6652463 MK |
222 | The inclusion of |
223 | .I <sys/types.h> | |
224 | and | |
225 | .I <sys/ipc.h> | |
226 | isn't required on Linux or by any version of POSIX. | |
227 | However, | |
228 | some old implementations required the inclusion of these header files, | |
229 | and the SVID also documented their inclusion. | |
230 | Applications intended to be portable to such old systems may need | |
231 | to include these header files. | |
232 | .\" Like Linux, the FreeBSD man pages still document | |
233 | .\" the inclusion of these header files. | |
234 | ||
fea681da MK |
235 | .B IPC_PRIVATE |
236 | isn't a flag field but a | |
c952e226 | 237 | .I key_t |
fea681da MK |
238 | type. |
239 | If this special value is used for | |
240 | .IR key , | |
c952e226 | 241 | the system call ignores everything but the least significant 9 bits of |
fea681da MK |
242 | .I semflg |
243 | and creates a new semaphore set (on success). | |
244 | .PP | |
540036b2 | 245 | The following limits on semaphore set resources affect the |
c952e226 | 246 | .BR semget () |
fea681da | 247 | call: |
89b3c6b8 | 248 | .TP |
fea681da | 249 | .B SEMMNI |
c952e226 MK |
250 | System wide maximum number of semaphore sets: policy dependent |
251 | (on Linux, this limit can be read and modified via the fourth field of | |
252 | .IR /proc/sys/kernel/sem ). | |
253 | .\" This /proc file is not available in Linux 2.2 and earlier -- MTK | |
fea681da MK |
254 | .TP |
255 | .B SEMMSL | |
256 | Maximum number of semaphores per semid: implementation dependent | |
c952e226 MK |
257 | (on Linux, this limit can be read and modified via the first field of |
258 | .IR /proc/sys/kernel/sem ). | |
fea681da MK |
259 | .TP |
260 | .B SEMMNS | |
c952e226 MK |
261 | System wide maximum number of semaphores: policy dependent |
262 | (on Linux, this limit can be read and modified via the second field of | |
263 | .IR /proc/sys/kernel/sem ). | |
fea681da MK |
264 | Values greater than |
265 | .B SEMMSL * SEMMNI | |
266 | makes it irrelevant. | |
267 | .SH BUGS | |
682edefb MK |
268 | The name choice |
269 | .B IPC_PRIVATE | |
9091595d MK |
270 | was perhaps unfortunate, |
271 | .B IPC_NEW | |
fea681da MK |
272 | would more clearly show its function. |
273 | .LP | |
d9bfdb9c | 274 | The semaphores in a set are not initialized by |
c952e226 | 275 | .BR semget (). |
d9bfdb9c | 276 | .\" In fact they are initialized to zero on Linux, but POSIX.1-2001 |
704a18f0 | 277 | .\" does not specify this, and we can't portably rely on it. |
d9bfdb9c | 278 | In order to initialize the semaphores, |
fea681da | 279 | .BR semctl (2) |
c952e226 | 280 | must be used to perform a |
fea681da MK |
281 | .B SETVAL |
282 | or a | |
283 | .B SETALL | |
c952e226 MK |
284 | operation on the semaphore set. |
285 | (Where multiple peers do not know who will be the first to | |
c7094399 | 286 | initialize the set, checking for a nonzero |
c952e226 MK |
287 | .I sem_otime |
288 | in the associated data structure retrieved by a | |
0bfa087b | 289 | .BR semctl (2) |
c952e226 MK |
290 | .B IPC_STAT |
291 | operation can be used to avoid races.) | |
47297adb | 292 | .SH SEE ALSO |
fea681da MK |
293 | .BR semctl (2), |
294 | .BR semop (2), | |
295 | .BR ftok (3), | |
f675ea37 | 296 | .BR capabilities (7), |
2c5e151c MK |
297 | .BR sem_overview (7), |
298 | .BR svipc (7) |