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 -lrt or -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), etc.
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

CONFORMING TO

49       POSIX.1-2001.
50

NOTES

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

SEE ALSO

57       sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7)
58

COLOPHON

60       This  page  is  part of release 3.25 of the Linux man-pages project.  A
61       description of the project, and information about reporting  bugs,  can
62       be found at http://www.kernel.org/doc/man-pages/.
63
64
65
66Linux                             2008-07-27                       SEM_INIT(3)
Impressum