sem_open()

Создать или обратиться к именованному семафору

Прототип:

#include <semaphore.h>
sem_t * sem_open( const char *sem_name,
int oflags,
... );

Аргументы:

sem_name
The name of the semaphore that you want to create or access.
oflags
Flags that affect how the function creates a new semaphore. This argument is a combination of:
O_CREAT
O_EXCL

Note: Don't set oflags to O_RDONLY, O_RDWR, or O_WRONLY. A semaphore's behavior is undefined with these flags. The ЗОСРВ «Нейтрино» libraries silently ignore these options, but they may reduce your code's portability.

mode_t
mode If you set O_CREAT in oflag, you must also specify this argument. The semaphore's mode (just like file modes). For portability, you should set the read, write, and execute bits to the same value. An easy way of doing this is to use the constants from <sys/stat.h>:
S_IRWXG
for group access.
S_IRWXO
for other's access.
S_IRWXU
for your own access.
For more information, see Access permissions.
unsigned
int value If you set O_CREAT in oflag, you must also specify this argument. The initial value of the semaphore. A positive value (i.e. greater than zero) indicates an unlocked semaphore, and a value of 0 (zero) indicates a locked semaphore. This value must not exceed SEM_VALUE_MAX.

Библиотека:

libc

Описание:

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.


Note: If you want to use named semaphores, the named-semaphore manager must be running. procnto-* manages named semaphores directly, 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(). You can unlink the semaphore by calling sem_unlink(); when all processes have unlinked the semaphore, it's destroyed.

Named semaphores are always created under /dev/sem:

In either case, slash characters other than the leading slash character aren't interpreted, and the specified name, including these slash characters, is used to identify the semaphore. In other words, additional slashes don't create a directory structure under /dev/sem.

For example, if your current directory is /tmp:

name Pathname space entry
/my_sem /dev/sem/my_sem
my_sem /dev/sem/tmp/my_sem


Note: If you want to create or access a semaphore on another node, 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):

O_CREAT
Create a new named semaphore. If you set this bit, you must provide the mode and value arguments to sem_open().
O_EXCL
When creating a new named semaphore, O_EXCL causes sem_open() to fail if a semaphore with sem_name already exists. Without O_EXCL, sem_open() attaches to an existing semaphore or creates a new one if sem_name doesn't exist.


Note: Don't mix named semaphore operations (sem_open() and sem_close()) with unnamed semaphore operations (sem_init() and sem_destroy()) on the same semaphore.

Возвращаемое значение:

Указатель на успешно созданный или открытый семафор. Если возникла ошибка функция возвращает SEM_FAILED, код ошибки записывается в errno.

Коды ошибок:

EACCES
Either the named semaphore exists and you don't have permission to access it, or you're trying to create a new semaphore and you don't have permission.
EEXIST
You specified O_CREAT and O_EXCL in oflags, but the semaphore already exists.
EINVAL
The sem_name argument is invalid or, when creating a semaphore, value is greater than SEM_VALUE_MAX.
EINTR
The call was interrupted by a signal.
ELOOP
Too many levels of symbolic links or prefixes.
EMFILE
The process is using too many files or semaphores.
ENFILE
The system ran out of resources and couldn't open the semaphore.
ENAMETOOLONG
The sem_name argument is longer than (NAME_MAX - 8).
ENOENT
Either the manager for named semaphores ( mqueue, if procnto-* isn't managing them) isn't running, or the sem_name argument doesn't exist and you didn't specify O_CREAT in oflags.
ENOSPC
There's insufficient space to create a new named semaphore.
ENOSYS
The sem_open() function isn't implemented for the filesystem specified in sem_name.

Классификация:

POSIX 1003.1 SEM

Точка остановки потока
Нет
Обработчик прерываний
Нет
Обработчик сигналов
Да
В потоке
Да

Тематические ссылки:

struct stat, sem_close(), sem_destroy(), sem_init(), sem_post(), sem_trywait(), sem_unlink(), sem_wait()

mqueue, procnto-* в Справочнике по Утилитам




Предыдущий раздел: Описание API системной библиотеки