1 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH SEM_INIT 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
7 sem_init \- initialize an unnamed semaphore
10 .RI ( libpthread ", " \-lpthread )
13 .B #include <semaphore.h>
15 .BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value );
19 initializes the unnamed semaphore at the address pointed to by
23 argument specifies the initial value for the semaphore.
27 argument indicates whether this semaphore is to be shared
28 between the threads of a process, or between processes.
33 then the semaphore is shared between the threads of a process,
34 and should be located at some address that is visible to all threads
35 (e.g., a global variable, or a variable allocated dynamically on
40 is nonzero, then the semaphore is shared between processes,
41 and should be located in a region of shared memory (see
46 (Since a child created by
48 inherits its parent's memory mappings, it can also access the semaphore.)
49 Any process that can access the shared memory region
50 can operate on the semaphore using
55 Initializing a semaphore that has already been initialized
56 results in undefined behavior.
60 on error, \-1 is returned, and
62 is set to indicate the error.
73 but the system does not support process-shared semaphores (see
74 .BR sem_overview (7)).
76 For an explanation of the terms used in this section, see
84 Interface Attribute Value
87 T} Thread safety MT-Safe
95 Bizarrely, POSIX.1-2001 does not specify the value that should
96 be returned by a successful call to
98 POSIX.1-2008 rectifies this, specifying the zero return on success.