sem_open()--Open Named Semaphore


  Syntax
 #include <semaphore.h>

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

  Service Program Name: QP0ZPSEM

  Default Public Authority: *USE

  Threadsafe: Yes

The sem_open() function opens a named semaphore, returning a semaphore pointer that may be used on subsequent calls to sem_post(), sem_post_np(), sem_wait(), sem_wait_np(), sem_trywait(), sem_getvalue(), and sem_close(). When a semaphore is being created, the parameters mode and value must be specified on the call to sem_open(). If a semaphore is created, then the maximum value of the semaphore is set to SEM_VALUE_MAX and the title of the semaphore is set to the last 16 characters of the name.

If sem_open() is called multiple times within the same process using the same name, sem_open() will return a pointer to the same semaphore, as long as another process has not used sem_unlink() to unlink the semaphore.

If sem_open() is called from a program using data model LLP64, the returned semaphore pointer must be declared as a sem_t *__ptr128.


Parameters

name
(Input) A pointer to the null-terminated name of the semaphore to be opened. The name should begin with a slash ('/') character. If the name does not begin with a slash ('/') character, the system adds a slash to the beginning of the name.

This parameter is assumed to be represented in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.

The name is added to a set of names that is used only by named semaphores. The name has no relationship to any file system path names. The maximum length of the name is SEM_NAME_MAX.

See QlgSem_open()--Open Named Semaphore (using NLS-enabled path name) for a description and an example of supplying the name in any CCSID.

oflag
(Input) Option flags.

The oflag parameter value is either zero or is obtained by performing an OR operation on one or more of the following constants:

'0x0008' or O_CREAT Creates the named semaphore if it does not already exist.

'0x0010' or O_EXCL Causes sem_open() to fail if O_CREAT is also set and the named semaphore already exists.

mode
(input) Permission flags.

The mode parameter value is either zero or is obtained by performing an OR operation on one or more of the following list of constants. For another process to open the semaphore, the process's effective UIDd must be able to open the semaphore in both read and write mode.

'0x0100' or S_IRUSR Permits the creator of the named semaphore to open the semaphore in read mode.

'0x0080' or S_IWUSR Permits the creator of the named semaphore to open the semaphore in write mode.

'0x0020' or S_IRGRP Permits the group associated with the named semaphore to open the semaphore in read mode.

'0x0010' or S_IWGRP Permits the group associated with the named semaphore to open the semaphore in write mode.

'0x0004' or S_IROTH Permits others to open the named semaphore in read mode.

'0x0002' or S_IWOTH Permits others to open the named semaphore in write mode.


value
(Input) Initial value of the named semaphore.

Authorities

Authorization required for sem_open()

Object Referred to Authority Required errno
Named semaphore to be created None None
Existing named semaphore to be accessed *RW EACCES


Return Value

value sem_open() was successful. The value returned is a pointer to the open named semaphore.

SEM_FAILED sem_open() was not successful. The errno variable is set to indicate the error.


Error Conditions

If sem_open() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

[EACCES]

Permission denied.

An attempt was made to access an object in a way forbidden by its object access permissions.

[EEXIST]

Semaphore exists.

A named semaphore exists for the parameter name, but O_CREAT and O_EXCL are both set in oflag.

[EINVAL]

The value specified for the argument is not correct.

A function was passed incorrect argument values, or an operation was attempted on an object and the operation specified is not supported for that type of object.

An argument value is not valid, out of range, or NULL.

The value parameter is greater than SEM_VALUE_MAX.

[ENAMETOOLONG]

The name is too long. The name is longer than the SEM_NAME_MAX characters.

[ENOENT]

No such path or directory.

The name specified on the sem_open() call does not refer to an existing named semaphore and O_CREAT was not set in oflag.

[ENOSPC]

No space available.

The requested operations required additional space on the device and there is no space left. This also could be caused by exceeding the user profile storage limit when creating or transferring ownership of an object.

Insufficient space remains to hold the intended file, directory, or link.

System semaphore resources have been exhausted.


Error Messages

None.


Related Information


Example

The following example opens the named semaphore "/mysemaphore" and creates the semaphore with an initial value of 10 if it does not already exist. If the semaphore is created, the permissions are set such that only the current user has access to the semaphore.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <semaphore.h>
main() {
  sem_t * my_semaphore;
  int rc;

  my_semaphore = sem_open("/mysemaphore",
                          O_CREAT, S_IRUSR | S_IWUSR, 10);

}


API introduced: V4R4

[ Back to top | UNIX-Type APIs | APIs by category ]