OSDN Git Service

16167c561c16c0aff0a40450dd32229fec86bf53
[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 .\" Updated 2010-10-27, Akihiro Motoki, LDP v3.29
30 .\"
31 .TH MQ_NOTIFY 3 2010-10-04 "Linux" "Linux Programmer's Manual"
32 .\"O .SH NAME
33 .SH 名前
34 .\"O mq_notify \- register for notification when a message is available
35 mq_notify \- メッセージ到着時に通知を行うよう登録する
36 .\"O .SH SYNOPSIS
37 .SH 書式
38 .nf
39 .B #include <mqueue.h>
40 .sp
41 .BI "int mq_notify(mqd_t " mqdes ", const struct sigevent *" notification );
42 .fi
43 .sp
44 .\"O Link with \fI\-lrt\fP.
45 \fI\-lrt\fP でリンクする。
46 .\"O .SH DESCRIPTION
47 .SH 説明
48 .\"O .BR mq_notify ()
49 .\"O allows the calling process to register or unregister for delivery of
50 .\"O an asynchronous notification when a new message arrives on
51 .\"O the empty message queue referred to by the descriptor
52 .\"O .IR mqdes .
53 .BR mq_notify ()
54 を使うと、ディスクリプタ
55 .I mqdes
56 で参照される空のメッセージキューに新しくメッセージが到着した時に
57 非同期の通知 (notification) の配送が行われるように登録したり、
58 その解除を行ったりできる。
59
60 .\"O The
61 .\"O .I sevp
62 .\"O argument is a pointer to a
63 .\"O .I sigevent
64 .\"O structure.
65 .\"O For the definition and general details of this structure, see
66 .\"O .BR sigevent (7).
67 .I sevp
68 引き数は
69 .I sigevent
70 構造体へのポインタである。
71 この構造体の定義と一般的な詳細については
72 .BR sigevent (7)
73 を参照。
74 .PP
75 .\"O If
76 .\"O .I sevp
77 .\"O is a non-NULL pointer, then
78 .\"O .BR mq_notify ()
79 .\"O registers the calling process to receive message notification.
80 .I sevp
81 が NULL でないポインタであれば、
82 .BR mq_notify ()
83 はメッセージ通知を受け取るように呼び出し元のプロセスを登録する。
84 .\"O The
85 .\"O .I sigev_notify
86 .\"O field of the
87 .\"O .I sigevent
88 .\"O structure to which
89 .\"O .I sevp
90 .\"O points specifies how notification is to be performed.
91 .\"O This field has one of the following values:
92 .I sevp
93 が指す
94 .I sigevent
95 構造体の
96 .I sigev_notify
97 フィールドは、どのような通知を行うのかを指定する。
98 このフィールドは以下の値のいずれかを持つ。
99 .TP
100 .B SIGEV_NONE
101 .\"O A "null" notification: the calling process is registered as the target
102 .\"O for notification, but when a message arrives, no notification is sent.
103 「空の (null)」の通知: 呼び出し元のプロセスを通知の宛先として登録するが、
104 実際にはメッセージが到着した時に通知は送られない。
105 .\" When is SIGEV_NONE useful?
106 .TP
107 .B SIGEV_SIGNAL
108 .\"O Notify the process by sending the signal specified in
109 .\"O .IR sigev_signo .
110 .\"O See
111 .\"O .BR sigevent (7)
112 .\"O for general details.
113 .I sigev_signo
114 で指定されたシグナルを送って、プロセスに通知する。
115 一般的な詳細については
116 .BR sigevent (7)
117 を参照。
118 .\"O The
119 .\"O .I si_code
120 .\"O field of the
121 .\"O .I siginfo_t
122 .\"O structure will be set to
123 .\"O .BR SI_MESGQ .
124 .I siginfo_t
125 構造体の
126 .I si_code
127 フィールドには
128 .B SI_MESGQ
129 が設定される。
130 .\"O In addition,
131 .\" I don't know of other implementations that set
132 .\" si_pid and si_uid -- MTK
133 .\"O .I si_pid
134 .\"O will be set to the PID of the process that sent the message, and
135 .\"O .I si_uid
136 .\"O will be set to the real user ID of the sending process.
137 さらに、
138 .I si_pid
139 にはメッセージを送信したプロセスの PID が、
140 .I si_uid
141 には送信プロセスの実ユーザ ID が設定される。
142 .TP
143 .B SIGEV_THREAD
144 .\"O Upon message delivery, invoke
145 .\"O .I sigev_notify_function
146 .\"O as if it were the start function of a new thread.
147 .\"O See
148 .\"O .BR sigevent (7)
149 .\"O for details.
150 メッセージの配送時には、
151 .I sigev_notify_function
152 があたかも新しいスレッドの開始関数であるかのように起動される。
153 詳細は
154 .BR sigevent (7)
155 を参照。
156 .PP
157 .\"O Only one process can be registered to receive notification
158 .\"O from a message queue.
159 一つのメッセージキューから通知を受信するように登録できるプロセスは
160 一つだけである。
161
162 .\"O If
163 .\"O .I sevp
164 .\"O is NULL, and the calling process is currently registered to receive
165 .\"O notifications for this message queue, then the registration is removed;
166 .\"O another process can then register to receive a message notification
167 .\"O for this queue.
168 .I sevp
169 が NULL で、かつ呼び出し元のプロセスがこのメッセージキューからの
170 通知を受信するに現在登録している場合、登録を削除する。
171 これ以降、別のプロセスがこのメッセージキューから通知を受信するように
172 登録できるようになる。
173
174 .\"O Message notification only occurs when a new message arrives and
175 .\"O the queue was previously empty.
176 .\"O If the queue was not empty at the time
177 .\"O .BR mq_notify ()
178 .\"O was called, then a notification will only occur after
179 .\"O the queue is emptied and a new message arrives.
180 メッセージ通知は、それまで空のキューに新しいメッセージが到着した
181 場合にのみ行われる。
182 .BR mq_notify ()
183 が呼び出された時にそのキューが空でない場合、
184 そのキューが空になり、その後新しいメッセージが到着した時に
185 初めて通知が行われることになる。
186
187 .\"O If another process or thread is waiting to read a message
188 .\"O from an empty queue using
189 .\"O .BR mq_receive (3),
190 .\"O then any message notification registration is ignored:
191 .\"O the message is delivered to the process or thread calling
192 .\"O .BR mq_receive (3),
193 .\"O and the message notification registration remains in effect.
194 別のプロセスやスレッドが
195 .BR mq_receive (3)
196 を使って、空のキューからメッセージの読み出しを待っている場合、
197 メッセージ通知の登録は全て無視される。
198 メッセージは
199 .BR mq_receive (3)
200 を呼び出しているプロセスやスレッドに配送され、
201 メッセージ通知の登録は効力を持ったままとなる。
202
203 .\"O Notification occurs once: after a notification is delivered,
204 .\"O the notification registration is removed,
205 .\"O and another process can register for message notification.
206 .\"O If the notified process wishes to receive the next notification,
207 .\"O it can use
208 .\"O .BR mq_notify ()
209 .\"O to request a further notification.
210 .\"O This should be done before emptying all unread messages from the queue.
211 .\"O (Placing the queue in nonblocking mode is useful for emptying
212 .\"O the queue of messages without blocking once it is empty.)
213 通知は一度だけ行われる。通知が送られた後は、通知要求の登録は削除され、
214 別のプロセスがメッセージ通知を受信するように登録できるようになる。
215 通知を受けたプロセスが次の通知も受信したい場合は、
216 .BR mq_notify ()
217 を使ってその後の通知も受けるように要求することができる。
218 .BR mq_notify ()
219 を再度呼び出すのは、読み出していないメッセージを全部読み出して
220 キューが空になる前にすべきである
221 (キューからのメッセージ読み出しをキューが空になった時に
222 停止 (block) せずに行うには、キューを非停止モード (non-blocking mode)
223 に設定しておくとよい)。
224 .\"O .SH RETURN VALUE
225 .SH 返り値
226 .\"O On success
227 .\"O .BR mq_notify ()
228 .\"O returns 0; on error, \-1 is returned, with
229 .\"O .I errno
230 .\"O set to indicate the error.
231 成功すると、
232 .BR mq_notify ()
233 は 0 を返す。エラーの場合、\-1 を返し、
234 .I errno
235 をエラーを示す値に設定する。
236 .\"O .SH ERRORS
237 .SH エラー
238 .TP
239 .B EBADF
240 .\"O The descriptor specified in
241 .\"O .I mqdes
242 .\"O is invalid.
243 .I mqdes
244 に指定されたディスクリプタが不正である。
245 .TP
246 .B EBUSY
247 .\"O Another process has already registered to receive notification
248 .\"O for this message queue.
249 別のプロセスがすでに
250 このメッセージキューに対する通知を受信するように登録している。
251 .TP
252 .B EINVAL
253 .\"O .I sevp\->sigev_notify
254 .\"O is not one of the permitted values; or
255 .\"O .I sevp\->sigev_notify
256 .\"O is
257 .\"O .B SIGEV_SIGNAL
258 .\"O and
259 .\"O .I sevp\->sigev_signo
260 .\"O is not a valid signal number.
261 .I sevp\->sigev_notify
262 が許可された値のいずれでもない。もしくは
263 .I sevp\->sigev_notify
264
265 .B SIGEV_SIGNAL
266 だが
267 .I sevp\->sigev_signo
268 が有効なシグナル番号ではない。
269 .TP
270 .B ENOMEM
271 .\"O Insufficient memory.
272 必要なメモリがなかった。
273 .PP
274 .\"O POSIX.1-2008 says that an implementation
275 .\"O .I may
276 .\"O generate an
277 .\"O .B EINVAL
278 .\"O .\" Linux does not do this
279 .\"O error if
280 .\"O .I sevp
281 .\"O is NULL, and the caller is not currently registered to receive
282 .\"O notifications for the queue
283 .\"O .IR mqdes .
284 POSIX.1-2008 では、
285 .I sevp
286 が NULL で、呼び出し元のプロセスがキュー
287 .I mqdes
288 に関する通知を受信するように登録されていない場合、エラー
289 .B EINVAL
290 を生成するような実装を行っても「よい」ことになっている。
291 .\" Linux の実装では EINVAL は生成されない
292 .\"O .SH CONFORMING TO
293 .SH 準拠
294 POSIX.1-2001.
295 .\"O .SH EXAMPLE
296 .SH 例
297 .\"O The following program registers a notification request for the
298 .\"O message queue named in its command-line argument.
299 .\"O Notification is performed by creating a thread.
300 .\"O The thread executes a function which reads one message from the
301 .\"O queue and then terminates the process.
302 以下のプログラムは、
303 コマンドライン引き数で指定された名前のメッセージキューへの
304 通知要求を登録し、通知はスレッドの作成によって行われる。
305 そのスレッドは、そのキューからメッセージを一つ読み出してから、
306 プロセスを終了する関数を実行する。
307 .nf
308
309 #include <pthread.h>
310 #include <mqueue.h>
311 #include <stdio.h>
312 #include <stdlib.h>
313 #include <unistd.h>
314
315 #define handle_error(msg) \\
316     do { perror(msg); exit(EXIT_FAILURE); } while (0)
317
318 .\"O static void                     /* Thread start function */
319 static void                     /* スレッド開始関数 */
320 tfunc(union sigval sv)
321 {
322     struct mq_attr attr;
323     ssize_t nr;
324     void *buf;
325     mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
326
327 .\"O     /* Determine max. msg size; allocate buffer to receive msg */
328     /* 最大メッセージサイズを決定し、
329        メッセージ受信用のバッファを確保する */
330
331     if (mq_getattr(mqdes, &attr) == \-1)
332         handle_error("mq_getattr");
333     buf = malloc(attr.mq_msgsize);
334     if (buf == NULL)
335         handle_error("malloc");
336
337     nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
338     if (nr == \-1)
339         handle_error("mq_receive");
340
341     printf("Read %ld bytes from MQ\\n", (long) nr);
342     free(buf);
343 .\"O     exit(EXIT_SUCCESS);         /* Terminate the process */
344     exit(EXIT_SUCCESS);         /* プロセスを終了する */
345 }
346
347 int
348 main(int argc, char *argv[])
349 {
350     mqd_t mqdes;
351     struct sigevent sev;
352
353     if (argc != 2) {
354         fprintf(stderr, "Usage: %s <mq-name>\\n", argv[0]);
355         exit(EXIT_FAILURE);
356     }
357
358     mqdes = mq_open(argv[1], O_RDONLY);
359     if (mqdes == (mqd_t) \-1)
360         handle_error("mq_open");
361
362     sev.sigev_notify = SIGEV_THREAD;
363     sev.sigev_notify_function = tfunc;
364     sev.sigev_notify_attributes = NULL;
365 .\"O     sev.sigev_value.sival_ptr = &mqdes;   /* Arg. to thread func. */
366     sev.sigev_value.sival_ptr = &mqdes;   /* スレッド関数に渡す引き数 */
367     if (mq_notify(mqdes, &sev) == \-1)
368         handle_error("mq_notify");
369
370 .\"O     pause();    /* Process will be terminated by thread function */
371     pause();    /* プロセスはスレッド関数により終了される */
372 }
373 .fi
374 .\"O .SH "SEE ALSO"
375 .SH 関連項目
376 .BR mq_close (3),
377 .BR mq_getattr (3),
378 .BR mq_open (3),
379 .BR mq_receive (3),
380 .BR mq_send (3),
381 .BR mq_unlink (3),
382 .BR mq_overview (7),
383 .BR sigevent (7)