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

NAME

6       semget - get a 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 semaphore set identifier associ‐
17       ated with the argument key.  A new set of nsems semaphores  is  created
18       if  key  has  the  value IPC_PRIVATE or if no existing semaphore set is
19       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       IPC_PRIVATE isn't a flag field but a key_t type.  If this special value
98       is  used for key, the system call ignores everything but the least sig‐
99       nificant 9 bits of semflg and creates a new semaphore set (on success).
100
101       The following limits on semaphore set  resources  affect  the  semget()
102       call:
103
104       SEMMNI System  wide  maximum number of semaphore sets: policy dependent
105              (on Linux, this limit can be read and modified  via  the  fourth
106              field of /proc/sys/kernel/sem).
107
108       SEMMSL Maximum number of semaphores per semid: implementation dependent
109              (on Linux, this limit can be read and  modified  via  the  first
110              field of /proc/sys/kernel/sem).
111
112       SEMMNS System  wide  maximum number of semaphores: policy dependent (on
113              Linux, this limit can be read and modified via the second  field
114              of  /proc/sys/kernel/sem).   Values greater than SEMMSL * SEMMNI
115              makes it irrelevant.
116

BUGS

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

SEE ALSO

129       semctl(2),   semop(2),   ftok(3),   capabilities(7),   sem_overview(7),
130       svipc(7)
131

COLOPHON

133       This  page  is  part of release 3.25 of the Linux man-pages project.  A
134       description of the project, and information about reporting  bugs,  can
135       be found at http://www.kernel.org/doc/man-pages/.
136
137
138
139Linux                             2004-05-27                         SEMGET(2)
Impressum