]>
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> | |
305a0578 MK |
25 | .\" Modified 4 Jan 2002, Michael Kerrisk <mtk-manpages@gmx.net> |
26 | .\" Modified, 27 May 2004, Michael Kerrisk <mtk-manpages@gmx.net> | |
fea681da | 27 | .\" Added notes on capability requirements |
c952e226 MK |
28 | .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk-manpages@gmx.net> |
29 | .\" Language and formatting clean-ups | |
30 | .\" Added notes on /proc files | |
704a18f0 | 31 | .\" Rewrote BUGS note about semget()'s failure to initialise |
c952e226 | 32 | .\" semaphore values |
fea681da MK |
33 | .\" |
34 | .TH SEMGET 2 2004-05-27 "Linux 2.6.6" "Linux Programmer's Manual" | |
35 | .SH NAME | |
36 | semget \- get a semaphore set identifier | |
37 | .SH SYNOPSIS | |
38 | .nf | |
39 | .B | |
40 | #include <sys/types.h> | |
41 | .B | |
42 | #include <sys/ipc.h> | |
43 | .B | |
44 | #include <sys/sem.h> | |
45 | .fi | |
46 | .sp | |
47 | .BI "int semget(key_t " key , | |
48 | .BI "int " nsems , | |
49 | .BI "int " semflg ); | |
50 | .SH DESCRIPTION | |
c952e226 MK |
51 | The |
52 | .BR semget () | |
53 | system call returns the semaphore set identifier | |
fea681da MK |
54 | associated with the argument |
55 | .IR key . | |
56 | A new set of | |
57 | .I nsems | |
58 | semaphores is created if | |
59 | .I key | |
60 | has the value | |
61 | .B IPC_PRIVATE | |
540036b2 | 62 | or if no existing semaphore set is associated with |
fea681da MK |
63 | .I key |
64 | and | |
65 | .B IPC_CREAT | |
c952e226 MK |
66 | is specified in |
67 | .IR semflg . | |
fea681da | 68 | .PP |
c952e226 | 69 | If |
fea681da | 70 | .I semflg |
c952e226 | 71 | specifies both |
fea681da MK |
72 | .B IPC_CREAT |
73 | and | |
74 | .B IPC_EXCL | |
75 | and a semaphore set already exists for | |
c952e226 MK |
76 | .IR key , |
77 | then | |
78 | .BR semget () | |
c13182ef | 79 | fails with |
c952e226 MK |
80 | .I errno |
81 | set to | |
82 | .BR EEXIST . | |
c13182ef | 83 | (This is analogous to the effect of the combination |
c952e226 MK |
84 | .B O_CREAT | O_EXCL |
85 | for | |
86 | .BR open (2).) | |
fea681da | 87 | .PP |
c952e226 | 88 | Upon creation, the least significant 9 bits of the argument |
fea681da | 89 | .I semflg |
c952e226 | 90 | define the permissions (for owner, group and others) |
fea681da MK |
91 | for the semaphore set. |
92 | These bits have the same format, and the same | |
c952e226 MK |
93 | meaning, as the |
94 | .I mode | |
95 | argument of | |
fea681da | 96 | .BR open (2) |
c952e226 | 97 | (though the execute permissions are |
fea681da MK |
98 | not meaningful for semaphores, and write permissions mean permission |
99 | to alter semaphore values). | |
100 | .PP | |
2c5e151c | 101 | The values of the semaphores in a newly created set are indeterminate. |
c13182ef | 102 | (POSIX.1-2001 is explicit on this point.) |
2c5e151c MK |
103 | Although Linux, like many other implementations, |
104 | initialises the semaphore values to 0, | |
105 | a portable application cannot rely on this: | |
106 | it should explicitly initialise the semaphores to the desired values. | |
c13182ef MK |
107 | .\" In truth, every one of the many implementations that I've tested sets |
108 | .\" the values to zero, but I suppose there is/was some obscure | |
2c5e151c MK |
109 | .\" implementation out there that does not. |
110 | .PP | |
fea681da | 111 | When creating a new semaphore set, |
c952e226 | 112 | .BR semget () |
2c5e151c | 113 | initialises the set's associated data structure, |
c952e226 | 114 | .I semid_ds |
c13182ef | 115 | (see |
2c5e151c | 116 | .BR semctl (2)), |
fea681da MK |
117 | as follows: |
118 | .IP | |
c952e226 | 119 | .I sem_perm.cuid |
fea681da | 120 | and |
c952e226 | 121 | .I sem_perm.uid |
499d62f2 | 122 | are set to the effective user ID of the calling process. |
fea681da | 123 | .IP |
c952e226 | 124 | .I sem_perm.cgid |
fea681da | 125 | and |
c952e226 | 126 | .I sem_perm.gid |
499d62f2 | 127 | are set to the effective group ID of the calling process. |
fea681da | 128 | .IP |
c952e226 MK |
129 | The least significant 9 bits of |
130 | .I sem_perm.mode | |
131 | are set to the least significant 9 bits of | |
fea681da MK |
132 | .IR semflg . |
133 | .IP | |
c952e226 | 134 | .I sem_nsems |
fea681da MK |
135 | is set to the value of |
136 | .IR nsems . | |
137 | .IP | |
c952e226 | 138 | .I sem_otime |
fea681da MK |
139 | is set to 0. |
140 | .IP | |
c952e226 | 141 | .I sem_ctime |
fea681da MK |
142 | is set to the current time. |
143 | .PP | |
144 | The argument | |
145 | .I nsems | |
c952e226 | 146 | can be 0 |
fea681da MK |
147 | (a don't care) |
148 | when a semaphore set is not being created. | |
149 | Otherwise | |
150 | .I nsems | |
c952e226 | 151 | must be greater than 0 |
fea681da MK |
152 | and less than or equal to the maximum number of semaphores per semaphore set |
153 | .RB ( SEMMSL ). | |
154 | .PP | |
c952e226 | 155 | If the semaphore set already exists, the permissions are |
fea681da MK |
156 | verified. |
157 | .\" and a check is made to see if it is marked for destruction. | |
158 | .SH "RETURN VALUE" | |
159 | If successful, the return value will be the semaphore set identifier | |
18be658d | 160 | (a non-negative integer), otherwise \-1 |
fea681da MK |
161 | is returned, with |
162 | .I errno | |
163 | indicating the error. | |
164 | .SH ERRORS | |
165 | On failure | |
166 | .I errno | |
167 | will be set to one of the following: | |
168 | .TP 11 | |
169 | .B EACCES | |
170 | A semaphore set exists for | |
171 | .IR key , | |
172 | but the calling process does not have permission to access the set, | |
173 | and does not have the | |
174 | .BR CAP_IPC_OWNER | |
175 | capability. | |
176 | .TP | |
177 | .B EEXIST | |
178 | A semaphore set exists for | |
179 | .B key | |
180 | and | |
181 | .I semflg | |
c952e226 | 182 | specified both |
fea681da MK |
183 | .B IPC_CREAT |
184 | and | |
185 | .BR IPC_EXCL . | |
186 | .\" .TP | |
187 | .\" .B EIDRM | |
188 | .\" The semaphore set is marked to be deleted. | |
189 | .TP | |
190 | .B EINVAL | |
191 | .IR nsems | |
192 | is less than 0 or greater than the limit on the number | |
193 | of semaphores per semaphore set | |
194 | .RB ( SEMMSL ), | |
195 | or a semaphore set corresponding to | |
196 | .I key | |
197 | already exists, and | |
198 | .I nsems | |
199 | is larger than the number of semaphores in that set. | |
200 | .TP | |
201 | .B ENOENT | |
202 | No semaphore set exists for | |
203 | .I key | |
204 | and | |
205 | .I semflg | |
c952e226 | 206 | did not specify |
fea681da MK |
207 | .BR IPC_CREAT . |
208 | .TP | |
209 | .B ENOMEM | |
c952e226 MK |
210 | A semaphore set has to be created but the system does not have |
211 | enough memory for the new data structure. | |
fea681da MK |
212 | .TP |
213 | .B ENOSPC | |
214 | A semaphore set has to be created but the system limit for the maximum | |
215 | number of semaphore sets | |
216 | .RB ( SEMMNI ), | |
217 | or the system wide maximum number of semaphores | |
218 | .RB ( SEMMNS ), | |
219 | would be exceeded. | |
220 | .SH NOTES | |
221 | .B IPC_PRIVATE | |
222 | isn't a flag field but a | |
c952e226 | 223 | .I key_t |
fea681da MK |
224 | type. |
225 | If this special value is used for | |
226 | .IR key , | |
c952e226 | 227 | the system call ignores everything but the least significant 9 bits of |
fea681da MK |
228 | .I semflg |
229 | and creates a new semaphore set (on success). | |
230 | .PP | |
540036b2 | 231 | The following limits on semaphore set resources affect the |
c952e226 | 232 | .BR semget () |
fea681da MK |
233 | call: |
234 | .TP 11 | |
235 | .B SEMMNI | |
c952e226 MK |
236 | System wide maximum number of semaphore sets: policy dependent |
237 | (on Linux, this limit can be read and modified via the fourth field of | |
238 | .IR /proc/sys/kernel/sem ). | |
239 | .\" This /proc file is not available in Linux 2.2 and earlier -- MTK | |
fea681da MK |
240 | .TP |
241 | .B SEMMSL | |
242 | Maximum number of semaphores per semid: implementation dependent | |
c952e226 MK |
243 | (on Linux, this limit can be read and modified via the first field of |
244 | .IR /proc/sys/kernel/sem ). | |
fea681da MK |
245 | .TP |
246 | .B SEMMNS | |
c952e226 MK |
247 | System wide maximum number of semaphores: policy dependent |
248 | (on Linux, this limit can be read and modified via the second field of | |
249 | .IR /proc/sys/kernel/sem ). | |
fea681da MK |
250 | Values greater than |
251 | .B SEMMSL * SEMMNI | |
252 | makes it irrelevant. | |
253 | .SH BUGS | |
254 | The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW | |
255 | would more clearly show its function. | |
256 | .LP | |
c952e226 MK |
257 | The semaphores in a set are not initialised by |
258 | .BR semget (). | |
c13182ef | 259 | .\" In fact they are initialised to zero on Linux, but POSIX.1-2001 |
704a18f0 | 260 | .\" does not specify this, and we can't portably rely on it. |
c952e226 | 261 | In order to initialise the semaphores, |
fea681da | 262 | .BR semctl (2) |
c952e226 | 263 | must be used to perform a |
fea681da MK |
264 | .B SETVAL |
265 | or a | |
266 | .B SETALL | |
c952e226 MK |
267 | operation on the semaphore set. |
268 | (Where multiple peers do not know who will be the first to | |
269 | initialise the set, checking for a non-zero | |
270 | .I sem_otime | |
271 | in the associated data structure retrieved by a | |
0bfa087b | 272 | .BR semctl (2) |
c952e226 MK |
273 | .B IPC_STAT |
274 | operation can be used to avoid races.) | |
fea681da | 275 | .SH "CONFORMING TO" |
97c1eac8 MK |
276 | SVr4, POSIX.1-2001. |
277 | .\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN, | |
278 | .\" ERANGE, EFAULT. | |
fea681da MK |
279 | .SH "SEE ALSO" |
280 | .BR semctl (2), | |
281 | .BR semop (2), | |
282 | .BR ftok (3), | |
f675ea37 | 283 | .BR capabilities (7), |
2c5e151c MK |
284 | .BR sem_overview (7), |
285 | .BR svipc (7) |