1SEM_INIT(3P) POSIX Programmer's Manual SEM_INIT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 sem_init - initialize an unnamed semaphore (REALTIME)
13
15 #include <semaphore.h>
16
17 int sem_init(sem_t *sem, int pshared, unsigned value);
18
19
21 The sem_init() function shall initialize the unnamed semaphore referred
22 to by sem. The value of the initialized semaphore shall be value. Fol‐
23 lowing a successful call to sem_init(), the semaphore may be used in
24 subsequent calls to sem_wait(), sem_trywait(), sem_post(), and
25 sem_destroy(). This semaphore shall remain usable until the semaphore
26 is destroyed.
27
28 If the pshared argument has a non-zero value, then the semaphore is
29 shared between processes; in this case, any process that can access the
30 semaphore sem can use sem for performing sem_wait(), sem_trywait(),
31 sem_post(), and sem_destroy() operations.
32
33 Only sem itself may be used for performing synchronization. The result
34 of referring to copies of sem in calls to sem_wait(), sem_trywait(),
35 sem_post(), and sem_destroy() is undefined.
36
37 If the pshared argument is zero, then the semaphore is shared between
38 threads of the process; any thread in this process can use sem for per‐
39 forming sem_wait(), sem_trywait(), sem_post(), and sem_destroy() opera‐
40 tions. The use of the semaphore by threads other than those created in
41 the same process is undefined.
42
43 Attempting to initialize an already initialized semaphore results in
44 undefined behavior.
45
47 Upon successful completion, the sem_init() function shall initialize
48 the semaphore in sem. Otherwise, it shall return -1 and set errno to
49 indicate the error.
50
52 The sem_init() function shall fail if:
53
54 EINVAL The value argument exceeds {SEM_VALUE_MAX}.
55
56 ENOSPC A resource required to initialize the semaphore has been
57 exhausted, or the limit on semaphores ( {SEM_NSEMS_MAX}) has
58 been reached.
59
60 EPERM The process lacks the appropriate privileges to initialize the
61 semaphore.
62
63
64 The following sections are informative.
65
67 None.
68
70 The sem_init() function is part of the Semaphores option and need not
71 be available on all implementations.
72
74 Although this volume of IEEE Std 1003.1-2001 fails to specify a suc‐
75 cessful return value, it is likely that a later version may require the
76 implementation to return a value of zero if the call to sem_init() is
77 successful.
78
80 None.
81
83 sem_destroy(), sem_post(), sem_timedwait(), sem_trywait(), sem_wait(),
84 the Base Definitions volume of IEEE Std 1003.1-2001, <semaphore.h>
85
87 Portions of this text are reprinted and reproduced in electronic form
88 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
89 -- Portable Operating System Interface (POSIX), The Open Group Base
90 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
91 Electrical and Electronics Engineers, Inc and The Open Group. In the
92 event of any discrepancy between this version and the original IEEE and
93 The Open Group Standard, the original IEEE and The Open Group Standard
94 is the referee document. The original Standard can be obtained online
95 at http://www.opengroup.org/unix/online.html .
96
97
98
99IEEE/The Open Group 2003 SEM_INIT(3P)