1SEMCTL(2) Linux Programmer's Manual SEMCTL(2)
2
3
4
6 semctl - semaphore control operations
7
9 #include <sys/types.h>
10 #include <sys/ipc.h>
11 #include <sys/sem.h>
12
13 int semctl(int semid, int semnum, int cmd, ...);
14
16 semctl() performs the control operation specified by cmd on the sema‐
17 phore set identified by semid, or on the semnum-th semaphore of that
18 set. (The semaphores in a set are numbered starting at 0.)
19
20 This function has three or four arguments, depending on cmd. When
21 there are four, the fourth has the type union semun. The calling pro‐
22 gram must define this union as follows:
23
24 union semun {
25 int val; /* Value for SETVAL */
26 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
27 unsigned short *array; /* Array for GETALL, SETALL */
28 struct seminfo *__buf; /* Buffer for IPC_INFO
29 (Linux-specific) */
30 };
31
32 The semid_ds data structure is defined in <sys/sem.h> as follows:
33
34 struct semid_ds {
35 struct ipc_perm sem_perm; /* Ownership and permissions */
36 time_t sem_otime; /* Last semop time */
37 time_t sem_ctime; /* Last change time */
38 unsigned short sem_nsems; /* No. of semaphores in set */
39 };
40
41 The ipc_perm structure is defined in <sys/ipc.h> as follows (the high‐
42 lighted fields are settable using IPC_SET):
43
44 struct ipc_perm {
45 key_t __key; /* Key supplied to semget(2) */
46 uid_t uid; /* Effective UID of owner */
47 gid_t gid; /* Effective GID of owner */
48 uid_t cuid; /* Effective UID of creator */
49 gid_t cgid; /* Effective GID of creator */
50 unsigned short mode; /* Permissions */
51 unsigned short __seq; /* Sequence number */
52 };
53
54 Valid values for cmd are:
55
56 IPC_STAT Copy information from the kernel data structure associated
57 with semid into the semid_ds structure pointed to by arg.buf.
58 The argument semnum is ignored. The calling process must
59 have read permission on the semaphore set.
60
61 IPC_SET Write the values of some members of the semid_ds structure
62 pointed to by arg.buf to the kernel data structure associated
63 with this semaphore set, updating also its sem_ctime member.
64 The following members of the structure are updated:
65 sem_perm.uid, sem_perm.gid, and (the least significant 9 bits
66 of) sem_perm.mode. The effective UID of the calling process
67 must match the owner (sem_perm.uid) or creator
68 (sem_perm.cuid) of the semaphore set, or the caller must be
69 privileged. The argument semnum is ignored.
70
71 IPC_RMID Immediately remove the semaphore set, awakening all processes
72 blocked in semop(2) calls on the set (with an error return
73 and errno set to EIDRM). The effective user ID of the call‐
74 ing process must match the creator or owner of the semaphore
75 set, or the caller must be privileged. The argument semnum
76 is ignored.
77
78 IPC_INFO (Linux-specific)
79 Returns information about system-wide semaphore limits and
80 parameters in the structure pointed to by arg.__buf. This
81 structure is of type seminfo, defined in <sys/sem.h> if the
82 _GNU_SOURCE feature test macro is defined:
83
84 struct seminfo {
85 int semmap; /* Number of entries in semaphore
86 map; unused within kernel */
87 int semmni; /* Maximum number of semaphore sets */
88 int semmns; /* Maximum number of semaphores in all
89 semaphore sets */
90 int semmnu; /* System-wide maximum number of undo
91 structures; unused within kernel */
92 int semmsl; /* Maximum number of semaphores in a
93 set */
94 int semopm; /* Maximum number of operations for
95 semop(2) */
96 int semume; /* Maximum number of undo entries per
97 process; unused within kernel */
98 int semusz; /* Size of struct sem_undo */
99 int semvmx; /* Maximum semaphore value */
100 int semaem; /* Max. value that can be recorded for
101 semaphore adjustment (SEM_UNDO) */
102 };
103
104 The semmsl, semmns, semopm, and semmni settings can be
105 changed via /proc/sys/kernel/sem; see proc(5) for details.
106
107 SEM_INFO (Linux-specific)
108 Returns a seminfo structure containing the same information
109 as for IPC_INFO, except that the following fields are
110 returned with information about system resources consumed by
111 semaphores: the semusz field returns the number of semaphore
112 sets that currently exist on the system; and the semaem field
113 returns the total number of semaphores in all semaphore sets
114 on the system.
115
116 SEM_STAT (Linux-specific)
117 Returns a semid_ds structure as for IPC_STAT. However, the
118 semid argument is not a semaphore identifier, but instead an
119 index into the kernel's internal array that maintains infor‐
120 mation about all semaphore sets on the system.
121
122 GETALL Return semval (i.e., the current value) for all semaphores of
123 the set into arg.array. The argument semnum is ignored. The
124 calling process must have read permission on the semaphore
125 set.
126
127 GETNCNT The system call returns the value of semncnt for the sem‐
128 num-th semaphore of the set (i.e., the number of processes
129 waiting for an increase of semval for the semnum-th semaphore
130 of the set). The calling process must have read permission
131 on the semaphore set.
132
133 GETPID The system call returns the value of sempid for the semnum-th
134 semaphore of the set (i.e., the PID of the process that exe‐
135 cuted the last semop(2) call for the semnum-th semaphore of
136 the set). The calling process must have read permission on
137 the semaphore set.
138
139 GETVAL The system call returns the value of semval for the semnum-th
140 semaphore of the set. The calling process must have read
141 permission on the semaphore set.
142
143 GETZCNT The system call returns the value of semzcnt for the sem‐
144 num-th semaphore of the set (i.e., the number of processes
145 waiting for semval of the semnum-th semaphore of the set to
146 become 0). The calling process must have read permission on
147 the semaphore set.
148
149 SETALL Set semval for all semaphores of the set using arg.array,
150 updating also the sem_ctime member of the semid_ds structure
151 associated with the set. Undo entries (see semop(2)) are
152 cleared for altered semaphores in all processes. If the
153 changes to semaphore values would permit blocked semop(2)
154 calls in other processes to proceed, then those processes are
155 woken up. The argument semnum is ignored. The calling
156 process must have alter (write) permission on the semaphore
157 set.
158
159 SETVAL Set the value of semval to arg.val for the semnum-th sema‐
160 phore of the set, updating also the sem_ctime member of the
161 semid_ds structure associated with the set. Undo entries are
162 cleared for altered semaphores in all processes. If the
163 changes to semaphore values would permit blocked semop(2)
164 calls in other processes to proceed, then those processes are
165 woken up. The calling process must have alter permission on
166 the semaphore set.
167
169 On failure semctl() returns -1 with errno indicating the error.
170
171 Otherwise the system call returns a nonnegative value depending on cmd
172 as follows:
173
174 GETNCNT the value of semncnt.
175
176 GETPID the value of sempid.
177
178 GETVAL the value of semval.
179
180 GETZCNT the value of semzcnt.
181
182 IPC_INFO the index of the highest used entry in the kernel's inter‐
183 nal array recording information about all semaphore sets.
184 (This information can be used with repeated SEM_STAT opera‐
185 tions to obtain information about all semaphore sets on the
186 system.)
187
188 SEM_INFO As for IPC_INFO.
189
190 SEM_STAT the identifier of the semaphore set whose index was given
191 in semid.
192
193 All other cmd values return 0 on success.
194
196 On failure, errno will be set to one of the following:
197
198 EACCES The argument cmd has one of the values GETALL, GETPID, GETVAL,
199 GETNCNT, GETZCNT, IPC_STAT, SEM_STAT, SETALL, or SETVAL and the
200 calling process does not have the required permissions on the
201 semaphore set and does not have the CAP_IPC_OWNER capability.
202
203 EFAULT The address pointed to by arg.buf or arg.array isn't accessible.
204
205 EIDRM The semaphore set was removed.
206
207 EINVAL Invalid value for cmd or semid. Or: for a SEM_STAT operation,
208 the index value specified in semid referred to an array slot
209 that is currently unused.
210
211 EPERM The argument cmd has the value IPC_SET or IPC_RMID but the
212 effective user ID of the calling process is not the creator (as
213 found in sem_perm.cuid) or the owner (as found in sem_perm.uid)
214 of the semaphore set, and the process does not have the
215 CAP_SYS_ADMIN capability.
216
217 ERANGE The argument cmd has the value SETALL or SETVAL and the value to
218 which semval is to be set (for some semaphore of the set) is
219 less than 0 or greater than the implementation limit SEMVMX.
220
222 SVr4, POSIX.1-2001.
223
225 The IPC_INFO, SEM_STAT and SEM_INFO operations are used by the ipcs(1)
226 program to provide information on allocated resources. In the future
227 these may modified or moved to a /proc file system interface.
228
229 Various fields in a struct semid_ds were typed as short under Linux 2.2
230 and have become long under Linux 2.4. To take advantage of this, a
231 recompilation under glibc-2.1.91 or later should suffice. (The kernel
232 distinguishes old and new calls by an IPC_64 flag in cmd.)
233
234 In some earlier versions of glibc, the semun union was defined in
235 <sys/sem.h>, but POSIX.1-2001 requires that the caller define this
236 union. On versions of glibc where this union is not defined, the macro
237 _SEM_SEMUN_UNDEFINED is defined in <sys/sem.h>.
238
239 The following system limit on semaphore sets affects a semctl() call:
240
241 SEMVMX Maximum value for semval: implementation dependent (32767).
242
243 For greater portability it is best to always call semctl() with four
244 arguments.
245
247 ipc(2), semget(2), semop(2), capabilities(7), sem_overview(7), svipc(7)
248
250 This page is part of release 3.25 of the Linux man-pages project. A
251 description of the project, and information about reporting bugs, can
252 be found at http://www.kernel.org/doc/man-pages/.
253
254
255
256Linux 2009-09-27 SEMCTL(2)