Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

sem_destroy(3P4)

sem_unlink(3P4)

sem_lock(3P4)

sem_unlock(3P4)

sem_trylock(3P4)

sem_init(3P4)

NAME

sem_init − establish a connection to a counting semaphore

SYNOPSIS

#include <semaphore.h>

int sem_init(sem,oflag,name,pshared,value)
sem_t  ∗sem;
int    oflag;
char ∗name;
int    pshared;
int    value;

DESCRIPTION

The sem_init routine creates a semaphore descriptor sem that establishes a connection between the calling process and a counting semaphore.  The semaphore descriptor is used to refer to that semaphore when invoking other functions. 

The name argument points to a string naming a semaphore.  The Name does not appear in the file system and is not visible to other functions that take path names as arguments.  name must consist of less than {NAME_MAX} (defined in the file /usr/include/limits.h) characters from the portable file name character set.  Slash (/) characters are not allowed in name.  The current working directory does not affect the interpretation of name.  Processes calling sem_init with the same value of name refer to the same semaphore.  If the name argument is not the name of an existing semaphore and creation of a semaphore is not requested, sem_init will fail and return an error. 

The oflag argument must be set to either O_CREAT or zero. If oflag is set to O_CREAT, a semaphore is created and initialized.  If a semaphore name already exists and O_CREAT is specified, an error is returned.  When a semaphore is created, sem_init will initialize the semaphore to the initial value specified by value.  If oflag is zero, it indicates that the application wishes to attach to the semaphore name, which must already exist.  When oflag is zero, value is ignored. 

The pshared argument must be set to one, or an error is returned. 
 

RETURN VALUE

Upon successful completion, the sem_init function returns a zero.  If any of the following conditions occur, the sem_init function will return -1 and set errno to the corresponding value:

[EINVAL] O_CREAT was not specified, and name is not the name of an existing semaphore. 

[EINVAL] The argument pshared is not equal to 1. 

[EINVAL] The semaphore’s name has a slash character in it. 

[EINVAL] The argument oflag has a flag other than O_CREAT set. 

[EINVAL] The argument oflag has O_CREAT set and value is negative. 

[ENAMETOOLONG]
The semaphore’s name is greater than {NAME_MAX}
 characters.

[EEXIST] The application wishes to create a semaphore which already exists. 

[ENOENT] The application wishes to attach to a semaphore which does not exist. 

[ENOSPC] System resources for the creation of semaphores are unavailable. 
 

FILES

/usr/lib/libposix4.a

SEE ALSO

sem_destroy(3P4), sem_unlink(3P4), sem_lock(3P4), sem_unlock(3P4), sem_trylock(3P4).

WARNING

The interface to sem_init is based on IEEE Draft Standard P1003.4/D12.  This is an unapproved draft, subject to change.  Use of information contained in this unapproved draft is at your own risk.  This interface will change to reflect any changes made by future drafts of POSIX 1003.4. 

CX/UX Programmer’s Reference Manual

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026