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

NAME

6       semctl - System V 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 System
17       V semaphore set identified by semid, or on the semnum-th  semaphore  of
18       that 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 long   sem_nsems; /* No. of semaphores in set */
39           };
40
41       The  ipc_perm  structure  is defined as follows (the highlighted fields
42       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

RETURN VALUE

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

ERRORS

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

CONFORMING TO

222       SVr4, POSIX.1-2001.
223
224       POSIX.1-2001 specifies the sem_nsems field of the semid_ds structure as
225       having  the  type  unsigned short,  and the field is so defined on most
226       other systems.  It was also so defined on Linux 2.2 and  earlier,  but,
227       since Linux 2.4, the field has the type unsigned long.
228

NOTES

230       The  inclusion of <sys/types.h> and <sys/ipc.h> isn't required on Linux
231       or by any version of POSIX.  However, some old implementations required
232       the inclusion of these header files, and the SVID also documented their
233       inclusion.  Applications intended to be portable to  such  old  systems
234       may need to include these header files.
235
236       The  IPC_INFO, SEM_STAT and SEM_INFO operations are used by the ipcs(1)
237       program to provide information on allocated resources.  In  the  future
238       these may modified or moved to a /proc file system interface.
239
240       Various fields in a struct semid_ds were typed as short under Linux 2.2
241       and have become long under Linux 2.4.  To take  advantage  of  this,  a
242       recompilation  under glibc-2.1.91 or later should suffice.  (The kernel
243       distinguishes old and new calls by an IPC_64 flag in cmd.)
244
245       In some earlier versions of glibc,  the  semun  union  was  defined  in
246       <sys/sem.h>,  but  POSIX.1-2001  requires  that  the caller define this
247       union.  On versions of glibc where this union is not defined, the macro
248       _SEM_SEMUN_UNDEFINED is defined in <sys/sem.h>.
249
250       The following system limit on semaphore sets affects a semctl() call:
251
252       SEMVMX Maximum value for semval: implementation dependent (32767).
253
254       For  greater  portability  it is best to always call semctl() with four
255       arguments.
256

SEE ALSO

258       ipc(2), semget(2), semop(2), capabilities(7), sem_overview(7), svipc(7)
259

COLOPHON

261       This page is part of release 3.53 of the Linux  man-pages  project.   A
262       description  of  the project, and information about reporting bugs, can
263       be found at http://www.kernel.org/doc/man-pages/.
264
265
266
267Linux                             2013-06-03                         SEMCTL(2)
Impressum