1SEM_INIT(3)                Linux Programmer's Manual               SEM_INIT(3)
2
3
4

NAME

6       sem_init - initialize an unnamed semaphore
7

SYNOPSIS

9       #include <semaphore.h>
10
11       int sem_init(sem_t *sem, int pshared, unsigned int value);
12
13       Link with -pthread.
14

DESCRIPTION

16       sem_init()  initializes the unnamed semaphore at the address pointed to
17       by sem.  The value argument specifies the initial value for  the  sema‐
18       phore.
19
20       The  pshared  argument indicates whether this semaphore is to be shared
21       between the threads of a process, or between processes.
22
23       If pshared has the value 0, then the semaphore is  shared  between  the
24       threads  of  a  process,  and should be located at some address that is
25       visible to all threads (e.g., a global variable, or  a  variable  allo‐
26       cated dynamically on the heap).
27
28       If  pshared is nonzero, then the semaphore is shared between processes,
29       and should be located in a region of shared  memory  (see  shm_open(3),
30       mmap(2),  and  shmget(2)).   (Since a child created by fork(2) inherits
31       its parent's memory mappings, it can also access the  semaphore.)   Any
32       process  that  can  access  the shared memory region can operate on the
33       semaphore using sem_post(3), sem_wait(3), and so on.
34
35       Initializing a semaphore that has already been initialized  results  in
36       undefined behavior.
37

RETURN VALUE

39       sem_init() returns 0 on success; on error, -1 is returned, and errno is
40       set to indicate the error.
41

ERRORS

43       EINVAL value exceeds SEM_VALUE_MAX.
44
45       ENOSYS pshared is nonzero, but the system  does  not  support  process-
46              shared semaphores (see sem_overview(7)).
47

ATTRIBUTES

49       For   an   explanation   of   the  terms  used  in  this  section,  see
50       attributes(7).
51
52       ┌───────────┬───────────────┬─────────┐
53Interface  Attribute     Value   
54       ├───────────┼───────────────┼─────────┤
55sem_init() │ Thread safety │ MT-Safe │
56       └───────────┴───────────────┴─────────┘

CONFORMING TO

58       POSIX.1-2001.
59

NOTES

61       Bizarrely, POSIX.1-2001 does not  specify  the  value  that  should  be
62       returned  by  a  successful call to sem_init().  POSIX.1-2008 rectifies
63       this, specifying the zero return on success.
64

SEE ALSO

66       sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7)
67

COLOPHON

69       This page is part of release 4.16 of the Linux  man-pages  project.   A
70       description  of  the project, information about reporting bugs, and the
71       latest    version    of    this    page,    can     be     found     at
72       https://www.kernel.org/doc/man-pages/.
73
74
75
76Linux                             2017-09-15                       SEM_INIT(3)
Impressum