OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / man3 / sem_init.3
1 '\" t
2 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
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.
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH SEM_INIT 3 2014-03-10 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 sem_init \- initialize an unnamed semaphore
29 .SH SYNOPSIS
30 .nf
31 .B #include <semaphore.h>
32 .sp
33 .BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value );
34 .fi
35 .sp
36 Link with \fI\-pthread\fP.
37 .SH DESCRIPTION
38 .BR sem_init ()
39 initializes the unnamed semaphore at the address pointed to by
40 .IR sem .
41 The
42 .I value
43 argument specifies the initial value for the semaphore.
44
45 The
46 .I pshared
47 argument indicates whether this semaphore is to be shared
48 between the threads of a process, or between processes.
49
50 If
51 .I pshared
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
56 the heap).
57
58 If
59 .I pshared
60 is nonzero, then the semaphore is shared between processes,
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.)
69 Any process that can access the shared memory region
70 can operate on the semaphore using
71 .BR sem_post (3),
72 .BR sem_wait (3),
73 and so on.
74
75 Initializing a semaphore that has already been initialized
76 results in undefined behavior.
77 .SH RETURN VALUE
78 .BR sem_init ()
79 returns 0 on success;
80 on error, \-1 is returned, and
81 .I errno
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
92 is nonzero,
93 but the system does not support process-shared semaphores (see
94 .BR sem_overview (7)).
95 .SH ATTRIBUTES
96 .SS Multithreading (see pthreads(7))
97 The
98 .BR sem_init ()
99 function is thread-safe.
100 .SH CONFORMING TO
101 POSIX.1-2001.
102 .SH NOTES
103 Bizarrely, POSIX.1-2001 does not specify the value that should
104 be returned by a successful call to
105 .BR sem_init ().
106 POSIX.1-2008 rectifies this, specifying the zero return on success.
107 .SH SEE ALSO
108 .BR sem_destroy (3),
109 .BR sem_post (3),
110 .BR sem_wait (3),
111 .BR sem_overview (7)
112 .SH COLOPHON
113 This page is part of release 3.68 of the Linux
114 .I man-pages
115 project.
116 A description of the project,
117 information about reporting bugs,
118 and the latest version of this page,
119 can be found at
120 \%http://www.kernel.org/doc/man\-pages/.