1sem_init(3C) Standard C Library Functions sem_init(3C)
2
3
4
6 sem_init - initialize an unnamed semaphore
7
9 #include <semaphore.h>
10
11 int sem_init(sem_t *sem, int pshared, unsigned int value);
12
13
15 The sem_init() function is used to initialize the unnamed semaphore
16 referred to by sem. The value of the initialized semaphore is value.
17 Following a successful call to sem_init(), the semaphore may be used in
18 subsequent calls to sem_wait(3C), sem_trywait(3C), sem_post(3C), and
19 sem_destroy(3C). This semaphore remains usable until the semaphore is
20 destroyed.
21
22
23 If the pshared argument has a non-zero value, then the semaphore is
24 shared between processes; in this case, any process that can access the
25 semaphore sem can use sem for performing sem_wait(3C), sem_trywait(3C),
26 sem_post(3C), and sem_destroy(3C) operations.
27
28
29 Only sem itself may be used for performing synchronization. The result
30 of referring to copies of sem in calls to sem_wait(3C), sem_try‐
31 wait(3C), sem_post(3C), and sem_destroy(3C), is undefined.
32
33
34 If the pshared argument is zero, then the semaphore is shared between
35 threads of the process; any thread in this process can use sem for per‐
36 forming sem_wait(3C), sem_trywait(3C), sem_post(3C), and
37 sem_destroy(3C) operations. The use of the semaphore by threads other
38 than those created in the same process is undefined.
39
40
41 Attempting to initialize an already initialized semaphore results in
42 undefined behavior.
43
44
45 The sem_open(3C) function is used with named semaphores.
46
48 Upon successful completion, the function initializes the semaphore in
49 sem. Otherwise, it returns −1 and sets errno to indicate the error.
50
52 The sem_init() function will fail if:
53
54 EINVAL The value argument exceeds SEM_VALUE_MAX.
55
56
57 ENOSPC A resource required to initialize the semaphore has been
58 exhausted, or the resources have reached the limit on sema‐
59 phores (SEM_NSEMS_MAX).
60
61
62 ENOSYS The sem_init() function is not supported by the system.
63
64
65 EPERM The process lacks the appropriate privileges to initialize
66 the semaphore.
67
68
70 See attributes(5) for descriptions of the following attributes:
71
72
73
74
75 ┌─────────────────────────────┬─────────────────────────────┐
76 │ATTRIBUTE TYPE │ATTRIBUTE VALUE │
77 ├─────────────────────────────┼─────────────────────────────┤
78 │Interface Stability │Committed │
79 ├─────────────────────────────┼─────────────────────────────┤
80 │MT-Level │MT-Safe │
81 ├─────────────────────────────┼─────────────────────────────┤
82 │Standard │See standards(5). │
83 └─────────────────────────────┴─────────────────────────────┘
84
86 sem_destroy(3C), sem_open(3C), sem_post(3C), sem_wait(3C),
87 attributes(5), standards(5)
88
89
90
91SunOS 5.11 9 Jul 2009 sem_init(3C)