sema_init(3synch) sema_init(3synch)
NAME
sema_init - initialize a semaphore
SYNOPSIS
cc [options] -Kthread file
#include <synch.h>
int sema_init(sema_t *sema, int sema_count, int type, void *arg);
Parameters
sema pointer to semaphore to initialize
sema_count number of resources to be protected by the
semaphore
type USYNC_THREAD or USYNC_PROCESS
arg NULL (reserved for future use)
DESCRIPTION
sema_init initializes the semaphore sema of type type to
protect sema_count resources. Once initialized, the semaphore
can be used any number of times without being re-initialized.
sema_count Parameter
sema_count, which must be greater than or equal to zero,
defines the initial count of resources protected by the
semaphore.
sema Parameter
sema points to the semaphore to be initialized.
type Parameter
type can be set to one of the following values:
USYNC_THREAD Initialize the semaphore for threads within
the current process.
USYNC_PROCESS Initialize the semaphore for threads across
processes.
arg Parameter
arg should be set to NULL. It is not currently used, but is
reserved for future use.
Copyright 1994 Novell, Inc. Page 1
sema_init(3synch) sema_init(3synch)
Static Semaphore Initialization
A semaphore can be initialized statically if its storage is
zero-filled. In this case, the semaphore is of type
USYNC_THREAD, its sema_count is 0 (that is, it is ``locked'';
no resources are available), and sema_init need not be called.
sema_post must be called to unlock the semaphore.
Return Values
sema_init returns zero for success and an error number for
failure, as described below.
Errors
If any of the following conditions is detected, sema_init
returns the corresponding value:
EINVAL Invalid argument specified.
USAGE
Warnings
sema_init does not examine the sema argument before
initializing it. If sema_init is called more than once for
the same semaphore, it will overwrite its state. It is the
user's responsibility to ensure that sema_init is only called
once for each semaphore.
Operations on semaphores initialized with sema_init are not
recursive; a thread can block itself if it attempts to
reacquire a semaphore that it has already acquired.
REFERENCES
semaphore(3synch), sema_destroy(3synch), sema_post(3synch),
sema_trywait(3synch), sema_wait(3synch), synch(3synch)
Copyright 1994 Novell, Inc. Page 2