DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

sem_open(S-osr5)


sem_open -- initialize/open a named semaphore

Syntax

cc . . . -lsuds -lx
#include <semaphore.h>

sem_t *sem_open(const char *name, int oflag,...);

Description

The sem_open function establishes a connection between a named semaphore and a process. Note that the suds driver is required to be on: by default, it is switched off in the kernel. To switch this driver on, change the ``N'' in the /etc/conf/sdevice.d/suds file to a ``Y'', relink the kernel, and reboot. Following a call to sem_open with semaphore 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(S-osr5), sem_trywait(S-osr5), sem_post(S-osr5), and sem_close(S-osr5). The semaphore remains usable by this process until it is closed by a successful call to sem_close, exit(S-osr5), or exec(S-osr5). 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, below. 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 must be less than or equal to {SEM_VALUE_MAX}.

The semaphore's user ID is set to the effective user ID of the process; the semaphore's group ID is set to a system default group ID or to the effective group ID of the process. The semaphore's permission bits are set to the value of the mode argument except those set in the file mode creation mask of the process.

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.

The name argument points to a string naming a semaphore object. The name argument must conform to the construction rules for a pathname. If name begins with the slash character, then processes calling sem_open with the same value of name refer to the same semaphore object, as long as that name has not been removed.

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(S-osr5) for this semaphore.

Return values

Upon successful completion, the function returns the address of the semaphore.

Diagnostics

If any of the following conditions occurs, the sem_open function returns -1 and sets errno to the corresponding value:

[EACCES]
The named semaphore exists and the permissions specified by oflag 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 operation was interrupted by a signal.

[EINVAL]
The sem_open operation is not supported for the given name; or O_CREAT was specified in oflag and value was greater than {SEM_VALUE_MAX}.

[EMFILE]
Too many semaphore descriptors or file descriptors are currently in use by this process.

[ENAMETOOLONG]
The length of the 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.

See also

semaphore(FP), sem_close(S-osr5), sem_post(S-osr5), sem_trywait(S-osr5), sem_unlink(S-osr5), sem_wait(S-osr5)

Standards conformance

Text reprinted and/or adapted from IEEE Std 1003.1b-1993, IEEE Standard for Information Technology, POSIX Part 1: System Application Program Interface (API) Amendment 1: Realtime Extensions [C Language], copyright © 1993 by the Institute of Electrical and Electronics Engineers, Inc. The IEEE takes no responsibility for and will assume no liability for damages resulting from the reader's misinterpretation of said information resulting from the placement and context in this publication. Information is reproduced with the permission of the IEEE.
© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005