# phpman > man > sem_overview(7)

[SEM_OVERVIEW(7)](https://www.chedong.com/phpMan.php/man/SEMOVERVIEW/7/markdown)                       Linux Programmer's Manual                      [SEM_OVERVIEW(7)](https://www.chedong.com/phpMan.php/man/SEMOVERVIEW/7/markdown)



## NAME
       sem_overview - overview of POSIX semaphores

## DESCRIPTION
       POSIX semaphores allow processes and threads to synchronize 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  (**sem**___**[post**(3)](https://www.chedong.com/phpMan.php/man/post/3/markdown));  and
       decrement the semaphore value by one (**sem**___**[wait**(3)](https://www.chedong.com/phpMan.php/man/wait/3/markdown)).  If the value of a semaphore is currently
       zero, then a **sem**___**[wait**(3)](https://www.chedong.com/phpMan.php/man/wait/3/markdown) operation will block until the value becomes greater than zero.

       POSIX semaphores come in two forms: named semaphores and unnamed semaphores.

### Named semaphores
              A named semaphore is identified by a name of the form _/somename_; that is, a  null-ter‐
              minated  string  of  up  to **NAME**___**MAX**_-4_ (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 **sem**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown).

              The **sem**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown) function creates a new named semaphore or opens an existing named sem‐
              aphore.  After the semaphore has been opened, it can be operated on using  **sem**___**[post**(3)](https://www.chedong.com/phpMan.php/man/post/3/markdown)
              and  **sem**___**[wait**(3)](https://www.chedong.com/phpMan.php/man/wait/3/markdown).   When  a  process  has  finished  using  the  semaphore, it can use
              **sem**___**[close**(3)](https://www.chedong.com/phpMan.php/man/close/3/markdown) to close the semaphore.  When all processes have finished using the sema‐
              phore, it can be removed from the system using **sem**___**[unlink**(3)](https://www.chedong.com/phpMan.php/man/unlink/3/markdown).

### Unnamed semaphores (memory-based semaphores)
              An  unnamed  semaphore does not have a name.  Instead the semaphore is placed in a re‐
              gion of memory that is shared between multiple threads (a _thread-shared_ _semaphore_)  or
              processes  (a  _process-shared_  _semaphore_).   A thread-shared semaphore is placed in an
              area of memory shared between the threads of a process, for example,  a  global  vari‐
              able.   A  process-shared  semaphore must be placed in a shared memory region (e.g., a
              System V shared memory segment created using [**shmget**(2)](https://www.chedong.com/phpMan.php/man/shmget/2/markdown), or a POSIX shared  memory  ob‐
              ject built created using **shm**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown)).

              Before being used, an unnamed semaphore must be initialized using **sem**___**[init**(3)](https://www.chedong.com/phpMan.php/man/init/3/markdown).  It can
              then be operated on using **sem**___**[post**(3)](https://www.chedong.com/phpMan.php/man/post/3/markdown) and  **sem**___**[wait**(3)](https://www.chedong.com/phpMan.php/man/wait/3/markdown).   When  the  semaphore  is  no
              longer required, and before the memory in which it is located is deallocated, the sem‐
              aphore should be destroyed using **sem**___**[destroy**(3)](https://www.chedong.com/phpMan.php/man/destroy/3/markdown).

       The remainder of this section describes some specific details of the Linux implementation  of
       POSIX semaphores.

### Versions
       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 imple‐
       mentation of POSIX semaphores is provided.

### Persistence
       POSIX  named semaphores have kernel persistence: if not removed by **sem**___**[unlink**(3)](https://www.chedong.com/phpMan.php/man/unlink/3/markdown), a semaphore
       will exist until the system is shut down.

### Linking
       Programs using the POSIX semaphores API must be compiled with _cc_ _-pthread_ to link against the
       real-time library, _librt_.

### Accessing named semaphores via the filesystem
       On  Linux,  named  semaphores  are  created  in  a virtual filesystem, normally mounted under
       _/dev/shm_, with names of the form **sem.**_somename_.  (This is the reason that semaphore names  are
       limited to **NAME**___**MAX**_-4_ rather than **NAME**___**MAX** characters.)

       Since  Linux 2.6.19, ACLs can be placed on files under this directory, to control object per‐
       missions on a per-user and per-group basis.

## NOTES
       System V semaphores ([**semget**(2)](https://www.chedong.com/phpMan.php/man/semget/2/markdown), [**semop**(2)](https://www.chedong.com/phpMan.php/man/semop/2/markdown), 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  sema‐
       phores.

## EXAMPLES
       An example of the use of various POSIX semaphore functions is shown in **sem**___**[wait**(3)](https://www.chedong.com/phpMan.php/man/wait/3/markdown).

## SEE ALSO
       **sem**___**[close**(3)](https://www.chedong.com/phpMan.php/man/close/3/markdown), **sem**___**[destroy**(3)](https://www.chedong.com/phpMan.php/man/destroy/3/markdown), **sem**___**[getvalue**(3)](https://www.chedong.com/phpMan.php/man/getvalue/3/markdown), **sem**___**[init**(3)](https://www.chedong.com/phpMan.php/man/init/3/markdown), **sem**___**[open**(3)](https://www.chedong.com/phpMan.php/man/open/3/markdown), **sem**___**[post**(3)](https://www.chedong.com/phpMan.php/man/post/3/markdown), **sem**___**un**‐‐
       [**link**(3)](https://www.chedong.com/phpMan.php/man/link/3/markdown), **sem**___**[wait**(3)](https://www.chedong.com/phpMan.php/man/wait/3/markdown), [**pthreads**(7)](https://www.chedong.com/phpMan.php/man/pthreads/7/markdown), **shm**___**[overview**(7)](https://www.chedong.com/phpMan.php/man/overview/7/markdown)

## COLOPHON
       This page is part of release 5.10 of the Linux  _man-pages_  project.   A  description  of  the
       project,  information about reporting bugs, and the latest version of this page, can be found
       at <https://www.kernel.org/doc/man-pages/>.



Linux                                        2020-06-09                              [SEM_OVERVIEW(7)](https://www.chedong.com/phpMan.php/man/SEMOVERVIEW/7/markdown)
