OSDN Git Service

(split) LDP_man-pages: release ioctl.2 [JM:00378]
[linuxjm/LDP_man-pages.git] / draft / man3 / sem_init.3
1 '\" t
2 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\"
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI all rights reserved.
27 .\" Translated 2006-04-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
28 .\"
29 .TH SEM_INIT 3 2008-07-27 "Linux" "Linux Programmer's Manual"
30 .\"O .SH NAME
31 .SH Ì¾Á°
32 .\"O sem_init \- initialize an unnamed semaphore
33 sem_init \- Ì¾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤ò½é´ü²½¤¹¤ë
34 .\"O .SH SYNOPSIS
35 .SH ½ñ¼°
36 .nf
37 .B #include <semaphore.h>
38 .sp
39 .BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value );
40 .fi
41 .sp
42 .\"O Link with \fI\-lrt\fP or \fI\-pthread\fP.
43 \fI\-lrt\fP ¤Þ¤¿¤Ï \fI\-pthread\fP ¤Ç¥ê¥ó¥¯¤¹¤ë¡£
44 .\"O .SH DESCRIPTION
45 .SH ÀâÌÀ
46 .\"O .BR sem_init ()
47 .\"O initializes the unnamed semaphore at the address pointed to by
48 .\"O .IR sem .
49 .\"O The
50 .\"O .I value
51 .\"O argument specifies the initial value for the semaphore.
52 .BR sem_init ()
53 ¤Ï¡¢
54 .I sem
55 ¤¬»Ø¤¹¥¢¥É¥ì¥¹¤Ë¤¢¤ë̾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤ò½é´ü²½¤¹¤ë¡£
56 .I value
57 °ú¤­¿ô¤Ï¤½¤Î¥»¥Þ¥Õ¥©¤Î½é´üÃͤò»ØÄꤹ¤ë¡£
58
59 .\"O The
60 .\"O .I pshared
61 .\"O argument indicates whether this semaphore is to be shared
62 .\"O between the threads of a process, or between processes.
63 .I pshared
64 °ú¤­¿ô¤Ï¡¢¤³¤Î¥»¥Þ¥Õ¥©¤¬¥×¥í¥»¥¹Æâ¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¶¦Í­¤µ¤ì¤ë¤Î¤«¡¢
65 ¥×¥í¥»¥¹´Ö¤Ç¶¦Í­¤µ¤ì¤ë¤Î¤«¡¢¤ò¼¨¤¹¡£
66
67 .\"O If
68 .\"O .I pshared
69 .\"O has the value 0,
70 .\"O then the semaphore is shared between the threads of a process,
71 .\"O and should be located at some address that is visible to all threads
72 .\"O (e.g., a global variable, or a variable allocated dynamically on
73 .\"O the heap).
74 .I pshared
75 ¤¬ 0 ¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©¤Ï¥×¥í¥»¥¹Æâ¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¶¦Í­¤µ¤ì¤ë¡£
76 ¥»¥Þ¥Õ¥©¤Ï¤¹¤Ù¤Æ¤Î¥¹¥ì¥Ã¥É¤«¤é»²¾È²Äǽ¤Ê¥¢¥É¥ì¥¹¤ËÇÛÃÖ¤¹¤Ù¤­¤Ç¤¢¤ë
77 (Î㤨¤Ð¡¢Âç°èÊÑ¿ô¤ä¡¢¥Ò¡¼¥×¾å¤ËưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿ÊÑ¿ô¤Ê¤É)¡£
78
79 .\"O If
80 .\"O .I pshared
81 .\"O is nonzero, then the semaphore is shared between processes,
82 .\"O and should be located in a region of shared memory (see
83 .\"O .BR shm_open (3),
84 .\"O .BR mmap (2),
85 .\"O and
86 .\"O .BR shmget (2)).
87 .\"O (Since a child created by
88 .\"O .BR fork (2)
89 .\"O inherits its parent's memory mappings, it can also access the semaphore.)
90 .\"O Any process that can access the shared memory region
91 .\"O can operate on the semaphore using
92 .\"O .BR sem_post (3),
93 .\"O .BR sem_wait (3),
94 .\"O etc.
95 .I pshared
96 ¤¬ 0 °Ê³°¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©¤Ï¥×¥í¥»¥¹´Ö¤Ç¶¦Í­¤µ¤ì¤ë¡£¥»¥Þ¥Õ¥©¤Ï
97 ¶¦Í­¥á¥â¥êÎΰè¤ËÇÛÃÖ¤¹¤Ù¤­¤Ç¤¢¤ë
98 .RB ( shm_open (3),
99 .BR mmap (2),
100 .BR shmget (2)
101 »²¾È)¡£
102 .RB ( fork (2)
103 ¤ÇÀ¸À®¤µ¤ì¤¿»Ò¥×¥í¥»¥¹¤Ï¿Æ¥×¥í¥»¥¹¤Î¥á¥â¥ê¥Þ¥Ã¥Ô¥ó¥°¤ò·Ñ¾µ¤¹¤ë¤¿¤á¡¢
104 »Ò¥×¥í¥»¥¹¤â¥»¥Þ¥Õ¥©¤Ë¥¢¥¯¥»¥¹¤Ç¤­¤ë¡£)
105 ¶¦Í­¥á¥â¥êÎΰè¤Ë¥¢¥¯¥»¥¹¤Ç¤­¤ë¥×¥í¥»¥¹¤Ê¤é¤Ð¡¢¤É¤Î¥×¥í¥»¥¹¤Ç¤â
106 .BR sem_post (3)
107 ¤ä
108 .BR sem_wait (3)
109 ¤Ê¤É¤ò»È¤Ã¤Æ¥»¥Þ¥Õ¥©¤òÁàºî¤Ç¤­¤ë¡£
110
111 .\"O Initializing a semaphore that has already been initialized
112 .\"O results in undefined behavior.
113 ¤¹¤Ç¤Ë½é´ü²½ºÑ¤Î¥»¥Þ¥Õ¥©¤ò½é´ü²½¤·¤¿¾ì¹ç¤ÎµóÆ°¤ÏÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
114 .\"O .SH RETURN VALUE
115 .SH ÊÖ¤êÃÍ
116 .\"O .BR sem_init ()
117 .\"O returns 0 on success;
118 .\"O on error, \-1 is returned, and
119 .\"O .I errno
120 .\"O is set to indicate the error.
121 À®¸ù¤¹¤ë¤È¡¢
122 .BR sem_init ()
123 ¤Ï 0 ¤òÊÖ¤¹¡£¥¨¥é¡¼¤Î¾ì¹ç¡¢\-1 ¤òÊÖ¤·¡¢
124 .I errno
125 ¤Ë¥¨¥é¡¼¤ò¼¨¤¹Ãͤò¥»¥Ã¥È¤¹¤ë¡£
126 .\"O .SH ERRORS
127 .SH ¥¨¥é¡¼
128 .TP
129 .B EINVAL
130 .\"O .I value
131 .\"O exceeds
132 .\"O .BR SEM_VALUE_MAX .
133 .I value
134 ¤¬
135 .B SEM_VALUE_MAX
136 ¤òĶ¤¨¤Æ¤¤¤ë¡£
137 .TP
138 .B ENOSYS
139 .\"O .I pshared
140 .\"O is nonzero,
141 .\"O but the system does not support process-shared semaphores (see
142 .\"O .BR sem_overview (7)).
143 .I pshared
144 ¤¬ 0 °Ê³°¤À¤¬¡¢¥·¥¹¥Æ¥à¤¬¥×¥í¥»¥¹¶¦Í­¥»¥Þ¥Õ¥©¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Ê¤¤
145 .RB ( sem_overview (7)
146 »²¾È)¡£
147 .\"O .SH CONFORMING TO
148 .SH ½àµò
149 POSIX.1-2001.
150 .\"O .SH NOTES
151 .SH Ãí°Õ
152 .\"O Bizarrely, POSIX.1-2001 does not specify the value that should
153 .\"O be returned by a successful call to
154 .\"O .BR sem_init ().
155 .\"O POSIX.1-2008 rectifies this, specifying the zero return on success.
156 ̯¤Ê¤³¤È¤Ë¡¢POSIX.1-2001 ¤Ï
157 .BR sem_init ()
158 ¤¬À®¸ù¤·¤¿¾ì¹ç¤ËÊÖ¤¹¤Ù¤­Ãͤòµ¬Äꤷ¤Æ¤¤¤Ê¤¤¡£
159 POSIX.1-2008 ¤Ç¤Ï¤³¤ì¤¬½¤Àµ¤µ¤ì¡¢À®¸ù»þ¤Ë¤Ï 0 ¤òÊÖ¤¹¤³¤È¤¬µ¬Äꤵ¤ì¤¿¡£
160 .\"O .SH "SEE ALSO"
161 .SH ´ØÏ¢¹àÌÜ
162 .BR sem_destroy (3),
163 .BR sem_post (3),
164 .BR sem_wait (3),
165 .BR sem_overview (7)