1SEM_OPEN(3P)               POSIX Programmer's Manual              SEM_OPEN(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       sem_open - initialize and open a named semaphore (REALTIME)
13

SYNOPSIS

15       #include <semaphore.h>
16
17       sem_t *sem_open(const char *name, int oflag, ...);
18
19

DESCRIPTION

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

RETURN VALUE

86       Upon successful completion, the sem_open() function  shall  return  the
87       address  of  the  semaphore.  Otherwise,  it  shall  return  a value of
88       SEM_FAILED and set errno to indicate the error. The  symbol  SEM_FAILED
89       is  defined  in  the  <semaphore.h>  header.  No successful return from
90       sem_open() shall return the value SEM_FAILED.
91

ERRORS

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

EXAMPLES

127       None.
128

APPLICATION USAGE

130       The  sem_open()  function is part of the Semaphores option and need not
131       be available on all implementations.
132

RATIONALE

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

FUTURE DIRECTIONS

140       None.
141

SEE ALSO

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