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() */
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
58                   arg.buf.  The argument  semnum  is  ignored.   The  calling
59                   process must 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  associ‐
63                   ated  with  this semaphore set, updating also its sem_ctime
64                   member.   The  following  members  of  the  structure   are
65                   updated: sem_perm.uid, sem_perm.gid, and (the least signif‐
66                   icant 9 bits of) sem_perm.mode.  The effective UID  of  the
67                   calling process must match the owner (sem_perm.uid) or cre‐
68                   ator (sem_perm.cuid) of the semaphore set,  or  the  caller
69                   must be privileged.  The argument semnum is ignored.
70
71       IPC_RMID    Immediately  remove  the  semaphore set, awakening all pro‐
72                   cesses blocked in semop() calls on the set (with  an  error
73                   return  and  errno set to EIDRM).  The effective user ID of
74                   the calling process must match the creator or owner of  the
75                   semaphore set, or the caller must be privileged.  The argu‐
76                   ment semnum 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;  /* # of entries in semaphore map;
86                                         unused */
87                         int semmni;  /* Max. # of semaphore sets */
88                         int semmns;  /* Max. # of semaphores in all
89                                         semaphore sets */
90                         int semmnu;  /* System-wide max. # of undo
91                                         structures; unused */
92                         int semmsl;  /* Max. # of semaphores in a set */
93                         int semopm;  /* Max. # of operations for semop() */
94                         int semume;  /* Max. # of undo entries per
95                                         process; unused */
96                         int semusz;  /* size of struct sem_undo */
97                         int semvmx;  /* Maximum semaphore value */
98                         int semaem;  /* Max. value that can be recorded for
99                                         semaphore adjustment (SEM_UNDO) */
100                     };
101
102                   The  semmsl,  semmns,  semopm,  and  semmni settings can be
103                   changed via /proc/sys/kernel/sem; see proc(5) for details.
104
105       SEM_INFO (Linux specific)
106                   Returns a seminfo structure containing the same information
107                   as  for  IPC_INFO,  except  that  the  following fields are
108                   returned with information about system  resources  consumed
109                   by semaphores: the semusz field returns the number of sema‐
110                   phore sets that currently exist  on  the  system;  and  the
111                   semaem  field returns the total number of semaphores in all
112                   semaphore sets on the system.
113
114       SEM_STAT (Linux specific)
115                   Returns a semid_ds structure as for IPC_STAT.  However, the
116                   semid  argument  is not a semaphore identifier, but instead
117                   an index into the kernel's internal  array  that  maintains
118                   information about all semaphore sets on the system.
119
120       GETALL      Return  semval (i.e., the current value) for all semaphores
121                   of the set into arg.array.  The argument semnum is ignored.
122                   The  calling process must have read permission on the sema‐
123                   phore set.
124
125       GETNCNT     The system call returns the value  of  semncnt  (i.e.,  the
126                   number of processes waiting for the value of this semaphore
127                   to increase) for the semnum-th semaphore of the  set  (i.e.
128                   the  number  of processes waiting for an increase of semval
129                   for the semnum-th  semaphore  of  the  set).   The  calling
130                   process must have read permission on the semaphore set.
131
132       GETPID      The  system  call  returns the value of sempid for the sem‐
133                   num-th semaphore of the set (i.e. the PID  of  the  process
134                   that executed the last semop() call for the semnum-th sema‐
135                   phore of the set).  The calling process must have read per‐
136                   mission on the semaphore set.
137
138       GETVAL      The  system  call  returns the value of semval for the sem‐
139                   num-th semaphore of the set.  The calling process must have
140                   read permission on the semaphore set.
141
142       GETZCNT     The  system  call  returns  the value of semzcnt (i.e., the
143                   number of processes waiting for the value of this semaphore
144                   to  become  zero)  for  the  semnum-th semaphore of the set
145                   (i.e. the number of processes waiting  for  semval  of  the
146                   semnum-th  semaphore  of the set to become 0).  The calling
147                   process must have read permission on 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 struc‐
151                   ture associated with the set.  Undo entries (see  semop(2))
152                   are  cleared  for  altered semaphores in all processes.  If
153                   the  changes  to  semaphore  values  would  permit  blocked
154                   semop()  calls  in  other  processes to proceed, then those
155                   processes are woken up.  The argument  semnum  is  ignored.
156                   The  calling  process must have alter (write) permission on
157                   the semaphore 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
162                   are  cleared  for  altered semaphores in all processes.  If
163                   the  changes  to  semaphore  values  would  permit  blocked
164                   semop()  calls  in  other  processes to proceed, then those
165                   processes are woken up.   The  calling  process  must  have
166                   alter permission on 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 internal
183                  array recording information about all semaphore sets.  (This
184                  information can be used with repeated SEM_STAT operations to
185                  obtain information about all semaphore sets on the system.)
186
187       SEM_INFO   As for IPC_INFO.
188
189       SEM_STAT   the identifier of the semaphore set whose index was given in
190                  semid.
191
192       All other cmd values return 0 on success.
193

ERRORS

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

NOTES

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

CONFORMING TO

249       SVr4, POSIX.1-2001.
250

SEE ALSO

252       ipc(2), semget(2), semop(2), capabilities(7), sem_overview(7), svipc(7)
253
254
255
256Linux 2.6.9                       2004-11-10                         SEMCTL(2)
Impressum