1SEMCTL(3P)                 POSIX Programmer's Manual                SEMCTL(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       semctl — XSI semaphore control operations
14

SYNOPSIS

16       #include <sys/sem.h>
17
18       int semctl(int semid, int semnum, int cmd, ...);
19

DESCRIPTION

21       The semctl() function operates on XSI semaphores (see the Base  Defini‐
22       tions volume of POSIX.1‐2008, Section 4.16, Semaphore).  It is unspeci‐
23       fied whether this function interoperates with the realtime interprocess
24       communication facilities defined in Section 2.8, Realtime.
25
26       The  semctl()  function  provides a variety of semaphore control opera‐
27       tions as specified by cmd.  The fourth argument is optional and depends
28       upon  the  operation requested. If required, it is of type union semun,
29       which the application shall explicitly declare:
30
31           union semun {
32               int val;
33               struct semid_ds *buf;
34               unsigned short  *array;
35           } arg;
36
37       The following semaphore control operations as specified by cmd are exe‐
38       cuted with respect to the semaphore specified by semid and semnum.  The
39       level of permission required for each operation is shown with each com‐
40       mand;  see  Section  2.7, XSI Interprocess Communication.  The symbolic
41       names for the values of cmd are defined in the <sys/sem.h> header:
42
43       GETVAL      Return the value of semval; see <sys/sem.h>.  Requires read
44                   permission.
45
46       SETVAL      Set  the value of semval to arg.val, where arg is the value
47                   of the fourth argument to semctl().  When this  command  is
48                   successfully  executed,  the  semadj value corresponding to
49                   the specified semaphore in all processes is cleared.  Also,
50                   the  sem_ctime  timestamp shall be set to the current time,
51                   as described in Section  2.7.1,  IPC  General  Description.
52                   Requires  alter  permission; see Section 2.7, XSI Interpro‐
53                   cess Communication.
54
55       GETPID      Return the value of sempid.  Requires read permission.
56
57       GETNCNT     Return the value of semncnt.  Requires read permission.
58
59       GETZCNT     Return the value of semzcnt.  Requires read permission.
60
61       The following values of cmd operate on each semval in the set of  sema‐
62       phores:
63
64       GETALL      Return  the value of semval for each semaphore in the sema‐
65                   phore set and place into the array pointed to by arg.array,
66                   where  arg  is  the  fourth argument to semctl().  Requires
67                   read permission.
68
69       SETALL      Set the value of semval for each semaphore in the semaphore
70                   set  according  to the array pointed to by arg.array, where
71                   arg is the fourth argument to semctl().  When this  command
72                   is  successfully  executed, the semadj values corresponding
73                   to each specified semaphore in all processes  are  cleared.
74                   Also,  the  sem_ctime timestamp shall be set to the current
75                   time, as described in Section 2.7.1, IPC  General  Descrip‐
76                   tion.  Requires alter permission.
77
78       The following values of cmd are also available:
79
80       IPC_STAT    Place the current value of each member of the semid_ds data
81                   structure associated with semid into the structure  pointed
82                   to  by  arg.buf,  where  arg is the fourth argument to sem‐
83                   ctl().  The contents  of  this  structure  are  defined  in
84                   <sys/sem.h>.  Requires read permission.
85
86       IPC_SET     Set the value of the following members of the semid_ds data
87                   structure associated with semid to the corresponding  value
88                   found  in the structure pointed to by arg.buf, where arg is
89                   the fourth argument to semctl():
90
91                       sem_perm.uid
92                       sem_perm.gid
93                       sem_perm.mode
94
95                   The mode bits  specified  in  Section  2.7.1,  IPC  General
96                   Description  are  copied into the corresponding bits of the
97                   sem_perm.mode associated with semid.  The stored values  of
98                   any  other  bits  are  unspecified. The sem_ctime timestamp
99                   shall be set to the current time, as described  in  Section
100                   2.7.1, IPC General Description.
101
102                   This  command can only be executed by a process that has an
103                   effective user ID equal to either that of  a  process  with
104                   appropriate  privileges or to the value of sem_perm.cuid or
105                   sem_perm.uid in the semid_ds data structure associated with
106                   semid.
107
108       IPC_RMID    Remove the semaphore identifier specified by semid from the
109                   system and destroy the set of semaphores and semid_ds  data
110                   structure associated with it. This command can only be exe‐
111                   cuted by a process that has an effective user ID  equal  to
112                   either  that of a process with appropriate privileges or to
113                   the value of sem_perm.cuid or sem_perm.uid in the  semid_ds
114                   data structure associated with semid.
115

RETURN VALUE

117       If  successful,  the  value returned by semctl() depends on cmd as fol‐
118       lows:
119
120       GETVAL      The value of semval.
121
122       GETPID      The value of sempid.
123
124       GETNCNT     The value of semncnt.
125
126       GETZCNT     The value of semzcnt.
127
128       All others  0.
129
130       Otherwise, semctl() shall return −1  and  set  errno  to  indicate  the
131       error.
132

ERRORS

134       The semctl() function shall fail if:
135
136       EACCES Operation  permission is denied to the calling process; see Sec‐
137              tion 2.7, XSI Interprocess Communication.
138
139       EINVAL The value of semid is not a valid semaphore identifier,  or  the
140              value  of  semnum  is  less  than  0 or greater than or equal to
141              sem_nsems, or the value of cmd is not a valid command.
142
143       EPERM  The argument cmd is equal to IPC_RMID or IPC_SET and the  effec‐
144              tive  user  ID  of the calling process is not equal to that of a
145              process with appropriate privileges and it is not equal  to  the
146              value  of  sem_perm.cuid  or  sem_perm.uid in the data structure
147              associated with semid.
148
149       ERANGE The argument cmd is equal to SETVAL or SETALL and the  value  to
150              which  semval  is  to  be set is greater than the system-imposed
151              maximum.
152
153       The following sections are informative.
154

EXAMPLES

156       Refer to semop().
157

APPLICATION USAGE

159       The fourth parameter in the SYNOPSIS section is now specified as  "..."
160       in order to avoid a clash with the ISO C standard when referring to the
161       union semun (as defined in Issue 3) and for backwards-compatibility.
162
163       The POSIX Realtime Extension defines alternative interfaces for  inter‐
164       process  communication.  Application  developers  who  need  to use IPC
165       should design their applications so that modules using the IPC routines
166       described  in Section 2.7, XSI Interprocess Communication can be easily
167       modified to use the alternative interfaces.
168

RATIONALE

170       None.
171

FUTURE DIRECTIONS

173       None.
174

SEE ALSO

176       Section 2.7, XSI Interprocess Communication, Section 2.8, Realtime,
177       semget(), semop(), sem_close(), sem_destroy(), sem_getvalue(),
178       sem_init(), sem_open(), sem_post(), sem_trywait(), sem_unlink()
179
180       The Base Definitions volume of POSIX.1‐2008, Section  4.16,  Semaphore,
181       <sys_sem.h>
182
184       Portions  of  this text are reprinted and reproduced in electronic form
185       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
186       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
187       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
188       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
189       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  In  the
190       event of any discrepancy between this version and the original IEEE and
191       The Open Group Standard, the original IEEE and The Open Group  Standard
192       is  the  referee document. The original Standard can be obtained online
193       at http://www.unix.org/online.html .
194
195       Any typographical or formatting errors that appear  in  this  page  are
196       most likely to have been introduced during the conversion of the source
197       files to man page format. To report such errors,  see  https://www.ker
198       nel.org/doc/man-pages/reporting_bugs.html .
199
200
201
202IEEE/The Open Group                  2013                           SEMCTL(3P)
Impressum