]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/sem_overview.7
Add section numbers to page xrefs
[thirdparty/man-pages.git] / man7 / sem_overview.7
1 '\" t
2 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\"
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .TH SEM_OVERVIEW 7 2006-03-25 "Linux 2.6.16" "Linux Programmer's Manual"
24 .SH NAME
25 sem_overview \- Overview of POSIX semaphores
26 .SH DESCRIPTION
27 POSIX semaphores allow processes and threads to synchronise their actions.
28
29 A semaphore is an integer whose value is never allowed to fall below zero.
30 Two operations can be performed on semaphores:
31 increment the semaphore value by one
32 .RB ( sem_post (3));
33 and decrement the semaphore value by one
34 .RB ( sem_wait (3)).
35 If the value of a semaphore is currently zero, then a
36 .BR sem_wait (3)
37 operation will block until the value becomes greater than zero.
38
39 POSIX semaphores come in two forms: named semaphores and
40 unnamed semaphores.
41 .SS Named semaphores
42 A named semaphore is identified by a name of the form
43 .IR /somename .
44 Two processes can operate on the same named semaphore by passing
45 the same name to
46 .BR sem_open (3).
47
48 The
49 .BR sem_open (3)
50 function creates a new named semaphore or opens an existing
51 named semaphore.
52 After the semaphore has been opened, it can be operated on using
53 .BR sem_post (3)
54 and
55 .BR sem_wait (3).
56 When a process has finished using the semaphore, it can use
57 .BR sem_close (3)
58 to close the semaphore.
59 When all processes have finished using the semaphore,
60 it can be removed from the system using
61 .BR sem_unlink (3).
62 .SS Unnamed semaphores (memory-based semaphores)
63 An unnamed semaphore does not have a name.
64 Instead the semaphore is placed in a region of memory that
65 is shared between multiple threads (a
66 .IR "thread-shared semaphore" )
67 or processes (a
68 .IR "process-shared semaphore" ).
69 A thread-shared semaphore is placed in an area of memory shared
70 between by the threads of a process, for example, a global variable.
71 A process-shared semaphore must be placed in a shared memory region
72 (e.g., a System V shared memory segment created using
73 .BR semget (2),
74 or a POSIX shared memory object built created using
75 .BR shm_open (3)).
76
77 Before being used, an unnamed semaphore must be initialised using
78 .BR sem_init (3).
79 It can then be operated on using
80 .BR sem_post (3)
81 and
82 .BR sem_wait (3).
83 When the semaphore is no longer required,
84 and before the memory in which it is located is deallocated,
85 the semaphore should be destroyed using
86 .BR sem_destroy (3).
87 .SH LINUX SPECIFIC DETAILS
88 .SS Versions
89 Prior to kernel 2.6, Linux only supported unnamed,
90 thread-shared semaphores.
91 On a system with Linux 2.6 and a glibc that provides the NPTL
92 threading implementation,
93 a complete implementation of POSIX semaphores is provided.
94 .SS Persistence
95 POSIX named semaphores have kernel persistence:
96 if not removed by
97 .BR sem_unlink (3),
98 a semaphore will exist until the system is shut down.
99 .SS Linking
100 Programs using the POSIX semaphores API must be compiled with
101 .I cc \-lrt
102 to link against the real-time library,
103 .IR librt .
104 .SS Accessing named semaphores via the file system
105 On Linux, named semaphores are created in a virtual file system,
106 normally mounted under
107 .IR /dev/shm ,
108 with names of the form
109 .IR \fBsem.\fPname .
110 .SH "CONFORMING TO"
111 POSIX.1-2001.
112 .SH NOTES
113 System V semaphores
114 .RB ( semget (2),
115 .BR semop (2),
116 etc.) are an older semaphore API.
117 POSIX semaphores provide a simpler, and better designed interface than
118 System V semaphores;
119 on the other hand POSIX semaphores are less widely available
120 (especially on older systems) than System V semaphores.
121 .SH EXAMPLE
122 An example of the use of various POSIX semaphore functions is shown in
123 .BR sem_wait (3).
124 .SH "SEE ALSO"
125 .BR sem_close (3),
126 .BR sem_destroy (3),
127 .BR sem_init (3),
128 .BR sem_getvalue (3),
129 .BR sem_open (3),
130 .BR sem_post (3),
131 .BR sem_unlink (3),
132 .BR sem_wait (3),
133 .BR pthreads (7)