xref: /titanic_41/usr/src/man/man3c/sem_open.3c (revision 989f28072d20c73ae0955d6a1e3e2fc74831cb39)
te
Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
Copyright 1989 AT&T
Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at http://www.opengroup.org/bookstore/.
The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text
are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical
and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
This notice shall appear on any product containing this material.
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with
the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
SEM_OPEN 3C "Jul 9, 2009"
NAME
sem_open - initialize/open a named semaphore
SYNOPSIS

#include <semaphore.h>

sem_t *sem_open(const char *name, int oflag,
 /* unsigned long mode, unsigned int value */ ...);
DESCRIPTION

The sem_open() function establishes a connection between a named semaphore and a process (or LWP or thread). Following a call to sem_open() with semaphore name name, the process may reference the semaphore associated with name using the address returned from the call. This semaphore may be used in subsequent calls to sem_wait(3C), sem_trywait(3C), sem_post(3C), and sem_close(3C). The semaphore remains usable by this process until the semaphore is closed by a successful call to sem_close(3C), _Exit(2), or one of the exec functions.

The oflag argument controls whether the semaphore is created or merely accessed by the call to sem_open(). The following flag bits may be set in oflag: O_CREAT

This flag is used to create a semaphore if it does not already exist. If O_CREAT is set and the semaphore already exists, then O_CREAT has no effect, except as noted under O_EXCL. Otherwise, sem_open() creates a named semaphore. The O_CREAT flag requires a third and a fourth argument: mode, which is of type mode_t, and value, which is of type unsigned int. The semaphore is created with an initial value of value. Valid initial values for semaphores are less than or equal to SEM_VALUE_MAX. The user ID of the semaphore is set to the effective user ID of the process; the group ID of the semaphore is set to a system default group ID or to the effective group ID of the process. The permission bits of the semaphore are set to the value of the mode argument except those set in the file mode creation mask of the process (see umask(2)). When bits in mode other than the file permission bits are specified, the effect is unspecified. After the semaphore named name has been created by sem_open() with the O_CREAT flag, other processes can connect to the semaphore by calling sem_open() with the same value of name.

O_EXCL

If O_EXCL and O_CREAT are set, sem_open() fails if the semaphore name exists. The check for the existence of the semaphore and the creation of the semaphore if it does not exist are atomic with respect to other processes executing sem_open() with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set, the effect is undefined.

If flags other than O_CREAT and O_EXCL are specified in the oflag parameter, the effect is unspecified.

The name argument points to a string naming a semaphore object. It is unspecified whether the name appears in the file system and is visible to functions that take pathnames as arguments. The name argument conforms to the construction rules for a pathname. The first character of name must be a slash (/) character and the remaining characters of name cannot include any slash characters. For maximum portability, name should include no more than 14 characters, but this limit is not enforced.

If a process makes multiple successful calls to sem_open() with the same value for name, the same semaphore address is returned for each such successful call, provided that there have been no calls to sem_unlink(3C) for this semaphore.

References to copies of the semaphore produce undefined results.

The sem_init(3C) function is used with unnamed semaphores.

RETURN VALUES

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

ERRORS

If any of the following conditions occur, the sem_open() function will return SEM_FAILED and set errno to the corresponding value: EACCES

The named semaphore exists and the O_RDWR permissions are denied, or the named semaphore does not exist and permission to create the named semaphore is denied.

EEXIST

O_CREAT and O_EXCL are set and the named semaphore already exists.

EINTR

The sem_open() function was interrupted by a signal.

EINVAL

The sem_open() operation is not supported for the given name, or O_CREAT was set in oflag and value is greater than SEM_VALUE_MAX.

EMFILE

The number of open semaphore descriptors in this process exceeds SEM_NSEMS_MAX, or the number of open file descriptors in this process exceeds OPEN_MAX.

ENAMETOOLONG

The length of name string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX while _POSIX_NO_TRUNC is in effect.

ENFILE

Too many semaphores are currently open in the system.

ENOENT

O_CREAT is not set and the named semaphore does not exist.

ENOSPC

There is insufficient space for the creation of the new named semaphore.

ENOSYS

The sem_open() function is not supported by the system.

ATTRIBUTES

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE ATTRIBUTE VALUE
Interface Stability Committed
MT-Level MT-Safe
Standard See standards(5).
SEE ALSO

exec(2), exit(2), umask(2), sem_close(3C), sem_init(3C), sem_post(3C), sem_unlink(3C), sem_wait(3C), sysconf(3C), attributes(5), standards(5)