man-pages/man7/sem_overview.7

134 lines
4.5 KiB
Groff
Raw Normal View History

2006-03-25 20:42:27 +00:00
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
2006-03-25 20:42:27 +00:00
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.
.\"
2006-03-25 20:42:27 +00:00
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
2007-05-30 05:36:26 +00:00
.TH SEM_OVERVIEW 7 2006-03-25 "Linux" "Linux Programmer's Manual"
2006-03-25 20:42:27 +00:00
.SH NAME
sem_overview \- Overview of POSIX semaphores
.SH DESCRIPTION
POSIX semaphores allow processes and threads to synchronise their actions.
A semaphore is an integer whose value is never allowed to fall below zero.
Two operations can be performed on semaphores:
increment the semaphore value by one
2006-03-25 20:42:27 +00:00
.RB ( sem_post (3));
and decrement the semaphore value by one
.RB ( sem_wait (3)).
If the value of a semaphore is currently zero, then a
2006-03-25 20:42:27 +00:00
.BR sem_wait (3)
operation will block until the value becomes greater than zero.
POSIX semaphores come in two forms: named semaphores and
2006-03-25 20:42:27 +00:00
unnamed semaphores.
.SS Named semaphores
A named semaphore is identified by a name of the form
2006-03-25 20:42:27 +00:00
.IR /somename .
Two processes can operate on the same named semaphore by passing
2006-03-25 20:42:27 +00:00
the same name to
.BR sem_open (3).
The
2006-03-25 20:42:27 +00:00
.BR sem_open (3)
function creates a new named semaphore or opens an existing
2006-03-25 20:42:27 +00:00
named semaphore.
After the semaphore has been opened, it can be operated on using
.BR sem_post (3)
and
.BR sem_wait (3).
When a process has finished using the semaphore, it can use
.BR sem_close (3)
to close the semaphore.
When all processes have finished using the semaphore,
2006-03-25 20:42:27 +00:00
it can be removed from the system using
.BR sem_unlink (3).
.SS Unnamed semaphores (memory-based 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
2006-03-25 20:42:27 +00:00
.IR "thread-shared semaphore" )
or processes (a
2006-03-25 20:42:27 +00:00
.IR "process-shared semaphore" ).
A thread-shared semaphore is placed in an area of memory shared
2006-03-25 20:42:27 +00:00
between by the threads of a process, for example, a global variable.
A process-shared semaphore must be placed in a shared memory region
(e.g., a System V shared memory segment created using
2006-03-25 20:42:27 +00:00
.BR semget (2),
or a POSIX shared memory object built created using
.BR shm_open (3)).
Before being used, an unnamed semaphore must be initialised using
.BR sem_init (3).
It can then be operated on using
.BR sem_post (3)
and
.BR sem_wait (3).
When the semaphore is no longer required,
and before the memory in which it is located is deallocated,
the semaphore should be destroyed using
.BR sem_destroy (3).
.SH LINUX SPECIFIC DETAILS
.SS Versions
Prior to kernel 2.6, Linux only supported unnamed,
2006-03-25 20:42:27 +00:00
thread-shared semaphores.
On a system with Linux 2.6 and a glibc that provides the NPTL
2006-03-25 20:42:27 +00:00
threading implementation,
a complete implementation of POSIX semaphores is provided.
.SS Persistence
POSIX named semaphores have kernel persistence:
if not removed by
2007-05-11 23:29:44 +00:00
.BR sem_unlink (3),
2006-03-25 20:42:27 +00:00
a semaphore will exist until the system is shut down.
.SS Linking
Programs using the POSIX semaphores API must be compiled with
.I cc \-lrt
to link against the real-time library,
.IR librt .
.SS Accessing named semaphores via the file system
On Linux, named semaphores are created in a virtual file system,
2006-03-25 20:42:27 +00:00
normally mounted under
.IR /dev/shm ,
with names of the form
2006-03-25 20:42:27 +00:00
.IR \fBsem.\fPname .
.SH "CONFORMING TO"
POSIX.1-2001.
.SH NOTES
System V semaphores
.RB ( semget (2),
.BR semop (2),
etc.) are an older semaphore API.
POSIX semaphores provide a simpler, and better designed interface than
System V semaphores;
on the other hand POSIX semaphores are less widely available
2006-03-25 20:42:27 +00:00
(especially on older systems) than System V semaphores.
.SH EXAMPLE
An example of the use of various POSIX semaphore functions is shown in
.BR sem_wait (3).
.SH "SEE ALSO"
.BR sem_close (3),
.BR sem_destroy (3),
.BR sem_init (3),
.BR sem_getvalue (3),
.BR sem_open (3),
.BR sem_post (3),
.BR sem_unlink (3),
.BR sem_wait (3),
.BR pthreads (7)