This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs. |
Create or access a named semaphore
#include <semaphore.h> sem_t * sem_open( const char * sem_name, int oflags, ... );
Don't set oflags to O_RDONLY, O_RDWR, or O_WRONLY. A semaphore's behavior is undefined with these flags. The QNX libraries silently ignore these options, but they may reduce your code's portability. |
For more information, see below.
If you set O_CREAT in oflags, you must also pass the following arguments:
For more information, see "Access permissions" in the documentation for stat().
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The sem_open() function creates or accesses a named semaphore. Named semaphores are slower than the unnamed semaphores created with sem_init(). Semaphores persist as long as the system is up.
If you want to use named semaphores, the named-semaphore manager must be running. Starting with release 6.3.0, procnto, manages named semaphores, which mqueue used to do (and still does, if it detects that procnto isn't doing so). |
The sem_open() function returns a semaphore descriptor that you can use with sem_wait(), sem_trywait(), and sem_post(). You can use it until you call sem_close().
The sem_name argument is interpreted as follows:
name | Pathname space entry |
---|---|
entry | CWD/entry |
/entry | /dev/sem/entry |
entry/newentry | CWD/entry/newentry |
/entry/newentry | /entry/newentry |
where CWD is the current working directory for the program at the point that it calls sem_open().
If you want to create or access a semaphore on another node, you have to specify the name as /net/node/sem_location. |
The oflags argument is used only for semaphore creation. When creating a new semaphore, you can set oflags to O_CREAT or (O_CREAT|O_EXCL):
Don't mix named semaphore operations (sem_open() and sem_close()) with unnamed semaphore operations (sem_init() and sem_destroy()) on the same semaphore. |
A pointer to the created or accessed semaphore, or -1 for failure (errno is set).
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
sem_close(), sem_destroy(), sem_init(), sem_post(), sem_trywait(), sem_unlink(), sem_wait()
mqueue, procnto* in the Utilities Reference