1SEM_OVERVIEW(7) Linux Programmer's Manual SEM_OVERVIEW(7)
2
3
4
6 sem_overview - Overview of POSIX semaphores
7
9 POSIX semaphores allow processes and threads to synchronise their
10 actions.
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. Two
24 processes can operate on the same named semaphore by passing the same
25 name to sem_open(3).
26
27 The sem_open(3) function creates a new named semaphore or opens an
28 existing named semaphore. After the semaphore has been opened, it can
29 be operated on using sem_post(3) and sem_wait(3). When a process has
30 finished using the semaphore, it can use sem_close(3) to close the sem‐
31 aphore. When all processes have finished using the semaphore, it can
32 be removed from the system using sem_unlink(3).
33
34 Unnamed semaphores (memory-based semaphores)
35 An unnamed semaphore does not have a name. Instead the semaphore is
36 placed in a region of memory that is shared between multiple threads (a
37 thread-shared semaphore) or processes (a process-shared semaphore). A
38 thread-shared semaphore is placed in an area of memory shared between
39 by the threads of a process, for example, a global variable. A
40 process-shared semaphore must be placed in a shared memory region
41 (e.g., a System V shared memory segment created using semget(2), or a
42 POSIX shared memory object built created using shm_open(3)).
43
44 Before being used, an unnamed semaphore must be initialised using
45 sem_init(3). It can then be operated on using sem_post(3) and
46 sem_wait(3). When the semaphore is no longer required, and before the
47 memory in which it is located is deallocated, the semaphore should be
48 destroyed using sem_destroy(3).
49
51 Versions
52 Prior to kernel 2.6, Linux only supported unnamed, thread-shared sema‐
53 phores. On a system with Linux 2.6 and a glibc that provides the NPTL
54 threading implementation, a complete implementation of POSIX semaphores
55 is provided.
56
57 Persistence
58 POSIX named semaphores have kernel persistence: if not removed by
59 sem_unlink(), a semaphore will exist until the system is shut down.
60
61 Linking
62 Programs using the POSIX semaphores API must be compiled with cc -lrt
63 to link against the real-time library, librt.
64
65 Accessing named semaphores via the file system
66 On Linux, named semaphores are created in a virtual file system, nor‐
67 mally mounted under /dev/shm, with names of the form sem.name.
68
70 POSIX.1-2001.
71
73 System V semaphores (semget(2), semop(2), etc.) are an older semaphore
74 API. POSIX semaphores provide a simpler, and better designed interface
75 than System V semaphores; on the other hand POSIX semaphores are less
76 widely available (especially on older systems) than System V sema‐
77 phores.
78
80 An example of the use of various POSIX semaphore functions is shown in
81 sem_wait(3).
82
84 sem_close(3), sem_destroy(3), sem_init(3), sem_getvalue(3),
85 sem_open(3), sem_post(3), sem_unlink(3), sem_wait(3), pthreads(7)
86
87
88
89Linux 2.6.16 2006-03-25 SEM_OVERVIEW(7)