# man > sem_overview(7)

[_sem_overview_(7)](https://www.chedong.com/phpMan.php/man/semoverview/7/markdown)                   Miscellaneous Information 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/sempost/3/markdown));  and
       decrement the semaphore value by one ([**sem_wait**(3)](https://www.chedong.com/phpMan.php/man/semwait/3/markdown)).  If the value of a semaphore is currently
       zero, then a [**sem_wait**(3)](https://www.chedong.com/phpMan.php/man/semwait/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/semopen/3/markdown).

              The [**sem_open**(3)](https://www.chedong.com/phpMan.php/man/semopen/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/sempost/3/markdown)
              and  [**sem_wait**(3)](https://www.chedong.com/phpMan.php/man/semwait/3/markdown).   When  a  process  has  finished  using  the  semaphore, it can use
              [**sem_close**(3)](https://www.chedong.com/phpMan.php/man/semclose/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/semunlink/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/shmopen/3/markdown)).

              Before being used, an unnamed semaphore must be initialized using [**sem_init**(3)](https://www.chedong.com/phpMan.php/man/seminit/3/markdown).  It can
              then be operated on using [**sem_post**(3)](https://www.chedong.com/phpMan.php/man/sempost/3/markdown) and  [**sem_wait**(3)](https://www.chedong.com/phpMan.php/man/semwait/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/semdestroy/3/markdown).

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

### Versions
       Before Linux 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 implementa‐
       tion 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/semunlink/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/semwait/3/markdown).

## SEE ALSO
       [**sem_close**(3)](https://www.chedong.com/phpMan.php/man/semclose/3/markdown), [**sem_destroy**(3)](https://www.chedong.com/phpMan.php/man/semdestroy/3/markdown), [**sem_getvalue**(3)](https://www.chedong.com/phpMan.php/man/semgetvalue/3/markdown), [**sem_init**(3)](https://www.chedong.com/phpMan.php/man/seminit/3/markdown), [**sem_open**(3)](https://www.chedong.com/phpMan.php/man/semopen/3/markdown), [**sem_post**(3)](https://www.chedong.com/phpMan.php/man/sempost/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/semwait/3/markdown), [**pthreads**(7)](https://www.chedong.com/phpMan.php/man/pthreads/7/markdown), [**shm_overview**(7)](https://www.chedong.com/phpMan.php/man/shmoverview/7/markdown)

Linux man-pages 6.7                          2023-10-31                              [_sem_overview_(7)](https://www.chedong.com/phpMan.php/man/semoverview/7/markdown)
