sem_post(2) sem_post(2)
NAME [Toc] [Back]
sem_post - unlock a POSIX semaphore
SYNOPSIS [Toc] [Back]
#include <sys/semaphore.h>
int sem_post(sem_t *sem);
DESCRIPTION [Toc] [Back]
sem_post() is used to post the semaphore referenced by sem. The
calling thread will not return from its call to sem_post() unless it
can either: increment the semaphore value, if there are no blocked
threads on this semaphore; give the semaphore to a blocked thread, if
there are any blocked threads on this semaphore; or have an error
condition.
If the semaphore value is < 0, the semaphore has blocked threads,
waiting for it to become available (the absolute value of the
semaphore's value indicates the number of waiters at that moment). If
the semaphore value is >= 0, the semaphore has no waiters.
If the semaphore has no waiters at the time its value is checked, the
semaphore's value will be atomically incremented, with respect to the
checking of its value, up to its maximum value as specified by
{_POSIX_SEM_VALUE_MAX}. If the semaphore has waiters at the time its
value is checked, the semaphore value is not changed. Instead, the
calling thread will attempt to wake up a waiter. If the semaphore has
waiters having realtime priorities, the thread must wake up the
highest priority waiter. Otherwise the thread at the head of the
channel queue is woken up.
When a waiter is successfully woken, the semaphore being posted will
be given to the woken waiter. In other words, the state of the
semaphore remains unchanged. Instead, the semaphore being posted will
be inherited by the waiter being woken from this call to sem_post().
If the specified semaphore referred to by sem is a named semaphore,
then this semaphore must have been opened by the calling process with
sem_open(). The calling process must have both read and write
permissions on the semaphore to perform this operation. The
sem_post() routine may be called asynchronously, i.e. from a signal
handler.
To use this function, link in the realtime library by specifying -lrt
on the compiler or linker command line.
EXAMPLES [Toc] [Back]
The following call to sem_post() will post the semaphore sem.
sem_post(sem);
Hewlett-Packard Company - 1 - HP-UX 11i Version 2: August 2003
sem_post(2) sem_post(2)
RETURN VALUE [Toc] [Back]
A successful call to sem_post() will return 0 and the calling thread
would have posted the semaphore. Otherwise, the call to sem_post()
will return -1 with errno set to the appropriate value of the error
condition.
ERRORS [Toc] [Back]
sem_post() fails and does not perform the requested operation if any
of the following conditions are encountered:
[EPERM] The calling process does not have the privileges
necessary to post the semaphore.
[EINVAL] The argument sem does not refer to a valid
semaphore.
SEE ALSO [Toc] [Back]
<semaphore.h>.
STANDARDS CONFORMANCE [Toc] [Back]
sem_post(): POSIX
Hewlett-Packard Company - 2 - HP-UX 11i Version 2: August 2003 [ Back ] |