1SEM_INIT(3) Linux Programmer's Manual SEM_INIT(3)
2
3
4
6 sem_init - initialise an unnamed semaphore
7
9 #include <semaphore.h>
10
11 int sem_init(sem_t *sem, int pshared, unsigned int value);
12
14 sem_init() initialises the unnamed semaphore at the address pointed to
15 by sem. The value argument specifies the initial value for the sema‐
16 phore.
17
18 The pshared argument indicates whether this semaphore is to be shared
19 between the threads of a process, or between processes.
20
21 If pshared has the value 0, then the semaphore is shared between the
22 threads of a process, and should be located at some address that is
23 visible to all threads (e.g., a global variable, or a variable allo‐
24 cated dynamically on the heap).
25
26 If pshared is non-zero, then the semaphore is shared between processes,
27 and should be located in a region of shared memory (see shm_open(3),
28 mmap(2), and shmget(2)). (Since a child created by fork(2) inherits
29 its parent's memory mappings, it can also access the semaphore.) Any
30 process that can access the shared memory region can operate on the
31 semaphore using sem_post(3), sem_wait(3), etc.
32
33 Initialising a semaphore that has already been initialised results in
34 undefined behaviour.
35
37 sem_init() returns 0 on success; on error, -1 is returned, and errno is
38 set to indicate the error.
39
41 EINVAL value exceeds SEM_VALUE_MAX.
42
43 ENOSYS pshared is non-zero, but the system does not support process-
44 shared semaphores (see sem_overview(7)).
45
47 POSIX.1-2001.
48
50 Bizarrely, POSIX.1-2001 does not specify the value that should be
51 returned by a successful call to sem_init(). However, virtually every
52 implementation returns zero in this case.
53
55 sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7)
56
57
58
59Linux 2.6.16 2006-03-25 SEM_INIT(3)