1SEMGET(3P)                 POSIX Programmer's Manual                SEMGET(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

NAME

12       semget - get set of XSI semaphores
13

SYNOPSIS

15       #include <sys/sem.h>
16
17       int semget(key_t key, int nsems, int semflg);
18
19

DESCRIPTION

21       The semget() function operates on XSI semaphores (see the Base  Defini‐
22       tions  volume of IEEE Std 1003.1-2001, Section 4.15, Semaphore).  It is
23       unspecified whether  this  function  interoperates  with  the  realtime
24       interprocess communication facilities defined in Realtime .
25
26       The  semget() function shall return the semaphore identifier associated
27       with key.
28
29       A semaphore identifier with its associated semid_ds data structure  and
30       its associated set of nsems semaphores (see <sys/sem.h>) is created for
31       key if one of the following is true:
32
33        * The argument key is equal to IPC_PRIVATE.
34
35        * The argument key does not already have a semaphore identifier  asso‐
36          ciated with it and (semflg &IPC_CREAT) is non-zero.
37
38       Upon creation, the semid_ds data structure associated with the new sem‐
39       aphore identifier is initialized as follows:
40
41        * In the operation permissions structure sem_perm.cuid,  sem_perm.uid,
42          sem_perm.cgid,  and sem_perm.gid shall be set equal to the effective
43          user ID  and  effective  group  ID,  respectively,  of  the  calling
44          process.
45
46        * The low-order 9 bits of sem_perm.mode shall be set equal to the low-
47          order 9 bits of semflg.
48
49        * The variable sem_nsems shall be set equal to the value of nsems.
50
51        * The variable sem_otime shall be set equal to 0 and  sem_ctime  shall
52          be set equal to the current time.
53
54        * The  data  structure associated with each semaphore in the set shall
55          not be initialized. The semctl() function with the command SETVAL or
56          SETALL can be used to initialize each semaphore.
57

RETURN VALUE

59       Upon  successful completion, semget() shall return a non-negative inte‐
60       ger, namely a semaphore identifier; otherwise, it shall return  -1  and
61       set errno to indicate the error.
62

ERRORS

64       The semget() function shall fail if:
65
66       EACCES A  semaphore identifier exists for key, but operation permission
67              as specified by the low-order 9 bits  of  semflg  would  not  be
68              granted; see XSI Interprocess Communication .
69
70       EEXIST A  semaphore identifier exists for the argument key but ((semflg
71              &IPC_CREAT) &&(semflg &IPC_EXCL)) is non-zero.
72
73       EINVAL The value of nsems is either less than or equal to 0 or  greater
74              than  the system-imposed limit, or a semaphore identifier exists
75              for the argument key, but the number of semaphores  in  the  set
76              associated  with it is less than nsems and nsems is not equal to
77              0.
78
79       ENOENT A semaphore identifier does not exist for the argument  key  and
80              (semflg &IPC_CREAT) is equal to 0.
81
82       ENOSPC A  semaphore  identifier is to be created but the system-imposed
83              limit on the maximum number of  allowed  semaphores  system-wide
84              would be exceeded.
85
86
87       The following sections are informative.
88

EXAMPLES

90   Creating a Semaphore Identifier
91       The  following  example  gets  a  unique semaphore key using the ftok()
92       function, then gets a semaphore ID associated with that key  using  the
93       semget() function (the first call also tests to make sure the semaphore
94       exists). If the semaphore does not exist, the program  creates  it,  as
95       shown by the second call to semget(). In creating the semaphore for the
96       queuing process, the program attempts  to  create  one  semaphore  with
97       read/write  permission  for  all. It also uses the IPC_EXCL flag, which
98       forces semget() to fail if the semaphore already exists.
99
100       After creating the semaphore, the program uses a  call  to  semop()  to
101       initialize  it to the values in the sbuf array. The number of processes
102       that can execute concurrently without queuing is initially  set  to  2.
103       The  final  call to semget() creates a semaphore identifier that can be
104       used later in the program.
105
106
107              #include <sys/types.h>
108              #include <stdio.h>
109              #include <sys/ipc.h>
110              #include <sys/sem.h>
111              #include <sys/stat.h>
112              #include <errno.h>
113              #include <unistd.h>
114              #include <stdlib.h>
115              #include <pwd.h>
116              #include <fcntl.h>
117              #include <limits.h>
118              ...
119              key_t semkey;
120              int semid, pfd, fv;
121              struct sembuf sbuf;
122              char *lgn;
123              char filename[PATH_MAX+1];
124              struct stat outstat;
125              struct passwd *pw;
126              ...
127              /* Get unique key for semaphore. */
128              if ((semkey = ftok("/tmp", 'a')) == (key_t) -1) {
129                  perror("IPC error: ftok"); exit(1);
130              }
131
132
133              /* Get semaphore ID associated with this key. */
134              if ((semid = semget(semkey, 0, 0)) == -1) {
135
136
137                  /* Semaphore does not exist - Create. */
138                  if ((semid = semget(semkey, 1, IPC_CREAT | IPC_EXCL | S_IRUSR |
139                      S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) != -1)
140                  {
141                      /* Initialize the semaphore. */
142                      sbuf.sem_num = 0;
143                      sbuf.sem_op = 2;  /* This is the number of runs
144                                           without queuing. */
145                      sbuf.sem_flg = 0;
146                      if (semop(semid, &sbuf, 1) == -1) {
147                          perror("IPC error: semop"); exit(1);
148                      }
149                  }
150                  else if (errno == EEXIST) {
151                      if ((semid = semget(semkey, 0, 0)) == -1) {
152                          perror("IPC error 1: semget"); exit(1);
153                      }
154                  }
155                  else {
156                      perror("IPC error 2: semget"); exit(1);
157                  }
158              }
159              ...
160

APPLICATION USAGE

162       The POSIX Realtime Extension defines alternative interfaces for  inter‐
163       process  communication.  Application  developers  who  need  to use IPC
164       should design their applications so that modules using the IPC routines
165       described  in  XSI Interprocess Communication can be easily modified to
166       use the alternative interfaces.
167

RATIONALE

169       None.
170

FUTURE DIRECTIONS

172       None.
173

SEE ALSO

175       XSI   Interprocess   Communication,   Realtime,   semctl(),    semop(),
176       sem_close(),  sem_destroy(),  sem_getvalue(),  sem_init(),  sem_open(),
177       sem_post(), sem_unlink(), sem_wait(), the Base  Definitions  volume  of
178       IEEE Std 1003.1-2001, <sys/sem.h>
179
181       Portions  of  this text are reprinted and reproduced in electronic form
182       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
183       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
184       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
185       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
186       event of any discrepancy between this version and the original IEEE and
187       The  Open Group Standard, the original IEEE and The Open Group Standard
188       is the referee document. The original Standard can be  obtained  online
189       at http://www.opengroup.org/unix/online.html .
190
191
192
193IEEE/The Open Group                  2003                           SEMGET(3P)
Impressum