]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/sem_init.3
_exit.2, bpf.2, cacheflush.2, capget.2, chdir.2, chmod.2, chroot.2, clock_getres...
[thirdparty/man-pages.git] / man3 / sem_init.3
CommitLineData
2c731798 1'\" t
c11b1abf 2.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2c731798 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
2c731798
MK
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.
8.\"
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.
c13182ef 13.\"
2c731798
MK
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
10d76543
MK
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
20.\" professionally.
c13182ef 21.\"
2c731798
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
2c731798 25.\"
fe0fefbf 26.TH SEM_INIT 3 2015-03-02 "Linux" "Linux Programmer's Manual"
2c731798 27.SH NAME
d9bfdb9c 28sem_init \- initialize an unnamed semaphore
2c731798
MK
29.SH SYNOPSIS
30.nf
31.B #include <semaphore.h>
68e4db0a 32.PP
2c731798
MK
33.BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value );
34.fi
68e4db0a 35.PP
2c5f8c8c 36Link with \fI\-pthread\fP.
2c731798
MK
37.SH DESCRIPTION
38.BR sem_init ()
d9bfdb9c 39initializes the unnamed semaphore at the address pointed to by
2c731798
MK
40.IR sem .
41The
42.I value
43argument specifies the initial value for the semaphore.
44
c13182ef 45The
2c731798
MK
46.I pshared
47argument indicates whether this semaphore is to be shared
48between the threads of a process, or between processes.
49
50If
51.I pshared
c13182ef
MK
52has the value 0,
53then the semaphore is shared between the threads of a process,
54and should be located at some address that is visible to all threads
55(e.g., a global variable, or a variable allocated dynamically on
2c731798
MK
56the heap).
57
58If
59.I pshared
c7094399 60is nonzero, then the semaphore is shared between processes,
2c731798
MK
61and should be located in a region of shared memory (see
62.BR shm_open (3),
63.BR mmap (2),
64and
65.BR shmget (2)).
66(Since a child created by
67.BR fork (2)
68inherits its parent's memory mappings, it can also access the semaphore.)
c13182ef 69Any process that can access the shared memory region
2c731798
MK
70can operate on the semaphore using
71.BR sem_post (3),
72.BR sem_wait (3),
f78ed33a 73and so on.
2c731798 74
d9bfdb9c
MK
75Initializing a semaphore that has already been initialized
76results in undefined behavior.
2c731798
MK
77.SH RETURN VALUE
78.BR sem_init ()
c13182ef 79returns 0 on success;
2c731798 80on error, \-1 is returned, and
c13182ef 81.I errno
2c731798
MK
82is set to indicate the error.
83.SH ERRORS
84.TP
85.B EINVAL
86.I value
87exceeds
88.BR SEM_VALUE_MAX .
89.TP
90.B ENOSYS
91.I pshared
c7094399 92is nonzero,
2c731798
MK
93but the system does not support process-shared semaphores (see
94.BR sem_overview (7)).
24bd2288 95.SH ATTRIBUTES
4835be30
PH
96For an explanation of the terms used in this section, see
97.BR attributes (7).
98.TS
99allbox;
100lb lb lb
101l l l.
102Interface Attribute Value
103T{
24bd2288 104.BR sem_init ()
4835be30
PH
105T} Thread safety MT-Safe
106.TE
2c731798
MK
107.SH CONFORMING TO
108POSIX.1-2001.
109.SH NOTES
110Bizarrely, POSIX.1-2001 does not specify the value that should
111be returned by a successful call to
112.BR sem_init ().
345edc6a 113POSIX.1-2008 rectifies this, specifying the zero return on success.
47297adb 114.SH SEE ALSO
2c731798
MK
115.BR sem_destroy (3),
116.BR sem_post (3),
117.BR sem_wait (3),
118.BR sem_overview (7)