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

NAME

6       semget - get a System V semaphore set identifier
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/ipc.h>
11       #include <sys/sem.h>
12
13       int semget(key_t key, int nsems, int semflg);
14

DESCRIPTION

16       The  semget() system call returns the System V semaphore set identifier
17       associated with the argument key.  A new set  of  nsems  semaphores  is
18       created  if  key  has the value IPC_PRIVATE or if no existing semaphore
19       set is associated with key and IPC_CREAT is specified in semflg.
20
21       If semflg specifies both IPC_CREAT and IPC_EXCL  and  a  semaphore  set
22       already  exists  for key, then semget() fails with errno set to EEXIST.
23       (This is analogous to the effect of the combination  O_CREAT  |  O_EXCL
24       for open(2).)
25
26       Upon  creation,  the  least  significant  9 bits of the argument semflg
27       define the permissions (for owner, group and others) for the  semaphore
28       set.   These  bits  have  the same format, and the same meaning, as the
29       mode argument of open(2) (though the execute permissions are not  mean‐
30       ingful  for  semaphores, and write permissions mean permission to alter
31       semaphore values).
32
33       The values of the semaphores in a newly created set are  indeterminate.
34       (POSIX.1-2001  is  explicit  on this point.)  Although Linux, like many
35       other implementations, initializes the semaphore values to 0, a  porta‐
36       ble  application  cannot  rely on this: it should explicitly initialize
37       the semaphores to the desired values.
38
39       When creating a new semaphore set, semget() initializes the set's asso‐
40       ciated data structure, semid_ds (see semctl(2)), as follows:
41
42              sem_perm.cuid  and sem_perm.uid are set to the effective user ID
43              of the calling process.
44
45              sem_perm.cgid and sem_perm.gid are set to the effective group ID
46              of the calling process.
47
48              The  least  significant  9  bits of sem_perm.mode are set to the
49              least significant 9 bits of semflg.
50
51              sem_nsems is set to the value of nsems.
52
53              sem_otime is set to 0.
54
55              sem_ctime is set to the current time.
56
57       The argument nsems can be 0 (a don't care) when a semaphore set is  not
58       being created.  Otherwise nsems must be greater than 0 and less than or
59       equal to the maximum number of semaphores per semaphore set (SEMMSL).
60
61       If the semaphore set already exists, the permissions are verified.
62

RETURN VALUE

64       If successful, the return value will be the semaphore set identifier (a
65       nonnegative  integer),  otherwise -1 is returned, with errno indicating
66       the error.
67

ERRORS

69       On failure errno will be set to one of the following:
70
71       EACCES A semaphore set exists for key, but the calling process does not
72              have  permission  to  access  the  set,  and  does  not have the
73              CAP_IPC_OWNER capability.
74
75       EEXIST A semaphore  set  exists  for  key  and  semflg  specified  both
76              IPC_CREAT and IPC_EXCL.
77
78       EINVAL nsems  is less than 0 or greater than the limit on the number of
79              semaphores per semaphore set (SEMMSL), or a semaphore set corre‐
80              sponding  to  key  already  exists, and nsems is larger than the
81              number of semaphores in that set.
82
83       ENOENT No semaphore set exists for  key  and  semflg  did  not  specify
84              IPC_CREAT.
85
86       ENOMEM A  semaphore  set has to be created but the system does not have
87              enough memory for the new data structure.
88
89       ENOSPC A semaphore set has to be created but the system limit  for  the
90              maximum  number  of  semaphore sets (SEMMNI), or the system wide
91              maximum number of semaphores (SEMMNS), would be exceeded.
92

CONFORMING TO

94       SVr4, POSIX.1-2001.
95

NOTES

97       The inclusion of <sys/types.h> and <sys/ipc.h> isn't required on  Linux
98       or by any version of POSIX.  However, some old implementations required
99       the inclusion of these header files, and the SVID also documented their
100       inclusion.   Applications  intended  to be portable to such old systems
101       may need to include these header files.
102
103       IPC_PRIVATE isn't a flag field but a key_t type.  If this special value
104       is  used for key, the system call ignores everything but the least sig‐
105       nificant 9 bits of semflg and creates a new semaphore set (on success).
106
107       The following limits on semaphore set  resources  affect  the  semget()
108       call:
109
110       SEMMNI System  wide  maximum number of semaphore sets: policy dependent
111              (on Linux, this limit can be read and modified  via  the  fourth
112              field of /proc/sys/kernel/sem).
113
114       SEMMSL Maximum number of semaphores per semid: implementation dependent
115              (on Linux, this limit can be read and  modified  via  the  first
116              field of /proc/sys/kernel/sem).
117
118       SEMMNS System  wide  maximum number of semaphores: policy dependent (on
119              Linux, this limit can be read and modified via the second  field
120              of  /proc/sys/kernel/sem).   Values greater than SEMMSL * SEMMNI
121              makes it irrelevant.
122

BUGS

124       The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
125       clearly show its function.
126
127       The  semaphores  in a set are not initialized by semget().  In order to
128       initialize the semaphores, semctl(2) must be used to perform  a  SETVAL
129       or  a  SETALL operation on the semaphore set.  (Where multiple peers do
130       not know who will be the first to initialize the set,  checking  for  a
131       nonzero  sem_otime in the associated data structure retrieved by a sem‐
132       ctl(2) IPC_STAT operation can be used to avoid races.)
133

SEE ALSO

135       semctl(2),   semop(2),   ftok(3),   capabilities(7),   sem_overview(7),
136       svipc(7)
137

COLOPHON

139       This  page  is  part of release 3.53 of the Linux man-pages project.  A
140       description of the project, and information about reporting  bugs,  can
141       be found at http://www.kernel.org/doc/man-pages/.
142
143
144
145Linux                             2012-05-31                         SEMGET(2)
Impressum