1SEM_OVERVIEW(7)            Linux Programmer's Manual           SEM_OVERVIEW(7)
2
3
4

NAME

6       sem_overview - overview of POSIX semaphores
7

DESCRIPTION

9       POSIX  semaphores  allow processes and threads to synchronize their ac‐
10       tions.
11
12       A semaphore is an integer whose value is never allowed  to  fall  below
13       zero.   Two  operations  can  be performed on semaphores: increment the
14       semaphore value by one (sem_post(3)); and decrement the semaphore value
15       by  one  (sem_wait(3)).  If the value of a semaphore is currently zero,
16       then a sem_wait(3) operation will block until the value becomes greater
17       than zero.
18
19       POSIX  semaphores come in two forms: named semaphores and unnamed sema‐
20       phores.
21
22       Named semaphores
23              A named semaphore is identified by a name of the form /somename;
24              that  is,  a  null-terminated  string of up to NAME_MAX-4 (i.e.,
25              251) characters consisting of an initial slash, followed by  one
26              or  more  characters,  none of which are slashes.  Two processes
27              can operate on the same named semaphore by passing the same name
28              to sem_open(3).
29
30              The  sem_open(3) function creates a new named semaphore or opens
31              an existing named  semaphore.   After  the  semaphore  has  been
32              opened, it can be operated on using sem_post(3) and sem_wait(3).
33              When a process has finished using  the  semaphore,  it  can  use
34              sem_close(3)  to  close  the semaphore.  When all processes have
35              finished using the semaphore, it can be removed from the  system
36              using sem_unlink(3).
37
38       Unnamed semaphores (memory-based semaphores)
39              An  unnamed  semaphore  does not have a name.  Instead the sema‐
40              phore is placed in a region of memory  that  is  shared  between
41              multiple  threads  (a  thread-shared  semaphore) or processes (a
42              process-shared semaphore).  A thread-shared semaphore is  placed
43              in  an  area  of memory shared between the threads of a process,
44              for example, a global variable.  A process-shared semaphore must
45              be  placed  in  a  shared memory region (e.g., a System V shared
46              memory segment created using shmget(2), or a POSIX shared memory
47              object built created using shm_open(3)).
48
49              Before  being used, an unnamed semaphore must be initialized us‐
50              ing sem_init(3).  It can then be operated on  using  sem_post(3)
51              and  sem_wait(3).  When the semaphore is no longer required, and
52              before the memory in which it is  located  is  deallocated,  the
53              semaphore should be destroyed using sem_destroy(3).
54
55       The  remainder  of  this section describes some specific details of the
56       Linux implementation of POSIX semaphores.
57
58   Versions
59       Prior to kernel 2.6, Linux supported only unnamed, thread-shared  sema‐
60       phores.   On a system with Linux 2.6 and a glibc that provides the NPTL
61       threading implementation, a complete implementation of POSIX semaphores
62       is provided.
63
64   Persistence
65       POSIX  named  semaphores  have  kernel  persistence:  if not removed by
66       sem_unlink(3), a semaphore will exist until the system is shut down.
67
68   Linking
69       Programs using the POSIX  semaphores  API  must  be  compiled  with  cc
70       -pthread to link against the real-time library, librt.
71
72   Accessing named semaphores via the filesystem
73       On  Linux,  named  semaphores are created in a virtual filesystem, nor‐
74       mally mounted under /dev/shm, with  names  of  the  form  sem.somename.
75       (This  is  the  reason  that  semaphore names are limited to NAME_MAX-4
76       rather than NAME_MAX characters.)
77
78       Since Linux 2.6.19, ACLs can be placed on files under  this  directory,
79       to control object permissions on a per-user and per-group basis.
80

NOTES

82       System  V semaphores (semget(2), semop(2), etc.) are an older semaphore
83       API.  POSIX semaphores provide a simpler, and better designed interface
84       than  System  V semaphores; on the other hand POSIX semaphores are less
85       widely available (especially on older  systems)  than  System  V  sema‐
86       phores.
87

EXAMPLES

89       An  example of the use of various POSIX semaphore functions is shown in
90       sem_wait(3).
91

SEE ALSO

93       sem_close(3),     sem_destroy(3),     sem_getvalue(3),     sem_init(3),
94       sem_open(3),   sem_post(3),  sem_unlink(3),  sem_wait(3),  pthreads(7),
95       shm_overview(7)
96

COLOPHON

98       This page is part of release 5.10 of the Linux  man-pages  project.   A
99       description  of  the project, information about reporting bugs, and the
100       latest    version    of    this    page,    can     be     found     at
101       https://www.kernel.org/doc/man-pages/.
102
103
104
105Linux                             2020-06-09                   SEM_OVERVIEW(7)
Impressum