245 lines
7.1 KiB
HTML
245 lines
7.1 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of SEM_OVERVIEW</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>SEM_OVERVIEW</H1>
|
|
Section: Linux Programmer's Manual (7)<BR>Updated: 2017-05-03<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
sem_overview - overview of POSIX semaphores
|
|
<A NAME="lbAC"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
POSIX semaphores allow processes and threads to synchronize their actions.
|
|
<P>
|
|
|
|
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
|
|
(<B><A HREF="/cgi-bin/man/man2html?3+sem_post">sem_post</A></B>(3));
|
|
|
|
and decrement the semaphore value by one
|
|
(<B><A HREF="/cgi-bin/man/man2html?3+sem_wait">sem_wait</A></B>(3)).
|
|
|
|
If the value of a semaphore is currently zero, then a
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_wait">sem_wait</A></B>(3)
|
|
|
|
operation will block until the value becomes greater than zero.
|
|
<P>
|
|
|
|
POSIX semaphores come in two forms: named semaphores and
|
|
unnamed semaphores.
|
|
<DL COMPACT>
|
|
<DT id="1"><B>Named semaphores</B>
|
|
|
|
<DD>
|
|
A named semaphore is identified by a name of the form
|
|
<I>/somename</I>;
|
|
|
|
that is, a null-terminated string of up to
|
|
<B>NAME_MAX</B><I>-4</I>
|
|
|
|
(i.e., 251) characters consisting of an initial slash,
|
|
|
|
|
|
|
|
followed by one or more characters, none of which are slashes.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Two processes can operate on the same named semaphore by passing
|
|
the same name to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_open">sem_open</A></B>(3).
|
|
|
|
<DT id="2"><DD>
|
|
The
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_open">sem_open</A></B>(3)
|
|
|
|
function creates a new named semaphore or opens an existing
|
|
named semaphore.
|
|
After the semaphore has been opened, it can be operated on using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_post">sem_post</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_wait">sem_wait</A></B>(3).
|
|
|
|
When a process has finished using the semaphore, it can use
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_close">sem_close</A></B>(3)
|
|
|
|
to close the semaphore.
|
|
When all processes have finished using the semaphore,
|
|
it can be removed from the system using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_unlink">sem_unlink</A></B>(3).
|
|
|
|
<DT id="3"><B>Unnamed semaphores (memory-based semaphores)</B>
|
|
|
|
<DD>
|
|
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
|
|
<I>thread-shared semaphore</I>)
|
|
|
|
or processes (a
|
|
<I>process-shared semaphore</I>).
|
|
|
|
A thread-shared semaphore is placed in an area of memory shared
|
|
between 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
|
|
<B><A HREF="/cgi-bin/man/man2html?2+shmget">shmget</A></B>(2),
|
|
|
|
or a POSIX shared memory object built created using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+shm_open">shm_open</A></B>(3)).
|
|
|
|
<DT id="4"><DD>
|
|
Before being used, an unnamed semaphore must be initialized using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_init">sem_init</A></B>(3).
|
|
|
|
It can then be operated on using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_post">sem_post</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_wait">sem_wait</A></B>(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
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_destroy">sem_destroy</A></B>(3).
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
The remainder of this section describes some specific details
|
|
of the Linux implementation of POSIX semaphores.
|
|
<A NAME="lbAD"> </A>
|
|
<H3>Versions</H3>
|
|
|
|
Prior to kernel 2.6, Linux supported only unnamed,
|
|
thread-shared semaphores.
|
|
On a system with Linux 2.6 and a glibc that provides the NPTL
|
|
threading implementation,
|
|
a complete implementation of POSIX semaphores is provided.
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Persistence</H3>
|
|
|
|
POSIX named semaphores have kernel persistence:
|
|
if not removed by
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_unlink">sem_unlink</A></B>(3),
|
|
|
|
a semaphore will exist until the system is shut down.
|
|
<A NAME="lbAF"> </A>
|
|
<H3>Linking</H3>
|
|
|
|
Programs using the POSIX semaphores API must be compiled with
|
|
<I>cc -pthread</I>
|
|
|
|
to link against the real-time library,
|
|
<I>librt</I>.
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H3>Accessing named semaphores via the filesystem</H3>
|
|
|
|
On Linux, named semaphores are created in a virtual filesystem,
|
|
normally mounted under
|
|
<I>/dev/shm</I>,
|
|
|
|
with names of the form
|
|
<I></I><B>sem.</B>somename.
|
|
|
|
(This is the reason that semaphore names are limited to
|
|
<B>NAME_MAX</B><I>-4</I>
|
|
|
|
rather than
|
|
<B>NAME_MAX</B>
|
|
|
|
characters.)
|
|
<P>
|
|
|
|
Since Linux 2.6.19, ACLs can be placed on files under this directory,
|
|
to control object permissions on a per-user and per-group basis.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
System V semaphores
|
|
(<B><A HREF="/cgi-bin/man/man2html?2+semget">semget</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+semop">semop</A></B>(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
|
|
(especially on older systems) than System V semaphores.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
An example of the use of various POSIX semaphore functions is shown in
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_wait">sem_wait</A></B>(3).
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_close">sem_close</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_destroy">sem_destroy</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_getvalue">sem_getvalue</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_init">sem_init</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_open">sem_open</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_post">sem_post</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_unlink">sem_unlink</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sem_wait">sem_wait</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+shm_overview">shm_overview</A></B>(7)
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="6"><A HREF="#lbAC">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="7"><A HREF="#lbAD">Versions</A><DD>
|
|
<DT id="8"><A HREF="#lbAE">Persistence</A><DD>
|
|
<DT id="9"><A HREF="#lbAF">Linking</A><DD>
|
|
<DT id="10"><A HREF="#lbAG">Accessing named semaphores via the filesystem</A><DD>
|
|
</DL>
|
|
<DT id="11"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="12"><A HREF="#lbAI">EXAMPLE</A><DD>
|
|
<DT id="13"><A HREF="#lbAJ">SEE ALSO</A><DD>
|
|
<DT id="14"><A HREF="#lbAK">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:06:09 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|