poll.2: Remove <signal.h>
[man-pages.git] / man3 / sem_init.3
blob83a2b0f8512d3932859eeb5440dc34dab996415e
1 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH SEM_INIT 3 2021-03-22 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 sem_init \- initialize an unnamed semaphore
28 .SH SYNOPSIS
29 .nf
30 .B #include <semaphore.h>
31 .PP
32 .BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value );
33 .fi
34 .PP
35 Link with \fI\-pthread\fP.
36 .SH DESCRIPTION
37 .BR sem_init ()
38 initializes the unnamed semaphore at the address pointed to by
39 .IR sem .
40 The
41 .I value
42 argument specifies the initial value for the semaphore.
43 .PP
44 The
45 .I pshared
46 argument indicates whether this semaphore is to be shared
47 between the threads of a process, or between processes.
48 .PP
50 .I pshared
51 has the value 0,
52 then the semaphore is shared between the threads of a process,
53 and should be located at some address that is visible to all threads
54 (e.g., a global variable, or a variable allocated dynamically on
55 the heap).
56 .PP
58 .I pshared
59 is nonzero, then the semaphore is shared between processes,
60 and should be located in a region of shared memory (see
61 .BR shm_open (3),
62 .BR mmap (2),
63 and
64 .BR shmget (2)).
65 (Since a child created by
66 .BR fork (2)
67 inherits its parent's memory mappings, it can also access the semaphore.)
68 Any process that can access the shared memory region
69 can operate on the semaphore using
70 .BR sem_post (3),
71 .BR sem_wait (3),
72 and so on.
73 .PP
74 Initializing a semaphore that has already been initialized
75 results in undefined behavior.
76 .SH RETURN VALUE
77 .BR sem_init ()
78 returns 0 on success;
79 on error, \-1 is returned, and
80 .I errno
81 is set to indicate the error.
82 .SH ERRORS
83 .TP
84 .B EINVAL
85 .I value
86 exceeds
87 .BR SEM_VALUE_MAX .
88 .TP
89 .B ENOSYS
90 .I pshared
91 is nonzero,
92 but the system does not support process-shared semaphores (see
93 .BR sem_overview (7)).
94 .SH ATTRIBUTES
95 For an explanation of the terms used in this section, see
96 .BR attributes (7).
97 .ad l
98 .nh
99 .TS
100 allbox;
101 lbx lb lb
102 l l l.
103 Interface       Attribute       Value
105 .BR sem_init ()
106 T}      Thread safety   MT-Safe
110 .sp 1
111 .SH CONFORMING TO
112 POSIX.1-2001.
113 .SH NOTES
114 Bizarrely, POSIX.1-2001 does not specify the value that should
115 be returned by a successful call to
116 .BR sem_init ().
117 POSIX.1-2008 rectifies this, specifying the zero return on success.
118 .SH EXAMPLES
120 .BR shm_open (3)
122 .BR sem_wait (3).
123 .SH SEE ALSO
124 .BR sem_destroy (3),
125 .BR sem_post (3),
126 .BR sem_wait (3),
127 .BR sem_overview (7)