]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/sem_overview.7
Wrapped long lines, wrapped at sentence boundaries; stripped trailing
[thirdparty/man-pages.git] / man7 / sem_overview.7
CommitLineData
2c731798
MK
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.
c13182ef 14.\"
2c731798
MK
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
c13182ef
MK
18.\" the use of the information contained herein.
19.\"
2c731798
MK
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
25sem_overview \- Overview of POSIX semaphores
26.SH DESCRIPTION
27POSIX semaphores allow processes and threads to synchronise their actions.
28
29A semaphore is an integer whose value is never allowed to fall below zero.
c13182ef
MK
30Two operations can be performed on semaphores:
31increment the semaphore value by one
2c731798
MK
32.RB ( sem_post (3));
33and decrement the semaphore value by one
34.RB ( sem_wait (3)).
c13182ef 35If the value of a semaphore is currently zero, then a
2c731798
MK
36.BR sem_wait (3)
37operation will block until the value becomes greater than zero.
38
c13182ef 39POSIX semaphores come in two forms: named semaphores and
2c731798
MK
40unnamed semaphores.
41.SS Named semaphores
c13182ef 42A named semaphore is identified by a name of the form
2c731798 43.IR /somename .
c13182ef 44Two processes can operate on the same named semaphore by passing
2c731798
MK
45the same name to
46.BR sem_open (3).
47
c13182ef 48The
2c731798 49.BR sem_open (3)
c13182ef 50function creates a new named semaphore or opens an existing
2c731798
MK
51named semaphore.
52After the semaphore has been opened, it can be operated on using
53.BR sem_post (3)
54and
55.BR sem_wait (3).
56When a process has finished using the semaphore, it can use
57.BR sem_close (3)
58to close the semaphore.
c13182ef 59When all processes have finished using the semaphore,
2c731798
MK
60it can be removed from the system using
61.BR sem_unlink (3).
62.SS Unnamed semaphores (memory-based semaphores)
63An unnamed semaphore does not have a name.
64Instead the semaphore is placed in a region of memory that
c13182ef 65is shared between multiple threads (a
2c731798 66.IR "thread-shared semaphore" )
c13182ef 67or processes (a
2c731798 68.IR "process-shared semaphore" ).
c13182ef 69A thread-shared semaphore is placed in an area of memory shared
2c731798 70between by the threads of a process, for example, a global variable.
c13182ef
MK
71A process-shared semaphore must be placed in a shared memory region
72(e.g., a System V shared memory segment created using
2c731798
MK
73.BR semget (2),
74or a POSIX shared memory object built created using
75.BR shm_open (3)).
76
77Before being used, an unnamed semaphore must be initialised using
78.BR sem_init (3).
79It can then be operated on using
80.BR sem_post (3)
81and
82.BR sem_wait (3).
83When the semaphore is no longer required,
84and before the memory in which it is located is deallocated,
85the semaphore should be destroyed using
86.BR sem_destroy (3).
87.SH LINUX SPECIFIC DETAILS
88.SS Versions
c13182ef 89Prior to kernel 2.6, Linux only supported unnamed,
2c731798 90thread-shared semaphores.
c13182ef 91On a system with Linux 2.6 and a glibc that provides the NPTL
2c731798
MK
92threading implementation,
93a complete implementation of POSIX semaphores is provided.
94.SS Persistence
95POSIX named semaphores have kernel persistence:
96if not removed by
97.BR sem_unlink (),
98a semaphore will exist until the system is shut down.
99.SS Linking
100Programs using the POSIX semaphores API must be compiled with
101.I cc \-lrt
102to link against the real-time library,
103.IR librt .
104.SS Accessing named semaphores via the file system
c13182ef 105On Linux, named semaphores are created in a virtual file system,
2c731798
MK
106normally mounted under
107.IR /dev/shm ,
c13182ef 108with names of the form
2c731798
MK
109.IR \fBsem.\fPname .
110.SH "CONFORMING TO"
111POSIX.1-2001.
112.SH NOTES
113System V semaphores
114.RB ( semget (2),
115.BR semop (2),
116etc.) are an older semaphore API.
117POSIX semaphores provide a simpler, and better designed interface than
c13182ef
MK
118System V semaphores;
119on the other hand POSIX semaphores are less widely available
2c731798
MK
120(especially on older systems) than System V semaphores.
121.SH EXAMPLE
122An 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),
f58c4bd8
MK
132.BR sem_wait (3),
133.BR pthreads (7)