2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI all rights reserved.
27 .\" Translated 2006-04-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
29 .TH SEM_INIT 3 2008-07-27 "Linux" "Linux Programmer's Manual"
32 .\"O sem_init \- initialize an unnamed semaphore
33 sem_init \- ̾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤ò½é´ü²½¤¹¤ë
37 .B #include <semaphore.h>
39 .BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value );
42 .\"O Link with \fI\-lrt\fP or \fI\-pthread\fP.
43 \fI\-lrt\fP ¤Þ¤¿¤Ï \fI\-pthread\fP ¤Ç¥ê¥ó¥¯¤¹¤ë¡£
47 .\"O initializes the unnamed semaphore at the address pointed to by
51 .\"O argument specifies the initial value for the semaphore.
55 ¤¬»Ø¤¹¥¢¥É¥ì¥¹¤Ë¤¢¤ë̾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤ò½é´ü²½¤¹¤ë¡£
57 °ú¤¿ô¤Ï¤½¤Î¥»¥Þ¥Õ¥©¤Î½é´üÃͤò»ØÄꤹ¤ë¡£
61 .\"O argument indicates whether this semaphore is to be shared
62 .\"O between the threads of a process, or between processes.
64 °ú¤¿ô¤Ï¡¢¤³¤Î¥»¥Þ¥Õ¥©¤¬¥×¥í¥»¥¹Æâ¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¶¦Í¤µ¤ì¤ë¤Î¤«¡¢
65 ¥×¥í¥»¥¹´Ö¤Ç¶¦Í¤µ¤ì¤ë¤Î¤«¡¢¤ò¼¨¤¹¡£
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
75 ¤¬ 0 ¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©¤Ï¥×¥í¥»¥¹Æâ¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¶¦Í¤µ¤ì¤ë¡£
76 ¥»¥Þ¥Õ¥©¤Ï¤¹¤Ù¤Æ¤Î¥¹¥ì¥Ã¥É¤«¤é»²¾È²Äǽ¤Ê¥¢¥É¥ì¥¹¤ËÇÛÃÖ¤¹¤Ù¤¤Ç¤¢¤ë
77 (Î㤨¤Ð¡¢Âç°èÊÑ¿ô¤ä¡¢¥Ò¡¼¥×¾å¤ËưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿ÊÑ¿ô¤Ê¤É)¡£
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),
87 .\"O (Since a child created by
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),
96 ¤¬ 0 °Ê³°¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©¤Ï¥×¥í¥»¥¹´Ö¤Ç¶¦Í¤µ¤ì¤ë¡£¥»¥Þ¥Õ¥©¤Ï
97 ¶¦Í¥á¥â¥êÎΰè¤ËÇÛÃÖ¤¹¤Ù¤¤Ç¤¢¤ë
103 ¤ÇÀ¸À®¤µ¤ì¤¿»Ò¥×¥í¥»¥¹¤Ï¿Æ¥×¥í¥»¥¹¤Î¥á¥â¥ê¥Þ¥Ã¥Ô¥ó¥°¤ò·Ñ¾µ¤¹¤ë¤¿¤á¡¢
104 »Ò¥×¥í¥»¥¹¤â¥»¥Þ¥Õ¥©¤Ë¥¢¥¯¥»¥¹¤Ç¤¤ë¡£)
105 ¶¦Í¥á¥â¥êÎΰè¤Ë¥¢¥¯¥»¥¹¤Ç¤¤ë¥×¥í¥»¥¹¤Ê¤é¤Ð¡¢¤É¤Î¥×¥í¥»¥¹¤Ç¤â
109 ¤Ê¤É¤ò»È¤Ã¤Æ¥»¥Þ¥Õ¥©¤òÁàºî¤Ç¤¤ë¡£
111 .\"O Initializing a semaphore that has already been initialized
112 .\"O results in undefined behavior.
113 ¤¹¤Ç¤Ë½é´ü²½ºÑ¤Î¥»¥Þ¥Õ¥©¤ò½é´ü²½¤·¤¿¾ì¹ç¤ÎµóÆ°¤ÏÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
114 .\"O .SH RETURN VALUE
117 .\"O returns 0 on success;
118 .\"O on error, \-1 is returned, and
120 .\"O is set to indicate the error.
123 ¤Ï 0 ¤òÊÖ¤¹¡£¥¨¥é¡¼¤Î¾ì¹ç¡¢\-1 ¤òÊÖ¤·¡¢
125 ¤Ë¥¨¥é¡¼¤ò¼¨¤¹Ãͤò¥»¥Ã¥È¤¹¤ë¡£
132 .\"O .BR SEM_VALUE_MAX .
141 .\"O but the system does not support process-shared semaphores (see
142 .\"O .BR sem_overview (7)).
144 ¤¬ 0 °Ê³°¤À¤¬¡¢¥·¥¹¥Æ¥à¤¬¥×¥í¥»¥¹¶¦Í¥»¥Þ¥Õ¥©¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Ê¤¤
145 .RB ( sem_overview (7)
147 .\"O .SH CONFORMING TO
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 ¤Ï
158 ¤¬À®¸ù¤·¤¿¾ì¹ç¤ËÊÖ¤¹¤Ù¤Ãͤòµ¬Äꤷ¤Æ¤¤¤Ê¤¤¡£
159 POSIX.1-2008 ¤Ç¤Ï¤³¤ì¤¬½¤Àµ¤µ¤ì¡¢À®¸ù»þ¤Ë¤Ï 0 ¤òÊÖ¤¹¤³¤È¤¬µ¬Äꤵ¤ì¤¿¡£