1SEMCTL(2)                  Linux Programmer's Manual                 SEMCTL(2)
2
3
4

NAME

6       semctl - semaphore control operations
7

SYNOPSIS

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

DESCRIPTION

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 (i.e., the  num‐
128                 ber  of  processes waiting for the value of this semaphore to
129                 increase) for the semnum-th semaphore of the set  (i.e.,  the
130                 number of processes waiting for an increase of semval for the
131                 semnum-th semaphore of the set).  The  calling  process  must
132                 have read permission on the semaphore set.
133
134       GETPID    The system call returns the value of sempid for the semnum-th
135                 semaphore of the set (i.e., the PID of the process that  exe‐
136                 cuted  the  last semop(2) call for the semnum-th semaphore of
137                 the set).  The calling process must have read  permission  on
138                 the semaphore set.
139
140       GETVAL    The system call returns the value of semval for the semnum-th
141                 semaphore of the set.  The calling  process  must  have  read
142                 permission on the semaphore set.
143
144       GETZCNT   The  system call returns the value of semzcnt (i.e., the num‐
145                 ber of processes waiting for the value of this  semaphore  to
146                 become  zero)  for  the semnum-th semaphore of the set (i.e.,
147                 the number of processes waiting for semval of  the  semnum-th
148                 semaphore  of the set to become 0).  The calling process must
149                 have read permission on the semaphore set.
150
151       SETALL    Set semval for all semaphores of  the  set  using  arg.array,
152                 updating  also the sem_ctime member of the semid_ds structure
153                 associated with the set.  Undo  entries  (see  semop(2))  are
154                 cleared  for  altered  semaphores  in  all processes.  If the
155                 changes to semaphore values  would  permit  blocked  semop(2)
156                 calls in other processes to proceed, then those processes are
157                 woken up.  The  argument  semnum  is  ignored.   The  calling
158                 process  must  have alter (write) permission on the semaphore
159                 set.
160
161       SETVAL    Set the value of semval to arg.val for  the  semnum-th  sema‐
162                 phore  of  the set, updating also the sem_ctime member of the
163                 semid_ds structure associated with the set.  Undo entries are
164                 cleared  for  altered  semaphores  in  all processes.  If the
165                 changes to semaphore values  would  permit  blocked  semop(2)
166                 calls in other processes to proceed, then those processes are
167                 woken up.  The calling process must have alter permission  on
168                 the semaphore set.
169

RETURN VALUE

171       On failure semctl() returns -1 with errno indicating the error.
172
173       Otherwise the system call returns a non-negative value depending on cmd
174       as follows:
175
176       GETNCNT     the value of semncnt.
177
178       GETPID      the value of sempid.
179
180       GETVAL      the value of semval.
181
182       GETZCNT     the value of semzcnt.
183
184       IPC_INFO    the index of the highest used entry in the kernel's  inter‐
185                   nal  array  recording information about all semaphore sets.
186                   (This information can be used with repeated SEM_STAT opera‐
187                   tions to obtain information about all semaphore sets on the
188                   system.)
189
190       SEM_INFO    As for IPC_INFO.
191
192       SEM_STAT    the identifier of the semaphore set whose index  was  given
193                   in semid.
194
195       All other cmd values return 0 on success.
196

ERRORS

198       On failure, errno will be set to one of the following:
199
200       EACCES The  argument  cmd has one of the values GETALL, GETPID, GETVAL,
201              GETNCNT, GETZCNT, IPC_STAT, SEM_STAT, SETALL, or SETVAL and  the
202              calling  process  does  not have the required permissions on the
203              semaphore set and does not have the CAP_IPC_OWNER capability.
204
205       EFAULT The address pointed to by arg.buf or arg.array isn't accessible.
206
207       EIDRM  The semaphore set was removed.
208
209       EINVAL Invalid value for cmd or semid.  Or: for a  SEM_STAT  operation,
210              the  index  value  specified  in semid referred to an array slot
211              that is currently unused.
212
213       EPERM  The argument cmd has the  value  IPC_SET  or  IPC_RMID  but  the
214              effective  user ID of the calling process is not the creator (as
215              found in sem_perm.cuid) or the owner (as found in  sem_perm.uid)
216              of  the  semaphore  set,  and  the  process  does  not  have the
217              CAP_SYS_ADMIN capability.
218
219       ERANGE The argument cmd has the value SETALL or SETVAL and the value to
220              which  semval  is  to  be set (for some semaphore of the set) is
221              less than 0 or greater than the implementation limit SEMVMX.
222

CONFORMING TO

224       SVr4, POSIX.1-2001.
225

NOTES

227       The IPC_INFO, SEM_STAT and SEM_INFO operations are used by the  ipcs(8)
228       program  to  provide information on allocated resources.  In the future
229       these may modified or moved to a /proc file system interface.
230
231       Various fields in a struct semid_ds were typed as short under Linux 2.2
232       and  have  become  long  under Linux 2.4.  To take advantage of this, a
233       recompilation under glibc-2.1.91 or later should suffice.  (The  kernel
234       distinguishes old and new calls by an IPC_64 flag in cmd.)
235
236       In  some  earlier  versions  of  glibc,  the semun union was defined in
237       <sys/sem.h>, but POSIX.1-2001 requires  that  the  caller  define  this
238       union.  On versions of glibc where this union is not defined, the macro
239       _SEM_SEMUN_UNDEFINED is defined in <sys/sem.h>.
240
241       The following system limit on semaphore sets affects a semctl() call:
242
243       SEMVMX Maximum value for semval: implementation dependent (32767).
244
245       For greater portability it is best to always call  semctl()  with  four
246       arguments.
247

SEE ALSO

249       ipc(2), semget(2), semop(2), capabilities(7), sem_overview(7), svipc(7)
250

COLOPHON

252       This  page  is  part of release 3.22 of the Linux man-pages project.  A
253       description of the project, and information about reporting  bugs,  can
254       be found at http://www.kernel.org/doc/man-pages/.
255
256
257
258Linux                             2008-08-06                         SEMCTL(2)
Impressum