OSDN Git Service

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