[man] sem_* (semaphore)

숭글·2022년 9월 24일
0

overview of POSIX semaphores

POSIX semaphores allow processes and threads to synchronize their actions.

A semaphore is an integer whose value is never allowed to fall below zero.

operations

  • sem_post
    : increment the semaphore value.
  • sem_wait
    : decrement the semaphore value.

If the value of a semaphore is currently zero, then a sem_wait operation will block until the value becomes greater than zero.

form of POSIX semaphores

  • named semaphores
    : The sem_open function creates a new named semaphore or opens an existing named semaphore.(by passing it as argument.) After the semaphore has been opened, it can be operated on using sem_post and sem_wait.
    When a process has finished using the semaphore, it can use sem_close to close the semaphore. When all processes have finished using the semaphore, it can be removed from the system using sem_unlink.

  • unnamed semaphores.
    : An unnamed semaphore does not have a name. Instead the semaphore is placed in a region of memory that is shared between multiple threads (a thread-shared semaphore) or processes (a process-shared semaphore). A thread-shared semaphore is placed in an area of memory shared between the threads of a process, for example, a global variable. A process-shared semaphore must be placed in a shared memory region.
    Before being used, an unnamed semaphore must be initialized using sem_init. It can then be operated on using sem_post and sem_wait. When the semaphore is no longer required, and before the memory in which it is located is deallocated, the semaphore should be destroyed using sem_destroy.

Persistence

POSIX named semaphores have kernel persistence: if not removed by sem_unlink, a semaphore will exist until the system is shut down.

Linking

Programs using the POSIX semaphores API must be compiled with cc -pthread to link against the real-time library, librt.

#include <semaphore.h>


sem_open

sem_open() creates a new POSIX semaphore or opens an existing semaphore.

#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */

sem_t *sem_open(const char *name, int oflag);
sem_t *sem_open(const char *name, int oflag, mode_t mode, unsigned int value);

PARAMETERS

  • name
    : identify the semaphore.

  • oflag
    : specifies flags that control the operation of the call. (either O_CREAT or O_EXCL)
    If O_CREAT is specified in oflag, then the semaphore is created if it does not already exist.
    If O_CREAT is specified in oflag, then two additional arguments must be supplied, mode and value.

  • mode
    : specifies the permissions to be placed on the new semaphore, as for open. (Symbolic definitions for the permissions bits can be obtained by including <sys/stat.h>.)(#define ACCESSPERMS (S_IRWXU | S_IRWXG | S_IRWXO))

  • value
    : specifies the initial value for the new semaphore.
    If O_CREAT is specified, and a semaphore with the given name already exists, then mode and value are ignored.

RETURN VALUE

: On success, sem_open() returns the address of the new semaphore; this address is used when calling other semaphore-related functions.
On error, sem_open() returns SEM_FAILED, with errno set to indicate the error.


sem_post

sem_post() increments (unlocks) the semaphore pointed to by sem(mentioned below).

int sem_post(sem_t *sem);

RETURN VALUE
: on success, sempost() returns 0.
_on error
, the value of the semaphore is left unchanged, -1 is returned, and errno is set to indicate the error.


sem_wait

sem_wait() decrements (locks) the semaphore pointed to by sem.

int sem_wait(sem_t *sem);

If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately.
If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement, or a signal handler interrupts the call.

RETURN VALUE
: on success, it returns 0.
on error, the value of the semaphore is left unchanged, -1 is returned, and errno is set to indicate the error.


sem_close

sem_close() closes the named semaphore referred to by sem, allowing any resources that the system has allocated to the calling process for this semaphore to be freed.

int sem_close(sem_t *sem);

RETURN VALUE
: on success, sem_close() returns 0.
on error, -1 is returned, with errno set to indicate the error.


sem_unlink() removes the named semaphore referred to by name.
The semaphore name is removed immediately. The semaphore is destroyed once all other processes that have the semaphore open close it.

int sem_unlink(const char *name);

RETURN VALUE
: on success, sem_unlink() returns 0.
on error, -1 is returned, with errno set to indicate the error.


📖 POSIX semaphores
📖 sem_open
📖 sem_close
📖 sem_unlink
📖 sem_post
📖 sem_wait

profile
Hi!😁 I'm Soongle. Welcome to my Velog!!!

0개의 댓글