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

NAME

6       semget - get set of XSI semaphores
7

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

53       Upon successful completion, semget() shall return a non-negative  inte‐
54       ger,  namely  a semaphore identifier; otherwise, it shall return -1 and
55       set errno to indicate the error.
56

ERRORS

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

EXAMPLES

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

APPLICATION USAGE

156       The  POSIX Realtime Extension defines alternative interfaces for inter‐
157       process communication. Application  developers  who  need  to  use  IPC
158       should design their applications so that modules using the IPC routines
159       described in XSI Interprocess Communication can be easily  modified  to
160       use the alternative interfaces.
161

RATIONALE

163       None.
164

FUTURE DIRECTIONS

166       None.
167

SEE ALSO

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