OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / draft / man3 / mq_notify.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-07-31, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
28 .\" Updated 2010-04-18, Akihiro MOTOKI, LDP v3.24
29 .\"
30 .TH MQ_NOTIFY 3 2009-09-15 "Linux" "Linux Programmer's Manual"
31 .\"O .SH NAME
32 .SH Ì¾Á°
33 .\"O mq_notify \- register for notification when a message is available
34 mq_notify \- ¥á¥Ã¥»¡¼¥¸ÅþÃå»þ¤ËÄÌÃΤò¹Ô¤¦¤è¤¦ÅÐÏ¿¤¹¤ë
35 .\"O .SH SYNOPSIS
36 .SH ½ñ¼°
37 .nf
38 .B #include <mqueue.h>
39 .sp
40 .BI "mqd_t mq_notify(mqd_t " mqdes ", const struct sigevent *" notification );
41 .fi
42 .sp
43 .\"O Link with \fI\-lrt\fP.
44 \fI\-lrt\fP ¤Ç¥ê¥ó¥¯¤¹¤ë¡£
45 .\"O .SH DESCRIPTION
46 .SH ÀâÌÀ
47 .\"O .BR mq_notify ()
48 .\"O allows the calling process to register or unregister for delivery of
49 .\"O an asynchronous notification when a new message arrives on
50 .\"O the empty message queue referred to by the descriptor
51 .\"O .IR mqdes .
52 .BR mq_notify ()
53 ¤ò»È¤¦¤È¡¢¥Ç¥£¥¹¥¯¥ê¥×¥¿
54 .I mqdes
55 ¤Ç»²¾È¤µ¤ì¤ë¶õ¤Î¥á¥Ã¥»¡¼¥¸¥­¥å¡¼¤Ë¿·¤·¤¯¥á¥Ã¥»¡¼¥¸¤¬ÅþÃ夷¤¿»þ¤Ë
56 ÈóƱ´ü¤ÎÄÌÃΠ(notification) ¤ÎÇÛÁ÷¤¬¹Ô¤ï¤ì¤ë¤è¤¦¤ËÅÐÏ¿¤·¤¿¤ê¡¢
57 ¤½¤Î²ò½ü¤ò¹Ô¤Ã¤¿¤ê¤Ç¤­¤ë¡£
58
59 .\"O The
60 .\"O .I notification
61 .\"O argument is a pointer to a
62 .\"O .I sigevent
63 .\"O structure that is defined something like the following:
64 .I notification
65 °ú¤­¿ô¤Ï
66 .I sigevent
67 ¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
68 .I sigevent
69 ¹½Â¤ÂΤϰʲ¼¤Î¤è¤¦¤Ê´¶¤¸¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë:
70 .in +4n
71 .nf
72
73 union sigval {          /* Data passed with notification */
74     int     sival_int;         /* Integer value */
75     void   *sival_ptr;         /* Pointer value */
76 };
77
78 struct sigevent {
79     int          sigev_notify; /* Notification method */
80     int          sigev_signo;  /* Notification signal */
81     union sigval sigev_value;  /* Data passed with
82                                   notification */
83     void       (*sigev_notify_function) (union sigval);
84                                /* Function for thread
85                                   notification */
86     void        *sigev_notify_attributes;
87                                /* Thread function attributes */
88 };
89 .fi
90 .in
91 .PP
92 .\"O If
93 .\"O .I notification
94 .\"O is a non-NULL pointer, then
95 .\"O .BR mq_notify ()
96 .\"O registers the calling process to receive message notification.
97 .I notification
98 ¤¬ NULL ¤Ç¤Ê¤¤¥Ý¥¤¥ó¥¿¤Ç¤¢¤ì¤Ð¡¢
99 .BR mq_notify ()
100 ¤Ï¥á¥Ã¥»¡¼¥¸ÄÌÃΤò¼õ¤±¼è¤ë¤è¤¦¤Ë¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤òÅÐÏ¿¤¹¤ë¡£
101 .\"O The
102 .\"O .I sigev_notify
103 .\"O field of the
104 .\"O .I sigevent
105 .\"O to which
106 .\"O .I notification
107 .\"O points specifies how notification is to be performed.
108 .\"O This field has one of the following values:
109 .I notification
110 ¤¬»Ø¤¹
111 .I sigevent
112 ¤Î
113 .I sigev_notify
114 ¥Õ¥£¡¼¥ë¥É¤Ï¡¢¤É¤Î¤è¤¦¤ÊÄÌÃΤò¹Ô¤¦¤Î¤«¤ò»ØÄꤹ¤ë¡£
115 ¤³¤Î¥Õ¥£¡¼¥ë¥É¤Ï°Ê²¼¤ÎÃͤΤ¤¤º¤ì¤«¤ò»ý¤Ä¡£
116 .TP
117 .B SIGEV_NONE
118 .\"O A "null" notification: the calling process is registered as the target
119 .\"O for notification, but when a message arrives, no notification is sent.
120 ¡Ö¶õ¤Î (null)¡×¤ÎÄÌÃÎ: ¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤òÄÌÃΤΰ¸Àè¤È¤·¤ÆÅÐÏ¿¤¹¤ë¤¬¡¢
121 ¼ÂºÝ¤Ë¤Ï¥á¥Ã¥»¡¼¥¸¤¬ÅþÃ夷¤¿»þ¤ËÄÌÃΤÏÁ÷¤é¤ì¤Ê¤¤¡£
122 .\" When is SIGEV_NONE useful?
123 .TP
124 .B SIGEV_SIGNAL
125 .\"O Notify the process by sending the signal specified in
126 .\"O .IR sigev_signo .
127 .\"O If the signal is caught with a signal handler that
128 .\"O was registered using the
129 .\"O .BR sigaction (2)
130 .\"O .B SA_SIGINFO
131 .\"O flag, then the following fields are set in the
132 .\"O .I siginfo_t
133 .\"O structure that is passed as the second argument of the handler:
134 .I sigev_signo
135 ¤Ç»ØÄꤵ¤ì¤¿¥·¥°¥Ê¥ë¤òÁ÷¤Ã¤Æ¡¢¥×¥í¥»¥¹¤ËÄÌÃΤ¹¤ë¡£
136 .BR sigaction (2)
137 ¤Î
138 .B SA_SIGINFO
139 ¥Õ¥é¥°¤Ç¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤òÅÐÏ¿¤·¤Æ¤¤¤ë¾ì¹ç¡¢
140 ¤½¤Î¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ç¥·¥°¥Ê¥ë¤¬Ê᪤µ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢
141 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤ÎÂèÆó°ú¤­¿ô¤È¤·¤ÆÅϤµ¤ì¤ë
142 .I siginfo_t
143 ¹½Â¤ÂΤγƥե£¡¼¥ë¥É¤Ï°Ê²¼¤Î¤è¤¦¤ËÀßÄꤵ¤ì¤ë¡£
144 .\"O .I si_code
145 .\"O is set to
146 .\"O .BR SI_MESGQ ;
147 .\"O .I si_signo
148 .\"O is set to the signal number;
149 .\"O .I si_value
150 .\"O is set to the value specified in
151 .\"O .IR notification\->sigev_value ;
152 .I si_code
153 ¤Ë¤Ï
154 .B SI_MESGQ
155 ¤¬¡¢
156 .I si_signo
157 ¤Ë¤Ï¥·¥°¥Ê¥ëÈֹ椬¡¢
158 .I si_value
159 ¤Ë¤Ï
160 .I notification\->sigev_value
161 ¤Ç»ØÄꤷ¤¿Ãͤ¬ÀßÄꤵ¤ì¤ë¡£
162 .\" I don't know of other implementations that set
163 .\" si_pid and si_uid -- MTK
164 .\"O .I si_pid
165 .\"O is set to the PID of the process that sent the message; and
166 .\"O .I si_uid
167 .\"O is set to the real user ID of the sending process.
168 .\"O The same information is available if the signal is accepted using
169 .\"O .BR sigwaitinfo (2).
170 .I si_pid
171 ¤Ë¤Ï¥á¥Ã¥»¡¼¥¸¤òÁ÷¿®¤·¤¿¥×¥í¥»¥¹¤Î PID ¤¬¡¢
172 .I si_uid
173 ¤Ë¤ÏÁ÷¿®¥×¥í¥»¥¹¤Î¼Â¥æ¡¼¥¶ ID ¤¬ÀßÄꤵ¤ì¤ë¡£
174 .BR sigwaitinfo (2)
175 ¤ò»È¤Ã¤Æ¥·¥°¥Ê¥ë¤ò¼õ¿®¤¹¤ë¾ì¹ç¤â¡¢Æ±¤¸¾ðÊó¤¬ÆÀ¤é¤ì¤ë¡£
176 .TP
177 .B SIGEV_THREAD
178 .\"O Deliver notification by invoking
179 .\"O .I notification\->sigev_notify_function
180 .\"O as the start function of a new thread.
181 .\"O The function is invoked with
182 .\"O .I notification\->sigev_value
183 .\"O as its sole argument.
184 .\"O If
185 .\"O .I notification\->sigev_notify_attributes
186 .\"O is not NULL, then it should point to a
187 .\"O .I pthread_attr_t
188 .\"O structure that defines attributes for the thread (see
189 .\"O .BR pthread_attr_init (3)).
190 ¿·¤·¤¤¥¹¥ì¥Ã¥É¤Î³«»Ï´Ø¿ô¤È¤·¤Æ
191 .I notification\->sigev_thread_function
192 ¤òµ¯Æ°¤¹¤ë¤³¤È¤ÇÄÌÃΤò¹Ô¤¦¡£
193 µ¯Æ°»þ¤Î´Ø¿ô¤Î°ú¤­¿ô¤È¤·¤Æ¤Ï
194 .I notification\->sigev_value
195 ¤À¤±¤¬ÅϤµ¤ì¤ë¡£
196 .I notification\->sigev_notify_attributes
197 ¤Ï¡¢NULL °Ê³°¤Î¾ì¹ç¡¢
198 ¤½¤Î¥¹¥ì¥Ã¥É¤Î°À­¤òÄêµÁ¤¹¤ë
199 .I pthread_attr_t
200 ¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤È¤Ê¤Ã¤Æ¤¤¤ëɬÍפ¬¤¢¤ë
201 .RB ( pthread_attr_init (3)
202 »²¾È)¡£
203 .PP
204 .\"O Only one process can be registered to receive notification
205 .\"O from a message queue.
206 °ì¤Ä¤Î¥á¥Ã¥»¡¼¥¸¥­¥å¡¼¤«¤éÄÌÃΤò¼õ¿®¤¹¤ë¤è¤¦¤ËÅÐÏ¿¤Ç¤­¤ë¥×¥í¥»¥¹¤Ï
207 °ì¤Ä¤À¤±¤Ç¤¢¤ë¡£
208
209 .\"O If
210 .\"O .I notification
211 .\"O is NULL, and the calling process is currently registered to receive
212 .\"O notifications for this message queue, then the registration is removed;
213 .\"O another process can then register to receive a message notification
214 .\"O for this queue.
215 .I notification
216 ¤¬ NULL ¤Ç¡¢¤«¤Ä¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤¬¤³¤Î¥á¥Ã¥»¡¼¥¸¥­¥å¡¼¤«¤é¤Î
217 ÄÌÃΤò¼õ¿®¤¹¤ë¤Ë¸½ºßÅÐÏ¿¤·¤Æ¤¤¤ë¾ì¹ç¡¢ÅÐÏ¿¤òºï½ü¤¹¤ë¡£
218 ¤³¤ì°Ê¹ß¡¢ÊÌ¤Î¥×¥í¥»¥¹¤¬¤³¤Î¥á¥Ã¥»¡¼¥¸¥­¥å¡¼¤«¤éÄÌÃΤò¼õ¿®¤¹¤ë¤è¤¦¤Ë
219 ÅÐÏ¿¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤ë¡£
220
221 .\"O Message notification only occurs when a new message arrives and
222 .\"O the queue was previously empty.
223 .\"O If the queue was not empty at the time
224 .\"O .BR mq_notify ()
225 .\"O was called, then a notification will only occur after
226 .\"O the queue is emptied and a new message arrives.
227 ¥á¥Ã¥»¡¼¥¸ÄÌÃΤϡ¢¤½¤ì¤Þ¤Ç¶õ¤Î¥­¥å¡¼¤Ë¿·¤·¤¤¥á¥Ã¥»¡¼¥¸¤¬ÅþÃ夷¤¿
228 ¾ì¹ç¤Ë¤Î¤ß¹Ô¤ï¤ì¤ë¡£
229 .BR mq_notify ()
230 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿»þ¤Ë¤½¤Î¥­¥å¡¼¤¬¶õ¤Ç¤Ê¤¤¾ì¹ç¡¢
231 ¤½¤Î¥­¥å¡¼¤¬¶õ¤Ë¤Ê¤ê¡¢¤½¤Î¸å¿·¤·¤¤¥á¥Ã¥»¡¼¥¸¤¬ÅþÃ夷¤¿»þ¤Ë
232 ½é¤á¤ÆÄÌÃΤ¬¹Ô¤ï¤ì¤ë¤³¤È¤Ë¤Ê¤ë¡£
233
234 .\"O If another process or thread is waiting to read a message
235 .\"O from an empty queue using
236 .\"O .BR mq_receive (3),
237 .\"O then any message notification registration is ignored:
238 .\"O the message is delivered to the process or thread calling
239 .\"O .BR mq_receive (3),
240 .\"O and the message notification registration remains in effect.
241 ÊÌ¤Î¥×¥í¥»¥¹¤ä¥¹¥ì¥Ã¥É¤¬
242 .BR mq_receive (3)
243 ¤ò»È¤Ã¤Æ¡¢¶õ¤Î¥­¥å¡¼¤«¤é¥á¥Ã¥»¡¼¥¸¤ÎÆɤ߽Ф·¤òÂԤäƤ¤¤ë¾ì¹ç¡¢
244 ¥á¥Ã¥»¡¼¥¸ÄÌÃΤÎÅÐÏ¿¤ÏÁ´¤Æ̵»ë¤µ¤ì¤ë¡£
245 ¥á¥Ã¥»¡¼¥¸¤Ï
246 .BR mq_receive (3)
247 ¤ò¸Æ¤Ó½Ð¤·¤Æ¤¤¤ë¥×¥í¥»¥¹¤ä¥¹¥ì¥Ã¥É¤ËÇÛÁ÷¤µ¤ì¡¢
248 ¥á¥Ã¥»¡¼¥¸ÄÌÃΤÎÅÐÏ¿¤Ï¸úÎϤò»ý¤Ã¤¿¤Þ¤Þ¤È¤Ê¤ë¡£
249
250 .\"O Notification occurs once: after a notification is delivered,
251 .\"O the notification registration is removed,
252 .\"O and another process can register for message notification.
253 .\"O If the notified process wishes to receive the next notification,
254 .\"O it can use
255 .\"O .BR mq_notify ()
256 .\"O to request a further notification.
257 .\"O This should be done before emptying all unread messages from the queue.
258 .\"O (Placing the queue in nonblocking mode is useful for emptying
259 .\"O the queue of messages without blocking once it is empty.)
260 ÄÌÃΤϰìÅÙ¤À¤±¹Ô¤ï¤ì¤ë¡£ÄÌÃΤ¬Á÷¤é¤ì¤¿¸å¤Ï¡¢ÄÌÃÎÍ×µá¤ÎÅÐÏ¿¤Ïºï½ü¤µ¤ì¡¢
261 ÊÌ¤Î¥×¥í¥»¥¹¤¬¥á¥Ã¥»¡¼¥¸ÄÌÃΤò¼õ¿®¤¹¤ë¤è¤¦¤ËÅÐÏ¿¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤ë¡£
262 ÄÌÃΤò¼õ¤±¤¿¥×¥í¥»¥¹¤¬¼¡¤ÎÄÌÃΤâ¼õ¿®¤·¤¿¤¤¾ì¹ç¤Ï¡¢
263 .BR mq_notify ()
264 ¤ò»È¤Ã¤Æ¤½¤Î¸å¤ÎÄÌÃΤâ¼õ¤±¤ë¤è¤¦¤ËÍ׵᤹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
265 .BR mq_notify ()
266 ¤òºÆÅٸƤӽФ¹¤Î¤Ï¡¢Æɤ߽Ф·¤Æ¤¤¤Ê¤¤¥á¥Ã¥»¡¼¥¸¤òÁ´ÉôÆɤ߽Ф·¤Æ
267 ¥­¥å¡¼¤¬¶õ¤Ë¤Ê¤ëÁ°¤Ë¤¹¤Ù¤­¤Ç¤¢¤ë
268 (¥­¥å¡¼¤«¤é¤Î¥á¥Ã¥»¡¼¥¸Æɤ߽Ф·¤ò¥­¥å¡¼¤¬¶õ¤Ë¤Ê¤Ã¤¿»þ¤Ë
269 Ää»ß (block) ¤»¤º¤Ë¹Ô¤¦¤Ë¤Ï¡¢¥­¥å¡¼¤òÈóÄä»ß¥â¡¼¥É (non-blocking mode)
270 ¤ËÀßÄꤷ¤Æ¤ª¤¯¤È¤è¤¤)¡£
271 .\"O .SH RETURN VALUE
272 .SH ÊÖ¤êÃÍ
273 .\"O On success
274 .\"O .BR mq_notify ()
275 .\"O returns 0; on error, \-1 is returned, with
276 .\"O .I errno
277 .\"O set to indicate the error.
278 À®¸ù¤¹¤ë¤È¡¢
279 .BR mq_notify ()
280 ¤Ï 0 ¤òÊÖ¤¹¡£¥¨¥é¡¼¤Î¾ì¹ç¡¢\-1 ¤òÊÖ¤·¡¢
281 .I errno
282 ¤ò¥¨¥é¡¼¤ò¼¨¤¹ÃͤËÀßÄꤹ¤ë¡£
283 .\"O .SH ERRORS
284 .SH ¥¨¥é¡¼
285 .TP
286 .B EBADF
287 .\"O The descriptor specified in
288 .\"O .I mqdes
289 .\"O is invalid.
290 .I mqdes
291 ¤Ë»ØÄꤵ¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬ÉÔÀµ¤Ç¤¢¤ë¡£
292 .TP
293 .B EBUSY
294 .\"O Another process has already registered to receive notification
295 .\"O for this message queue.
296 ÊÌ¤Î¥×¥í¥»¥¹¤¬¤¹¤Ç¤Ë
297 ¤³¤Î¥á¥Ã¥»¡¼¥¸¥­¥å¡¼¤ËÂФ¹¤ëÄÌÃΤò¼õ¿®¤¹¤ë¤è¤¦¤ËÅÐÏ¿¤·¤Æ¤¤¤ë¡£
298 .TP
299 .B EINVAL
300 .\"O .I notification\->sigev_notify
301 .\"O is not one of the permitted values; or
302 .\"O .I notification\->sigev_notify
303 .\"O is
304 .\"O .B SIGEV_SIGNAL
305 .\"O and
306 .\"O .I notification\->sigev_signo
307 .\"O is not a valid signal number.
308 .I notification\->sigev_notify
309 ¤¬µö²Ä¤µ¤ì¤¿ÃͤΤ¤¤º¤ì¤Ç¤â¤Ê¤¤¡£¤â¤·¤¯¤Ï
310 .I notification\->sigev_notify
311 ¤¬
312 .B SIGEV_SIGNAL
313 ¤À¤¬
314 .I notification\->sigev_signo
315 ¤¬Í­¸ú¤Ê¥·¥°¥Ê¥ëÈÖ¹æ¤Ç¤Ï¤Ê¤¤¡£
316 .TP
317 .B ENOMEM
318 .\"O Insufficient memory.
319 ɬÍפʥá¥â¥ê¤¬¤Ê¤«¤Ã¤¿¡£
320 .PP
321 .\"O POSIX.1-2008 says that an implementation
322 .\"O .I may
323 .\"O generate an
324 .\"O .B EINVAL
325 .\"O .\" Linux does not do this
326 .\"O error if
327 .\"O .I notification
328 .\"O is NULL, and the caller is not currently registered to receive
329 .\"O notifications for the queue
330 .\"O .IR mqdes .
331 POSIX.1-2008 ¤Ç¤Ï¡¢
332 .I notifications
333 ¤¬ NULL ¤Ç¡¢¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤¬¥­¥å¡¼
334 .I mqdes
335 ¤Ë´Ø¤¹¤ëÄÌÃΤò¼õ¿®¤¹¤ë¤è¤¦¤ËÅÐÏ¿¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢¥¨¥é¡¼
336 .B EINVAL
337 ¤òÀ¸À®¤¹¤ë¤è¤¦¤Ê¼ÂÁõ¤ò¹Ô¤Ã¤Æ¤â¡Ö¤è¤¤¡×¤³¤È¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
338 .\" Linux ¤Î¼ÂÁõ¤Ç¤Ï EINVAL ¤ÏÀ¸À®¤µ¤ì¤Ê¤¤
339 .\"O .SH CONFORMING TO
340 .SH ½àµò
341 POSIX.1-2001.
342 .\"O .SH EXAMPLE
343 .SH Îã
344 .\"O The following program registers a notification request for the
345 .\"O message queue named in its command-line argument.
346 .\"O Notification is performed by creating a thread.
347 .\"O The thread executes a function which reads one message from the
348 .\"O queue and then terminates the process.
349 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï¡¢
350 ¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤Ç»ØÄꤵ¤ì¤¿Ì¾Á°¤Î¥á¥Ã¥»¡¼¥¸¥­¥å¡¼¤Ø¤Î
351 ÄÌÃÎÍ×µá¤òÅÐÏ¿¤·¡¢ÄÌÃΤϥ¹¥ì¥Ã¥É¤ÎºîÀ®¤Ë¤è¤Ã¤Æ¹Ô¤ï¤ì¤ë¡£
352 ¤½¤Î¥¹¥ì¥Ã¥É¤Ï¡¢¤½¤Î¥­¥å¡¼¤«¤é¥á¥Ã¥»¡¼¥¸¤ò°ì¤ÄÆɤ߽Ф·¤Æ¤«¤é¡¢
353 ¥×¥í¥»¥¹¤ò½ªÎ»¤¹¤ë´Ø¿ô¤ò¼Â¹Ô¤¹¤ë¡£
354 .nf
355
356 #include <pthread.h>
357 #include <mqueue.h>
358 #include <stdio.h>
359 #include <stdlib.h>
360 #include <unistd.h>
361
362 #define handle_error(msg) \\
363     do { perror(msg); exit(EXIT_FAILURE); } while (0)
364
365 .\"O static void                     /* Thread start function */
366 static void                     /* ¥¹¥ì¥Ã¥É³«»Ï´Ø¿ô */
367 tfunc(union sigval sv)
368 {
369     struct mq_attr attr;
370     ssize_t nr;
371     void *buf;
372     mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
373
374 .\"O     /* Determine max. msg size; allocate buffer to receive msg */
375     /* ºÇÂç¥á¥Ã¥»¡¼¥¸¥µ¥¤¥º¤ò·èÄꤷ¡¢
376        ¥á¥Ã¥»¡¼¥¸¼õ¿®ÍѤΥХåե¡¤ò³ÎÊݤ¹¤ë */
377
378     if (mq_getattr(mqdes, &attr) == \-1)
379         handle_error("mq_getattr");
380     buf = malloc(attr.mq_msgsize);
381     if (buf == NULL)
382         handle_error("malloc");
383
384     nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
385     if (nr == \-1)
386         handle_error("mq_receive");
387
388     printf("Read %ld bytes from MQ\\n", (long) nr);
389     free(buf);
390 .\"O     exit(EXIT_SUCCESS);         /* Terminate the process */
391     exit(EXIT_SUCCESS);         /* ¥×¥í¥»¥¹¤ò½ªÎ»¤¹¤ë */
392 }
393
394 int
395 main(int argc, char *argv[])
396 {
397     mqd_t mqdes;
398     struct sigevent not;
399
400     if (argc != 2) {
401         fprintf(stderr, "Usage: %s <mq-name>\\n", argv[0]);
402         exit(EXIT_FAILURE);
403     }
404
405     mqdes = mq_open(argv[1], O_RDONLY);
406     if (mqdes == (mqd_t) \-1)
407         handle_error("mq_open");
408
409     not.sigev_notify = SIGEV_THREAD;
410     not.sigev_notify_function = tfunc;
411     not.sigev_notify_attributes = NULL;
412 .\"O     not.sigev_value.sival_ptr = &mqdes;   /* Arg. to thread func. */
413     not.sigev_value.sival_ptr = &mqdes;   /* ¥¹¥ì¥Ã¥É´Ø¿ô¤ËÅϤ¹°ú¤­¿ô */
414     if (mq_notify(mqdes, &not) == \-1)
415         handle_error("mq_notify");
416
417 .\"O     pause();    /* Process will be terminated by thread function */
418     pause();    /* ¥×¥í¥»¥¹¤Ï¥¹¥ì¥Ã¥É´Ø¿ô¤Ë¤è¤ê½ªÎ»¤µ¤ì¤ë */
419 }
420 .fi
421 .\"O .SH "SEE ALSO"
422 .SH ´ØÏ¢¹àÌÜ
423 .BR mq_close (3),
424 .BR mq_getattr (3),
425 .BR mq_open (3),
426 .BR mq_receive (3),
427 .BR mq_send (3),
428 .BR mq_unlink (3),
429 .BR mq_overview (7)