2 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH SEM_INIT 3 2015-03-02 "Linux" "Linux Programmer's Manual"
28 sem_init \- initialize an unnamed semaphore
31 .B #include <semaphore.h>
33 .BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value );
36 Link with \fI\-pthread\fP.
39 initializes the unnamed semaphore at the address pointed to by
43 argument specifies the initial value for the semaphore.
47 argument indicates whether this semaphore is to be shared
48 between the threads of a process, or between processes.
53 then the semaphore is shared between the threads of a process,
54 and should be located at some address that is visible to all threads
55 (e.g., a global variable, or a variable allocated dynamically on
60 is nonzero, then the semaphore is shared between processes,
61 and should be located in a region of shared memory (see
66 (Since a child created by
68 inherits its parent's memory mappings, it can also access the semaphore.)
69 Any process that can access the shared memory region
70 can operate on the semaphore using
75 Initializing a semaphore that has already been initialized
76 results in undefined behavior.
80 on error, \-1 is returned, and
82 is set to indicate the error.
93 but the system does not support process-shared semaphores (see
94 .BR sem_overview (7)).
96 For an explanation of the terms used in this section, see
102 Interface Attribute Value
105 T} Thread safety MT-Safe
110 Bizarrely, POSIX.1-2001 does not specify the value that should
111 be returned by a successful call to
113 POSIX.1-2008 rectifies this, specifying the zero return on success.