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

NAME

6       sem_open - initialize and open a named semaphore (REALTIME)
7

SYNOPSIS

9       #include <semaphore.h>
10
11       sem_t *sem_open(const char *name, int oflag, ...);
12
13

DESCRIPTION

15       The  sem_open()  function  shall establish a connection between a named
16       semaphore and a process. Following a call to sem_open() with  semaphore
17       name name, the process may reference the semaphore associated with name
18       using the address returned from the call. This semaphore may be used in
19       subsequent   calls   to   sem_wait(),  sem_trywait(),  sem_post(),  and
20       sem_close(). The semaphore remains usable by  this  process  until  the
21       semaphore  is  closed  by a successful call to sem_close(), _exit(), or
22       one of the exec functions.
23
24       The oflag argument controls whether the semaphore is created or  merely
25       accessed  by the call to sem_open(). The following flag bits may be set
26       in oflag:
27
28       O_CREAT
29              This flag is used to create a semaphore if it does  not  already
30              exist.  If O_CREAT is set and the semaphore already exists, then
31              O_CREAT has no effect, except as noted under O_EXCL.  Otherwise,
32              sem_open()  creates a named semaphore. The O_CREAT flag requires
33              a third and a fourth argument: mode, which is  of  type  mode_t,
34              and  value,  which is of type unsigned. The semaphore is created
35              with an initial value of value. Valid initial values  for  sema‐
36              phores are less than or equal to {SEM_VALUE_MAX}.
37
38       The  user  ID  of  the semaphore is set to the effective user ID of the
39       process; the group ID of the semaphore is set to a system default group
40       ID  or to the effective group ID of the process. The permission bits of
41       the semaphore are set to the value of the mode  argument  except  those
42       set  in  the  file mode creation mask of the process. When bits in mode
43       other than the file  permission  bits  are  specified,  the  effect  is
44       unspecified.
45
46       After  the semaphore named name has been created by sem_open() with the
47       O_CREAT flag, other processes can connect to the semaphore  by  calling
48       sem_open() with the same value of name.
49
50       O_EXCL If O_EXCL and O_CREAT are set, sem_open() fails if the semaphore
51              name exists. The check for the existence of  the  semaphore  and
52              the  creation  of  the semaphore if it does not exist are atomic
53              with respect to other processes executing sem_open() with O_EXCL
54              and  O_CREAT  set.  If O_EXCL is set and O_CREAT is not set, the
55              effect is undefined.
56
57       If flags other than O_CREAT and  O_EXCL  are  specified  in  the  oflag
58       parameter, the effect is unspecified.
59
60
61       The  name argument points to a string naming a semaphore object.  It is
62       unspecified whether the name appears in the file system and is  visible
63       to  functions that take pathnames as arguments.  The name argument con‐
64       forms to the construction rules for a pathname. If name begins with the
65       slash  character, then processes calling sem_open() with the same value
66       of name shall refer to the same semaphore object, as long as that  name
67       has  not been removed. If name does not begin with the slash character,
68       the effect is implementation-defined. The interpretation of slash char‐
69       acters  other  than  the leading slash character in name is implementa‐
70       tion-defined.
71
72       If a process makes multiple successful calls  to  sem_open()  with  the
73       same  value  for name, the same semaphore address shall be returned for
74       each such successful call, provided that there have been  no  calls  to
75       sem_unlink() for this semaphore.
76
77       References to copies of the semaphore produce undefined results.
78

RETURN VALUE

80       Upon  successful  completion,  the sem_open() function shall return the
81       address of the  semaphore.  Otherwise,  it  shall  return  a  value  of
82       SEM_FAILED  and  set errno to indicate the error. The symbol SEM_FAILED
83       is defined in the  <semaphore.h>  header.  No  successful  return  from
84       sem_open() shall return the value SEM_FAILED.
85

ERRORS

87       If any of the following conditions occur, the sem_open() function shall
88       return SEM_FAILED and set errno to the corresponding value:
89
90       EACCES The named semaphore exists  and  the  permissions  specified  by
91              oflag are denied, or the named semaphore does not exist and per‐
92              mission to create the named semaphore is denied.
93
94       EEXIST O_CREAT and O_EXCL are  set  and  the  named  semaphore  already
95              exists.
96
97       EINTR  The sem_open() operation was interrupted by a signal.
98
99       EINVAL The sem_open() operation is not supported for the given name, or
100              O_CREAT was specified  in  oflag  and  value  was  greater  than
101              {SEM_VALUE_MAX}.
102
103       EMFILE Too many semaphore descriptors or file descriptors are currently
104              in use by this process.
105
106       ENAMETOOLONG
107              The length of the name argument exceeds {PATH_MAX} or a pathname
108              component is longer than {NAME_MAX}.
109
110       ENFILE Too many semaphores are currently open in the system.
111
112       ENOENT O_CREAT is not set and the named semaphore does not exist.
113
114       ENOSPC There  is  insufficient  space for the creation of the new named
115              semaphore.
116
117
118       The following sections are informative.
119

EXAMPLES

121       None.
122

APPLICATION USAGE

124       The sem_open() function is part of the Semaphores option and  need  not
125       be available on all implementations.
126

RATIONALE

128       Early drafts required an error return value of -1 with the type sem_t *
129       for the sem_open() function, which is not  guaranteed  to  be  portable
130       across  implementations.  The  revised text provides the symbolic error
131       code SEM_FAILED to eliminate the type conflict.
132

FUTURE DIRECTIONS

134       None.
135

SEE ALSO

137       semctl() , semget() , semop() , sem_close() , sem_post()  ,  sem_timed‐
138       wait()  ,  sem_trywait() , sem_unlink() , sem_wait() , the Base Defini‐
139       tions volume of IEEE Std 1003.1-2001, <semaphore.h>
140
142       Portions of this text are reprinted and reproduced in  electronic  form
143       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
144       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
145       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
146       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
147       event of any discrepancy between this version and the original IEEE and
148       The Open Group Standard, the original IEEE and The Open Group  Standard
149       is  the  referee document. The original Standard can be obtained online
150       at http://www.opengroup.org/unix/online.html .
151
152
153
154IEEE/The Open Group                  2003                          SEM_OPEN(P)
Impressum