|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
# s# u% V, E- }' Z消息队列是Linux IPC中很常用的一种通信方式,它通常用来在不同进程间发送特定格式的消息数据。7 {" n; Z, P4 V- a' c
V3 T4 V8 M, E# I Y
消息队列和之前讨论过的管道和FIFO有很大的区别,主要有以下两点:
) x; E6 | k9 W4 b7 P
- B* u' Y7 T2 ^ P' j0 v$ W7 T- 一个进程向消息队列写入消息之前,并不需要某个进程在该队列上等待该消息的到达,而管道和FIFO是相反的,进程向其中写消息时,管道和FIFO必需已经打开来读,那么内核会产生SIGPIPE信号(感谢shanshan_fangfang的指正)。
- IPC的持续性不同。管道和FIFO是随进程的持续性,当管道和FIFO最后一次关闭发生时,仍在管道和FIFO中的数据会被丢弃。消息队列是随内核的持续性,即一个进程向消息队列写入消息后,然后终止,另外一个进程可以在以后某个时刻打开该队列读取消息。只要内核没有重新自举,消息队列没有被删除。
+ n% j6 p8 z1 ~( H/ { / s0 m4 c. U9 m5 S! d
0 Y/ y3 E8 m/ |, H
消息队列中的每条消息通常具有以下属性:& l2 V! }4 }& T
0 B R, U e. ~- 一个表示优先级的整数;
- 消息的数据部分的长度;
- 消息数据本身;
/ S7 p8 c) B0 I+ S& y! Q
# o6 `: N( G' ~' f6 }POSIX消息队列的一个可能的设计是一个如下图所示的消息链表,链表头部有消息队列的属性信息。
' v4 x) S; g$ s, K* z- J$ q. }
2 Y* b, E# D7 l& C: y
* R- d' n: t' c! e- s& i. W2 K/ ^% e: ]
图1消息队列的可能布局, y/ ?3 q2 X; p5 y$ `
0 Q5 h' [: H5 E6 }1 POSIX消息队列的创建和关闭; V R: v) K( \) n$ d' a% _" ?, \
POSIX消息队列的创建,关闭和删除用到以下三个函数接口:6 a- |/ Z- S F, a4 f1 e
7 n: @2 ?) j( W' J. B
- #include <mqueue.h>
- mqd_t mq_open(const char *name, int oflag, /* mode_t mode, struct mq_attr *attr */);
- //成功返回消息队列描述符,失败返回-1
- mqd_t mq_close(mqd_t mqdes);
- mqd_t mq_unlink(const char *name);
- //成功返回0,失败返回-1
# r6 D5 m4 R) h( K0 A# R 9 ^" c# [4 p0 ?
" r+ k! W! a3 m" t( ~
mq_open用于打开或创建一个消息队列。2 A# x9 o& x$ p; A+ E _) B# i$ g
name:表示消息队列的名字,它符合POSIX IPC的名字规则。+ r* M, N9 ~$ A9 y3 ~) H% X8 I8 |
( F8 |+ y8 [! X; coflag:表示打开的方式,和open函数的类似。有必须的选项:O_RDONLY,O_WRONLY,O_RDWR,还有可选的选项:O_NONBLOCK,O_CREAT,O_EXCL。
( q4 N0 @# u1 H4 X/ L- \2 j! Q. {" O. Y7 ?7 M
mode:是一个可选参数,在oflag中含有O_CREAT标志且消息队列不存在时,才需要提供该参数。表示默认访问权限。可以参考open。/ Z5 Q" U: E2 K5 X% R5 W
# z$ l1 ~' d' @
attr:也是一个可选参数,在oflag中含有O_CREAT标志且消息队列不存在时才需要。该参数用于给新队列设定某些属性,如果是空指针,那么就采用默认属性。
7 `4 J7 ]8 \' v n, q/ I. l
) T, ]* _. o' b6 @8 qmq_open返回值是mqd_t类型的值,被称为消息队列描述符。在Linux 2.6.18中该类型的定义为整型:
' i n5 p8 h) g. f
7 ~& F1 P, _( q- #include <bits/mqueue.h>
- typedef int mqd_t;2 K; n, `2 y* q$ l+ U4 U& U+ Q& v
1 i* M# F9 X8 a! G
0 ~6 h8 s' L% o8 m0 A
mq_close用于关闭一个消息队列,和文件的close类型,关闭后,消息队列并不从系统中删除。一个进程结束,会自动调用关闭打开着的消息队列。( K: h9 F' ? g& L
! k& F8 V: }- o1 ~
mq_unlink用于删除一个消息队列。消息队列创建后只有通过调用该函数或者是内核自举才能进行删除。每个消息队列都有一个保存当前打开着描述符数的引用计数器,和文件一样,因此本函数能够实现类似于unlink函数删除一个文件的机制。
. D3 I( t' q9 }3 C4 n7 K* d% r6 x& O5 y) @( f7 S
POSIX消息队列的名字所创建的真正路径名和具体的系统实现有关,关于具体POSIX IPC的名字规则可以参考《UNIX 网络编程 卷2:进程间通信》的P14。; j- z8 R1 x, p' \
' i Q" H# E' w7 T经过测试,在Linux 2.6.18中,所创建的POSIX消息队列不会在文件系统中创建真正的路径名。且POSIX的名字只能以一个’/’开头,名字中不能包含其他的’/’。3 |% m8 I; k; y. ]
# C& w) S9 Z! |* X: ~5 M, S- b) K
" n7 F+ [/ B' ^$ J5 r
2 POSIX消息队列的属性9 t5 @ t, P- V; \# {
/ K2 P# }5 z: \7 c9 lPOSIX标准规定消息队列属性mq_attr必须要含有以下四个内容:5 D3 m5 G$ g" f6 B) m; L
4 w. i/ @" j6 }+ @
- long mq_flags //消息队列的标志:0或O_NONBLOCK,用来表示是否阻塞
- long mq_maxmsg //消息队列的最大消息数
- long mq_msgsize //消息队列中每个消息的最大字节数
- long mq_curmsgs //消息队列中当前的消息数目
0 F; L$ j5 u3 u
_; w8 d# h. o6 d
& _8 f6 |# z: R0 }- z; L) f+ |在Linux 2.6.18中mq_attr结构的定义如下:
9 J1 f3 t4 ~# q$ e5 I
6 [8 C/ W* R% J6 @" Z& |* ]6 M- #include <bits/mqueue.h>
- struct mq_attr
- {
- long int mq_flags; /* Message queue flags. */
- long int mq_maxmsg; /* Maximum number of messages. */
- long int mq_msgsize; /* Maximum message size. */
- long int mq_curmsgs; /* Number of messages currently queued. */
- long int __pad[4];
- };4 T( m( N6 r, w4 H. o5 {( s
. ^9 S* \3 ?% _' G0 C
: y1 s( |* U; `* O r+ `POSIX消息队列的属性设置和获取可以通过下面两个函数实现:: m8 H& b) L3 a M1 P- n5 \9 W, }
( y5 T* k9 q" p7 \2 y3 m) f+ W- #include <mqueue.h>
- mqd_t mq_getattr(mqd_t mqdes, struct mq_attr *attr);
- mqd_t mq_setattr(mqd_t mqdes, struct mq_attr *newattr, struct mq_attr *oldattr);
- //成功返回0,失败返回-1
) M- w7 H+ `7 l- [( c 0 b' `/ W5 T0 U3 P
# }8 n( k- L: M6 | t
mq_getattr用于获取当前消息队列的属性,mq_setattr用于设置当前消息队列的属性。其中mq_setattr中的oldattr用于保存修改前的消息队列的属性,可以为空。
8 O9 }2 _# G3 h
$ ^7 W. Z* [8 Z3 Jmq_setattr可以设置的属性只有mq_flags,用来设置或清除消息队列的非阻塞标志。newattr结构的其他属性被忽略。mq_maxmsg和mq_msgsize属性只能在创建消息队列时通过mq_open来设置。mq_open只会设置该两个属性,忽略另外两个属性。mq_curmsgs属性只能被获取而不能被设置。
, n& z) B; Q6 p0 O6 c2 P9 b' g3 Y- t5 ~1 e1 h
下面是测试代码:
- o: d4 Z2 O( f2 j1 i, T
! x% J" W( u" D0 A, G/ [- #include <iostream>
- #include <cstring>
- #include <errno.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <mqueue.h>
- using namespace std;
- int main()
- {
- mqd_t mqID;
- mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT, 0666, NULL);
- if (mqID < 0)
- {
- cout<<"open message queue error..."<<strerror(errno)<<endl;
- return -1;
- }
- mq_attr mqAttr;
- if (mq_getattr(mqID, &mqAttr) < 0)
- {
- cout<<"get the message queue attribute error"<<endl;
- return -1;
- }
- cout<<"mq_flags:"<<mqAttr.mq_flags<<endl;
- cout<<"mq_maxmsg:"<<mqAttr.mq_maxmsg<<endl;
- cout<<"mq_msgsize:"<<mqAttr.mq_msgsize<<endl;
- cout<<"mq_curmsgs:"<<mqAttr.mq_curmsgs<<endl;
- }9 ^/ r1 f( e6 d5 V7 F
5 y. N3 Z$ V/ y
9 V8 q5 z4 L" B5 {; l4 d! L. ` 在Linux 2.6.18中执行结果是:* B4 f7 A" m" F9 `
& B- s3 J& u8 v2 G+ q+ E/ h
- mq_flags:0
- mq_maxmsg:10
- mq_msgsize:8192
- mq_curmsgs:0" U0 B- j; c0 f8 ~% j' [
3 n. i5 [0 f5 L( j" K+ ?0 Q9 ^
- m; B q* O4 \2 N& Q3 POSIX消息队列的使用
0 t; N: ]# \# z* |6 f
& L) U& s! O& Y5 MPOSIX消息队列可以通过以下两个函数来进行发送和接收消息:1 G4 d& z0 m. G. ~! P) r7 U) |, e B
$ |' o) v, t5 J, x2 C3 N
- #include <mqueue.h>
- mqd_t mq_send(mqd_t mqdes, const char *msg_ptr,
- size_t msg_len, unsigned msg_prio);
- //成功返回0,出错返回-1
- mqd_t mq_receive(mqd_t mqdes, char *msg_ptr,
- size_t msg_len, unsigned *msg_prio);
- //成功返回接收到消息的字节数,出错返回-1
- #ifdef __USE_XOPEN2K
- mqd_t mq_timedsend(mqd_t mqdes, const char *msg_ptr,
- size_t msg_len, unsigned msg_prio,
- const struct timespec *abs_timeout);
- mqd_t mq_timedreceive(mqd_t mqdes, char *msg_ptr,
- size_t msg_len, unsigned *msg_prio,
- const struct timespec *abs_timeout);
- #endif5 @# s# T" g0 T% l8 e
* _+ Q1 `6 ?7 X( m- I' O
( H; C' k! t5 d- f' ~- j
- U/ B8 g! J R mq_send向消息队列中写入一条消息,mq_receive从消息队列中读取一条消息。
/ v6 q, @" H. s4 a( w3 D6 K7 N. {8 U
mqdes:消息队列描述符;" [( F' {) q, @5 O
% B; \, g" m% X. _) p3 `5 j! H0 Emsg_ptr:指向消息体缓冲区的指针;
! C% Q6 r( ^9 W
! n) J* P' Y, p8 m2 k) smsg_len:消息体的长度,其中mq_receive的该参数不能小于能写入队列中消息的最大大小,即一定要大于等于该队列的mq_attr结构中mq_msgsize的大小。如果mq_receive中的msg_len小于该值,就会返回EMSGSIZE错误。POXIS消息队列发送的消息长度可以为0。2 S4 Z R& J% F& B) [% J2 ~$ ^
3 y7 m$ u( X$ T, t* Z* _. x7 Amsg_prio:消息的优先级;它是一个小于MQ_PRIO_MAX的数,数值越大,优先级越高。POSIX消息队列在调用mq_receive时总是返回队列中最高优先级的最早消息。如果消息不需要设定优先级,那么可以在mq_send是置msg_prio为0,mq_receive的msg_prio置为NULL。# O2 Z/ ]8 p( c: P" k, j
6 l9 j/ W# l& e/ g5 [
还有两个XSI定义的扩展接口限时发送和接收消息的函数:mq_timedsend和mq_timedreceive函数。默认情况下mq_send和mq_receive是阻塞进行调用,可以通过mq_setattr来设置为O_NONBLOCK。0 b0 R9 R2 ^0 s) X& J' s6 n, P
8 l* p9 d, |8 H
% ]" {( g, b) B+ q6 _* @下面是消息队列使用的测试代码:8 d: f7 Z1 ^& _# v4 A+ d
6 W* ~& O" Z$ I s/ s/ M* h& _
- #include <iostream>
- #include <cstring>
- #include <errno.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <mqueue.h>
- using namespace std;
- int main()
- {
- mqd_t mqID;
- mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT | O_EXCL, 0666, NULL);
- if (mqID < 0)
- {
- if (errno == EEXIST)
- {
- mq_unlink("/anonymQueue");
- mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT, 0666, NULL);
- }
- else
- {
- cout<<"open message queue error..."<<strerror(errno)<<endl;
- return -1;
- }
- }
- if (fork() == 0)
- {
- mq_attr mqAttr;
- mq_getattr(mqID, &mqAttr);
- char *buf = new char[mqAttr.mq_msgsize];
- for (int i = 1; i <= 5; ++i)
- {
- if (mq_receive(mqID, buf, mqAttr.mq_msgsize, NULL) < 0)
- {
- cout<<"receive message failed. ";
- cout<<"error info:"<<strerror(errno)<<endl;
- continue;
- }
- cout<<"receive message "<<i<<": "<<buf<<endl;
- }
- exit(0);
- }
- char msg[] = "yuki";
- for (int i = 1; i <= 5; ++i)
- {
- if (mq_send(mqID, msg, sizeof(msg), i) < 0)
- {
- cout<<"send message "<<i<<" failed. ";
- cout<<"error info:"<<strerror(errno)<<endl;
- }
- cout<<"send message "<<i<<" success. "<<endl;
- sleep(1);
- }
- }
; O% T6 O, x7 y
0 u0 _6 `4 \& g$ ?! U4 S! u/ M
5 W; G! L" G# L9 F* g* U在Linux 2.6.18下的执行结构如下:
4 u) m6 ^8 q9 ]. f! @! J- Q# \" ?9 c B
- send message 1 success.
- receive message 1: yuki
- send message 2 success.
- receive message 2: yuki
- send message 3 success.
- receive message 3: yuki
- send message 4 success.
- receive message 4: yuki
- send message 5 success.
- receive message 5: yuki* a& N; S. z& B' c4 _
. o+ c! V# `, s
; M+ o+ o4 b7 P# x4 y, `; ^, W
4 POSIX消息队列的限制# i. ]3 C& R; j) j/ G2 \7 I
3 c! I8 G1 Y' E% v$ L6 y Y
POSIX消息队列本身的限制就是mq_attr中的mq_maxmsg和mq_msgsize,分别用于限定消息队列中的最大消息数和每个消息的最大字节数。在前面已经说过了,这两个参数可以在调用mq_open创建一个消息队列的时候设定。当这个设定是受到系统内核限制的。
2 V% [) }+ p; [& H# `+ d+ T9 e+ K; `+ \; R" A3 C/ [* B6 v Z" |
下面是在Linux 2.6.18下shell对启动进程的POSIX消息队列大小的限制: M$ [" }; f% t0 l% S4 h2 N
; ~4 P# |% E; B: S( U4 }- # ulimit -a |grep message
- POSIX message queues (bytes, -q) 819200
. d4 X2 L I) C8 j% R6 M + H8 C7 ]2 A5 X0 R9 Z
9 {9 ^ w8 O( Y4 N7 d限制大小为800KB,该大小是整个消息队列的大小,不仅仅是最大消息数*消息的最大大小;还包括消息队列的额外开销。前面我们知道Linux 2.6.18下POSIX消息队列默认的最大消息数和消息的最大大小分别为:/ ?: D2 t+ g3 M/ v
2 M# {9 V" T- p
- mq_maxmsg = 10
- mq_msgsize = 81920 V3 i4 d; z& k" O
+ o7 c* i* W; P8 b: {5 Q
F$ O: R5 L( H3 Q- g* ^
为了说明上面的限制大小包括消息队列的额外开销,下面是测试代码:, c6 s( o( X. h- v! }
( y" M3 O1 V" ?, c2 s- #include <iostream>
- #include <cstring>
- #include <errno.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <mqueue.h>
- using namespace std;
- int main(int argc, char **argv)
- {
- mqd_t mqID;
- mq_attr attr;
- attr.mq_maxmsg = atoi(argv[1]);
- attr.mq_msgsize = atoi(argv[2]);
- mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT | O_EXCL, 0666, &attr);
- if (mqID < 0)
- {
- if (errno == EEXIST)
- {
- mq_unlink("/anonymQueue");
- mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT, 0666, &attr);
- if(mqID < 0)
- {
- cout<<"open message queue error..."<<strerror(errno)<<endl;
- return -1;
- }
- }
- else
- {
- cout<<"open message queue error..."<<strerror(errno)<<endl;
- return -1;
- }
- }
- mq_attr mqAttr;
- if (mq_getattr(mqID, &mqAttr) < 0)
- {
- cout<<"get the message queue attribute error"<<endl;
- return -1;
- }
- cout<<"mq_flags:"<<mqAttr.mq_flags<<endl;
- cout<<"mq_maxmsg:"<<mqAttr.mq_maxmsg<<endl;
- cout<<"mq_msgsize:"<<mqAttr.mq_msgsize<<endl;
- cout<<"mq_curmsgs:"<<mqAttr.mq_curmsgs<<endl;
- }
! M1 V0 e* r5 T3 B# t% c7 Y
2 P* ~; |1 L& a
: C7 g' ]4 h7 j+ R7 i4 N3 L8 x 下面进行创建消息队列时设置最大消息数和消息的最大大小进行测试:
! t) L, {! ]. }4 j5 U$ D$ m
0 N3 M/ j* U2 a- [root@idcserver program]# g++ -g test.cpp -lrt
- [root@idcserver program]# ./a.out 10 81920
- open message queue error...Cannot allocate memory
- [root@idcserver program]# ./a.out 10 80000
- open message queue error...Cannot allocate memory
- [root@idcserver program]# ./a.out 10 70000
- open message queue error...Cannot allocate memory
- [root@idcserver program]# ./a.out 10 60000
- mq_flags:0
- mq_maxmsg:10
- mq_msgsize:60000
- mq_curmsgs:0
6 V8 }3 K/ R+ L& c/ L( [1 \
2 s# Z, {# }4 n/ a w
$ S3 I) Y$ q1 }5 A% l* w从上面可以看出消息队列真正存放消息数据的大小是没有819200B的。可以通过修改该限制参数,来改变消息队列的所能容纳消息的数量。可以通过下面方式来修改限制,但这会在shell启动进程结束后失效,可以将设置写入开机启动的脚本中执行,例如.bashrc,rc.local。
8 a: n& N% O% Q2 N( }6 p2 H
4 u+ s: n, n2 B! j- [root@idcserver ~]# ulimit -q 1024000000
- [root@idcserver ~]# ulimit -a |grep message
- POSIX message queues (bytes, -q) 1024000000
g m1 ]( ?. a3 v& U% y
, x$ k6 J0 Y" i1 z4 @6 a
' c. J/ l7 i& |" x9 W; s% d下面再次测试可以设置的消息队列的属性。- b* d, H! e' d U1 {0 F
( A9 Q, O. X$ U1 N# @1 d3 c- [root@idcserver program]# ./a.out 10 81920
- mq_flags:0
- mq_maxmsg:10
- mq_msgsize:81920
- mq_curmsgs:0
- [root@idcserver program]# ./a.out 10 819200
- mq_flags:0
- mq_maxmsg:10
- mq_msgsize:819200
- mq_curmsgs:0
- [root@idcserver program]# ./a.out 1000 8192
- mq_flags:0
- mq_maxmsg:1000
- mq_msgsize:8192
- mq_curmsgs:0
, J. N, T$ Q8 g6 \" \- t" W / @/ D! z$ l; t0 p ?
& C! o2 X9 f2 j9 w
POSIX消息队列在实现上还有另外两个限制:4 h3 |& T( D# w- \. x
' I' m3 i! P& ]# u) zMQ_OPEN_MAX:一个进程能同时打开的消息队列的最大数目,POSIX要求至少为8;
$ \7 [0 h, U7 i5 e" a: H2 k" s$ W1 {$ J1 Q. D, m2 W
MQ_PRIO_MAX:消息的最大优先级,POSIX要求至少为32;* l# ^- g6 n" O5 v p3 [: Y/ R- \ G
- |7 {5 n" E( o3 O; O
* t% \0 ?3 W0 X# p. S* _4 a
|
|