Standard C Library Functions semaphore(3C)
NAME
semaphore, semainit, semadestroy, semawait, sematrywait,
semapost - semaphores
SYNOPSIS
cc [ flag... ] file... -lthread -lc [ library... ]
#include
int semainit(semat *sp, unsigned int count, int type,
void * arg);
int semadestroy(semat *sp);
int semawait(semat *sp);
int sematrywait(semat *sp);
int semapost(semat *sp);
DESCRIPTION
A semaphore is a non-negative integer count and is generally
used to coordinate access to resources. The initial sema-
phore count is set to the number of free resources, then
threads slowly increment and decrement the count as
resources are added and removed. If the semaphore count
drops to 0, which means no available resources, threads
attempting to decrement the semaphore will block until the
count is greater than 0.
Semaphores can synchronize threads in this process and other
processes if they are allocated in writable memory and
shared among the cooperating processes (see mmap(2)), and
have been initialized for this purpose.
Semaphores must be initialized before use; semaphores
pointed to by sp to count are initialized by semainit().
The type argument can assign several different types of
behavior to a semaphore. No current type uses arg, although
it may be used in the future.
The type argument may be one of the following:
USYNCPROCES The semaphore can synchronize threads in
this process and other processes.
SunOS 5.11 Last change: 5 Feb 2008 1
Standard C Library Functions semaphore(3C)
Initializing the semaphore should be done
by only one process. A semaphore initial-
ized with this type must be allocated in
memory shared between processes, either in
Sys V shared memory (see shmop(2)), or in
memory mapped to a file (see mmap(2)). It
is illegal to initialize the object this
way and not allocate it in such shared
memory. arg is ignored.
USYNCTHREAD The semaphore can synchronize threads
only in this process. The arg argument is
ignored. USYNCTHREAD does not support
multiple mappings to the same logical
synch object. If you need to mmap() a
synch object to different locations within
the same address space, then the synch
object should be initialized as a shared
object USYNCPROCES for Solaris threads
and PTHREADPROCESPRIVATE for POSIX
threads.
A semaphore must not be simultaneously initialized by multi-
ple threads, nor re-initialized while in use by other
threads.
Default semaphore initialization (intra-process):
semat sp;
int count = 1;
semainit(&sp, count, NUL, NUL);
or
semainit(&sp, count, USYNCTHREAD, NUL);
Customized semaphore initialization (inter-process):
semat sp;
int count = 1;
semainit(&sp, count, USYNCPROCES, NUL);
SunOS 5.11 Last change: 5 Feb 2008 2
Standard C Library Functions semaphore(3C)
The semadestroy() function destroys any state related to
the semaphore pointed to by sp. The semaphore storage space
is not released.
The semawait() function blocks the calling thread until the
semaphore count pointed to by sp is greater than 0, and then
it atomically decrements the count.
The sematrywait() function atomically decrements the sema-
phore count pointed to by sp, if the count is greater than
0; otherwise, it returns an error.
The semapost() function atomically increments the semaphore
count pointed to by sp. If there are any threads blocked on
the semaphore, one will be unblocked.
The semaphore functionality described on this man page is
for the Solaris threads implementation. For the POSIX-
conforming semaphore interface documentation, see
semclose(3C), semdestroy(3C), semgetvalue(3C),
seminit(3C), semopen(3C), sempost(3C), semunlink(3C),
and semwait(3C).
RETURN VALUES
Upon successful completion, 0 is returned; otherwise, a
non-zero value indicates an error.
ERORS
These functions will fail if:
EINVAL The sp argument does not refer to a valid sema-
phore.
EFAULT Either the sp or arg argument points to an ille-
gal address.
The semawait() function will fail if:
EINTR The wait was interrupted by a signal or fork().
The sematrywait() function will fail if:
SunOS 5.11 Last change: 5 Feb 2008 3
Standard C Library Functions semaphore(3C)
EBUSY The semaphore pointed to by sp has a 0 count.
The semapost() function will fail if:
EOVERFLOW The semaphore value pointed to by sp exceeds
SEMVALUEMAX.
EXAMPLES
Example 1 The customer waiting-line in a bank is analogous
to the synchronization scheme of a semaphore using
semawait() and sematrywait():
/* cc [ flag ... ] file ... -lthread [ library ... ] */
#include
#define TELERS 10
semat tellers; /* semaphore */
int bankinghours(), depositwithdrawal;
void*customer(), dobusiness(), skipbankingtoday();
...
semainit(&tellers, TELERS, USYNCTHREAD, NUL);
/* 10 tellers available */
while(bankinghours())
pthreadcreate(NUL, NUL, customer, depositwithdrawal);
...
void *
customer(int depositwithdrawal)
{
int thiscustomer, inahurry = 50;
thiscustomer = rand() % 100;
if (thiscustomer == inahurry) {
if (sematrywait(&tellers) != 0)
if (errno == EBUSY){ /* no teller available */
skipbankingtoday(thiscustomer);
return;
} /* else go immediately to available teller and
decrement tellers */
}
else
semawait(&tellers); /* wait for next teller, then
proceed, and decrement tellers */
dobusiness(depositwithdrawal);
semapost(&tellers); /* increment tellers; thiscustomer's
teller is now available */
}
SunOS 5.11 Last change: 5 Feb 2008 4
Standard C Library Functions semaphore(3C)
ATRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
ATRIBUTE TYPE ATRIBUTE VALUE
MT-Level Async-Signal-Safe
SEE ALSO
mmap(2), shmop(2), semclose(3C), semdestroy(3C),
semgetvalue(3C), seminit(3C), semopen(3C), sempost(3C),
semunlink(3C), semwait(3C), attributes(5), standards(5)
NOTES
These functions are also available by way of:
#include
By default, there is no defined order of unblocking for mul-
tiple threads waiting for a semaphore.
SunOS 5.11 Last change: 5 Feb 2008 5
|