sema_init(3synch)


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(3synch), semaphore(3synch), sema_destroy(3synch), sema_post(3synch), sema_trywait(3synch), sema_wait(3synch)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004