Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

mmap(2)

semaphore(3T)

NAME

semaphore, sema_init, sema_destroy, sema_wait, sema_trywait, sema_post − semaphores

SYNOPSIS

#include <synch.h>

int sema_init(sema_t ∗sp, unsigned int count, int type, void ∗ arg);

int sema_destroy(sema_t ∗sp);

int sema_wait(sema_t ∗sp);

int sema_trywait(sema_t ∗sp);

int sema_post(sema_t ∗sp);

MT-LEVEL

MT-Safe

DESCRIPTION

Conceptually, a semaphore is a non-negative integer count.  Semaphores are typically used to coordinate access to resources.  The semaphore count is initialized to the number of free resources.  Threads then atomically increment the count when resources are added and atomically decrement the count when resources are removed.  When the semaphore count becomes zero, indicating no more resources are present, threads trying to decrement the semaphore will block until the count becomes greater than zero. 

Semaphores can be used to synchronize threads in this process and other processes if they are allocated in memory that is writable and is shared among the cooperating processes (see mmap(2)) and have been initialized for this behavior. 

Semaphores must be initialized before use.  sema_init() initializes the semaphore pointed to by sp to count. A semaphore can potentially have several different types of behavior, specified by type. Each type may specify additional behavior parameters via arg. type may be one of the following:

USYNC_PROCESS The semaphore may be used to synchronize threads in this process and other processes.  Only one process should initialize the semaphore.  arg is ignored. 

USYNC_THREAD The semaphore may be used to synchronize only threads in this process.  arg is ignored. 

Multiple threads must not initialize the same semaphore simultaneously.  A semaphore must not be re-initialized while other threads may be using it. 

sema_destroy() destroys any state associated with the semaphore pointed to by sp. The storage for the semaphore itself is unaffected.

sema_wait() blocks the calling thread until the count in the semaphore pointed to by sp becomes greater than zero and then atomically decrements it. 

sema_trywait() atomically decrements the count in the semaphore pointed to by sp if the count is greater than zero.  Otherwise it returns an error. 

sema_post() atomically increments the count semaphore pointed to by sp. If there are any threads blocked on the semaphore, one is unblocked.

RETURN VALUES

Zero is returned when successful.  A non-zero value indicates an error. 

ERRORS

If any of the following conditions are detected, these functions fail and return the corresponding value:

EINVAL Invalid argument. 

EFAULT sp or arg point to an illegal address. 

If any of the following conditions occur, sema_wait() returns the corresponding value:

EINTR The wait was interrupted by a signal or fork(). 

If any of the following conditions occur, sema_trywait() returns the corresponding value:

EBUSY The semaphore pointed to by sp has a zero count. 

SEE ALSO

mmap(2). 

NOTES

These interfaces also available via:

#include <thread.h>

By default, there is no defined order of unblocking if multiple threads are waiting for a semaphore. 

SunOS 5.2  —  Last change: 22 Jan 1993

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