OSDN Git Service

(split) LDP: Restore and add Copyrights for draft pages
[linuxjm/LDP_man-pages.git] / draft / man3 / mq_notify.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 .\"*******************************************************************
27 .\"
28 .\" This file was generated with po4a. Translate the source file.
29 .\"
30 .\"*******************************************************************
31 .\"
32 .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI all rights reserved.
33 .\" Translated 2006-07-31, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
34 .\" Updated 2010-04-18, Akihiro MOTOKI, LDP v3.24
35 .\" Updated 2010-10-27, Akihiro Motoki, LDP v3.29
36 .\"
37 .TH MQ_NOTIFY 3 2010\-10\-04 Linux "Linux Programmer's Manual"
38 .SH 名前
39 mq_notify \- メッセージ到着時に通知を行うよう登録する
40 .SH 書式
41 .nf
42 \fB#include <mqueue.h>\fP
43 .sp
44 \fBint mq_notify(mqd_t \fP\fImqdes\fP\fB, const struct sigevent *\fP\fInotification\fP\fB);\fP
45 .fi
46 .sp
47 \fI\-lrt\fP でリンクする。
48 .SH 説明
49 \fBmq_notify\fP()  を使うと、ディスクリプタ \fImqdes\fP で参照される空のメッセージキューに新しくメッセージが到着した時に
50 非同期の通知 (notification) の配送が行われるように登録したり、 その解除を行ったりできる。
51
52 \fIsevp\fP 引き数は \fIsigevent\fP 構造体へのポインタである。 この構造体の定義と一般的な詳細については \fBsigevent\fP(7)
53 を参照。
54 .PP
55 \fIsevp\fP が NULL でないポインタであれば、 \fBmq_notify\fP()  はメッセージ通知を受け取るように呼び出し元のプロセスを登録する。
56 \fIsevp\fP が指す \fIsigevent\fP 構造体の \fIsigev_notify\fP フィールドは、どのような通知を行うのかを指定する。
57 このフィールドは以下の値のいずれかを持つ。
58 .TP 
59 \fBSIGEV_NONE\fP
60 .\" When is SIGEV_NONE useful?
61 「空の (null)」の通知: 呼び出し元のプロセスを通知の宛先として登録するが、 実際にはメッセージが到着した時に通知は送られない。
62 .TP 
63 \fBSIGEV_SIGNAL\fP
64 .\" I don't know of other implementations that set
65 .\" si_pid and si_uid -- MTK
66 \fIsigev_signo\fP で指定されたシグナルを送って、プロセスに通知する。 一般的な詳細については \fBsigevent\fP(7)  を参照。
67 \fIsiginfo_t\fP 構造体の \fIsi_code\fP フィールドには \fBSI_MESGQ\fP が設定される。 さらに、 \fIsi_pid\fP
68 にはメッセージを送信したプロセスの PID が、 \fIsi_uid\fP には送信プロセスの実ユーザ ID が設定される。
69 .TP 
70 \fBSIGEV_THREAD\fP
71 メッセージの配送時には、 \fIsigev_notify_function\fP があたかも新しいスレッドの開始関数であるかのように起動される。 詳細は
72 \fBsigevent\fP(7)  を参照。
73 .PP
74 一つのメッセージキューから通知を受信するように登録できるプロセスは 一つだけである。
75
76 \fIsevp\fP が NULL で、かつ呼び出し元のプロセスがこのメッセージキューからの 通知を受信するに現在登録している場合、登録を削除する。
77 これ以降、別のプロセスがこのメッセージキューから通知を受信するように 登録できるようになる。
78
79 メッセージ通知は、それまで空のキューに新しいメッセージが到着した 場合にのみ行われる。 \fBmq_notify\fP()
80 が呼び出された時にそのキューが空でない場合、 そのキューが空になり、その後新しいメッセージが到着した時に 初めて通知が行われることになる。
81
82 別のプロセスやスレッドが \fBmq_receive\fP(3)  を使って、空のキューからメッセージの読み出しを待っている場合、
83 メッセージ通知の登録は全て無視される。 メッセージは \fBmq_receive\fP(3)  を呼び出しているプロセスやスレッドに配送され、
84 メッセージ通知の登録は効力を持ったままとなる。
85
86 通知は一度だけ行われる。通知が送られた後は、通知要求の登録は削除され、 別のプロセスがメッセージ通知を受信するように登録できるようになる。
87 通知を受けたプロセスが次の通知も受信したい場合は、 \fBmq_notify\fP()  を使ってその後の通知も受けるように要求することができる。
88 \fBmq_notify\fP()  を再度呼び出すのは、読み出していないメッセージを全部読み出して キューが空になる前にすべきである
89 (キューからのメッセージ読み出しをキューが空になった時に 停止 (block) せずに行うには、キューを非停止モード (non\-blocking
90 mode)  に設定しておくとよい)。
91 .SH 返り値
92 成功すると、 \fBmq_notify\fP()  は 0 を返す。エラーの場合、\-1 を返し、 \fIerrno\fP をエラーを示す値に設定する。
93 .SH エラー
94 .TP 
95 \fBEBADF\fP
96 \fImqdes\fP に指定されたディスクリプタが不正である。
97 .TP 
98 \fBEBUSY\fP
99 別のプロセスがすでに このメッセージキューに対する通知を受信するように登録している。
100 .TP 
101 \fBEINVAL\fP
102 \fIsevp\->sigev_notify\fP が許可された値のいずれでもない。もしくは \fIsevp\->sigev_notify\fP が
103 \fBSIGEV_SIGNAL\fP だが \fIsevp\->sigev_signo\fP が有効なシグナル番号ではない。
104 .TP 
105 \fBENOMEM\fP
106 十分なメモリがない。
107 .PP
108 .\" Linux does not do this
109 POSIX.1\-2008 では、 \fIsevp\fP が NULL で、呼び出し元のプロセスがキュー \fImqdes\fP
110 に関する通知を受信するように登録されていない場合、エラー \fBEINVAL\fP を生成するような実装を行っても「よい」ことになっている。
111 .SH 準拠
112 POSIX.1\-2001.
113 .SH 例
114 以下のプログラムは、 コマンドライン引き数で指定された名前のメッセージキューへの 通知要求を登録し、通知はスレッドの作成によって行われる。
115 そのスレッドは、そのキューからメッセージを一つ読み出してから、 プロセスを終了する関数を実行する。
116 .nf
117
118 #include <pthread.h>
119 #include <mqueue.h>
120 #include <stdio.h>
121 #include <stdlib.h>
122 #include <unistd.h>
123
124 #define handle_error(msg) \e
125     do { perror(msg); exit(EXIT_FAILURE); } while (0)
126
127 static void                     /* スレッド開始関数 */
128 tfunc(union sigval sv)
129 {
130     struct mq_attr attr;
131     ssize_t nr;
132     void *buf;
133     mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
134
135     /* 最大メッセージサイズを決定し、
136        メッセージ受信用のバッファを確保する */
137
138     if (mq_getattr(mqdes, &attr) == \-1)
139         handle_error("mq_getattr");
140     buf = malloc(attr.mq_msgsize);
141     if (buf == NULL)
142         handle_error("malloc");
143
144     nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
145     if (nr == \-1)
146         handle_error("mq_receive");
147
148     printf("Read %ld bytes from MQ\en", (long) nr);
149     free(buf);
150     exit(EXIT_SUCCESS);         /* プロセスを終了する */
151 }
152
153 int
154 main(int argc, char *argv[])
155 {
156     mqd_t mqdes;
157     struct sigevent sev;
158
159     if (argc != 2) {
160         fprintf(stderr, "Usage: %s <mq\-name>\en", argv[0]);
161         exit(EXIT_FAILURE);
162     }
163
164     mqdes = mq_open(argv[1], O_RDONLY);
165     if (mqdes == (mqd_t) \-1)
166         handle_error("mq_open");
167
168     sev.sigev_notify = SIGEV_THREAD;
169     sev.sigev_notify_function = tfunc;
170     sev.sigev_notify_attributes = NULL;
171     sev.sigev_value.sival_ptr = &mqdes;   /* スレッド関数に渡す引き数 */
172     if (mq_notify(mqdes, &sev) == \-1)
173         handle_error("mq_notify");
174
175     pause();    /* プロセスはスレッド関数により終了される */
176 }
177 .fi
178 .SH 関連項目
179 \fBmq_close\fP(3), \fBmq_getattr\fP(3), \fBmq_open\fP(3), \fBmq_receive\fP(3),
180 \fBmq_send\fP(3), \fBmq_unlink\fP(3), \fBmq_overview\fP(7), \fBsigevent\fP(7)
181 .SH この文書について
182 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
183 である。プロジェクトの説明とバグ報告に関する情報は
184 http://www.kernel.org/doc/man\-pages/ に書かれている。