]>
Commit | Line | Data |
---|---|---|
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 | .\" |
4b8c67d9 | 26 | .TH SEM_INIT 3 2017-09-15 "Linux" "Linux Programmer's Manual" |
2c731798 | 27 | .SH NAME |
d9bfdb9c | 28 | sem_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 | 36 | Link with \fI\-pthread\fP. |
2c731798 MK |
37 | .SH DESCRIPTION |
38 | .BR sem_init () | |
d9bfdb9c | 39 | initializes the unnamed semaphore at the address pointed to by |
2c731798 MK |
40 | .IR sem . |
41 | The | |
42 | .I value | |
43 | argument specifies the initial value for the semaphore. | |
847e0d88 | 44 | .PP |
c13182ef | 45 | The |
2c731798 MK |
46 | .I pshared |
47 | argument indicates whether this semaphore is to be shared | |
48 | between the threads of a process, or between processes. | |
847e0d88 | 49 | .PP |
2c731798 MK |
50 | If |
51 | .I pshared | |
c13182ef MK |
52 | has the value 0, |
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 | |
2c731798 | 56 | the heap). |
847e0d88 | 57 | .PP |
2c731798 MK |
58 | If |
59 | .I pshared | |
c7094399 | 60 | is nonzero, then the semaphore is shared between processes, |
2c731798 MK |
61 | and should be located in a region of shared memory (see |
62 | .BR shm_open (3), | |
63 | .BR mmap (2), | |
64 | and | |
65 | .BR shmget (2)). | |
66 | (Since a child created by | |
67 | .BR fork (2) | |
68 | inherits its parent's memory mappings, it can also access the semaphore.) | |
c13182ef | 69 | Any process that can access the shared memory region |
2c731798 MK |
70 | can operate on the semaphore using |
71 | .BR sem_post (3), | |
72 | .BR sem_wait (3), | |
f78ed33a | 73 | and so on. |
847e0d88 | 74 | .PP |
d9bfdb9c MK |
75 | Initializing a semaphore that has already been initialized |
76 | results in undefined behavior. | |
2c731798 MK |
77 | .SH RETURN VALUE |
78 | .BR sem_init () | |
c13182ef | 79 | returns 0 on success; |
2c731798 | 80 | on error, \-1 is returned, and |
c13182ef | 81 | .I errno |
2c731798 MK |
82 | is set to indicate the error. |
83 | .SH ERRORS | |
84 | .TP | |
85 | .B EINVAL | |
86 | .I value | |
87 | exceeds | |
88 | .BR SEM_VALUE_MAX . | |
89 | .TP | |
90 | .B ENOSYS | |
91 | .I pshared | |
c7094399 | 92 | is nonzero, |
2c731798 MK |
93 | but the system does not support process-shared semaphores (see |
94 | .BR sem_overview (7)). | |
24bd2288 | 95 | .SH ATTRIBUTES |
4835be30 PH |
96 | For an explanation of the terms used in this section, see |
97 | .BR attributes (7). | |
98 | .TS | |
99 | allbox; | |
100 | lb lb lb | |
101 | l l l. | |
102 | Interface Attribute Value | |
103 | T{ | |
24bd2288 | 104 | .BR sem_init () |
4835be30 PH |
105 | T} Thread safety MT-Safe |
106 | .TE | |
2c731798 MK |
107 | .SH CONFORMING TO |
108 | POSIX.1-2001. | |
109 | .SH NOTES | |
110 | Bizarrely, POSIX.1-2001 does not specify the value that should | |
111 | be returned by a successful call to | |
112 | .BR sem_init (). | |
345edc6a | 113 | POSIX.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) |