sema_init(SYNCH)
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);
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 reinitialized.
Parameters
sema-
Points to the semaphore to be initialized.
sema_count-
Specifies the initial number of resources to be protected by the semaphore.
sema_count must be greater than or equal to zero.
type-
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-
arg should be set to NULL.
It is not currently used, but is reserved for future use.
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.
Errors
If the following condition is detected,
sema_init returns the corresponding value:
EINVAL-
invalid argument specified
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
Intro(SYNCH),
semaphore(SYNCH),
sema_destroy(SYNCH),
sema_post(SYNCH),
sema_trywait(SYNCH),
sema_wait(SYNCH)
© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 - 01 June 2005