NAME
sem_open — create/open a named POSIX semaphore
SYNOPSIS
#include <sys/semaphore.h>
sem_t * sem_open(const char *name, int oflag, mode_t mode,
unsigned int value);
DESCRIPTION
sem_open()
is used to open or create a named semaphore.
A successful call to
sem_open()
will create a descriptor for
the semaphore specified by
name.
The pointer to the semaphore returned by
sem_open()
can be used to access the semaphore associated with
name
in subsequent operations.
The
name
argument points to a string referring to a semaphore. It should begin with
a "/" and shall conform to pathname rules except that no path
component should be "." or "..".
The
oflag
argument specifies
whether a semaphore is to be created or not.
The following bits in it may be set:
- O_CREAT
If this flag is set, a new semaphore is created if it does not already exist.
If this flag is not set, the semaphore should already exist.
- O_EXCL
If this flag is set, the call fails if the semaphore already exists.
This flag is valid only when
O_CREAT
is also set; otherwise, it is ignored.
The
mode
and
value
arguments are provided to supply the permissions and the initial value
information necessary for creating a new semaphore.
To use this function, link in the realtime library by specifying
-lrt
on the compiler or linker command line.
EXAMPLES
The following call to
sem_open()
will create a new named semaphore
if one does not exist,
which depends on the flags specified in
oflag,
has the permissions specified in
mode
and has an initial value of
value.
sem_open(name, oflag, mode, value);
RETURN VALUE
If the semaphore was created and initialized,
sem_open()
returns a pointer to a
sem_t
structure containing the index of the new descriptor.
If the semaphore could not be created/initialized, the call returns -1 and sets
errno
to indicate the error.
If the named semaphore is already opened by the calling process,
a descriptor and a
sem_t
structure for
the named semaphore already exists for the calling process.
A new descriptor is not created and a pointer
to the existing
sem_t
structure is returned for this call.
ERRORS
sem_open()
fails and does not perform the requested operation
if any of the following conditions are encountered:
- [EACCES]
The named semaphore exists and the process does not have the permissions to
open the semaphore as described by
oflag,
or the named semaphore does not
exist and the process does not have the permission to open it.
- [EEXIST]
The flags
O_CREAT
and
O_EXCL
are set in
oflag
and the named semaphore exists.
- [EINTR]
A signal interrupted the
sem_open()
operation.
- [EINVAL]
The argument
value
is greater than
{_POSIX_SEM_VALUE_MAX}
and the
O_CREAT
flag was specified in
oflag.
- [EINVAL]
The
name
argument does not begin with "/" or contains "." or ".." as a pathname
component.
- [EMFILE]
Too many semaphore descriptors are currently in use by this process.
- [ENAMETOOLONG]
The name string is longer than
{PATH_MAX}.
- [ENFILE]
There are too many semaphores in the system.
- [ENOENT]
The flag
O_CREAT
is not set in
oflag
and the named semaphore does not exist.
- [ENOSPC]
There are insufficient resources for the creation of a new named semaphore.
STANDARDS CONFORMANCE
sem_open(): POSIX