|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
" Z' r8 u& i& ]4 A
消息队列是消息的链接表,包括Posix消息队列system V消息队列。消息队列用于运行于同一台机器上的进程间通信,它
+ y k( m+ D/ ~$ _/ U8 Y" F4 d和管道很相似,有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。消息队列克服了
8 _ O$ P5 Y4 P1 w9 q/ g( O8 Z信号承载信息量少,管道只能承载无格式字节流以及缓冲区大小受限等缺点。 我们可以用流管道或者套接口的方式来取代它。
1 s/ G6 @9 A9 r8 z6 W( V) v/ ~2 U
4 S' |4 P( F$ _" |. m! X查询系统消息队列:ipcs -q
9 r1 O( G' }4 ?% ^& I
$ V! V- G3 s" A% S( p) i7 w) ]2 g#include <sys/types.h>
`* N: j% [' Z& Q8 _3 \% z5 t+ U/ h" Y2 X$ C$ r. i
#include <sys/ipc.h>" `9 o5 R4 t: b% ^4 i- ~/ u
, I1 u8 u; y- t3 i* [, \- t#include <sys/msg.h>7 Y/ e( v" H3 J- O" D' f3 L
0 b- y0 G- l, A! v, }( \( R
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
) g6 c- G8 `6 D1 b0 F5 u
& E" V3 t% ]! {$ s% Pint msgget(key_t key, int msgflg);
! j" S' {' C6 k
2 y' x, l [5 _ S% g) mint msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);. H) \: u6 R0 M' r
& ?! y# R& C: g, f/ B @! r9 ^
int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);
# ^5 u& f0 \# o8 Z4 y3 x$ D. `' d; T' F) f/ c
9 M! Y3 Z6 \: |0 S: k5 ^( r9 O' }+ T/ T# Z7 \7 i `
C代码# H* C& s- {# s; e) p# [. _
/*msgserver.c*/ 6 y9 L2 s! [1 D/ Y# h
6 i1 Z. d7 m1 d. w
#include <stdlib.h> # ^$ K4 B: b) ]0 A* k w
#include <string.h>
w# \! ^8 @3 Z( l#include <errno.h>
( w" U' S4 \6 T& P#include <sys/types.h>
2 {. M! `' F! s# l. {#include <sys/ipc.h>
( q9 G% g4 \/ J1 e( k2 A#include <sys/msg.h> / p% Q3 `! V" z3 L
#include <sys/stat.h> * i1 ^ U m7 Q" z3 T: j
4 P4 @% _" {4 H#define MSG_FILE "msgserver.c"
* V. N H. O6 D( }4 L4 n! j#define BUFFER 255 * g) I Y9 }4 v E$ o8 ~ i
#define PERM S_IRUSR|S_IWUSR 8 A$ F! f2 r M5 R( a& E. [
/* 服务端创建的消息队列最后没有删除,我们要使用ipcrm命令来删除的 */ & A, O9 s+ t/ m9 d# [
/* ipcrm -q <msqid> */ + A, ]. h' }# F) d1 t" `- t
! X+ ^8 j- z& n8 Z$ U2 q
struct msgtype
8 g! c" K' O. H' r2 O8 r+ z% I{ 0 g' \( N) {) j9 {* q5 @
long mtype;
L8 u$ F6 {3 z B# ^# g: E0 v& y char buffer[BUFFER+1]; 7 ~* z. W7 b+ d# a. L; O
}; . [* \! R9 c1 |
& F( c; r, F3 g' T
int main()
( L- R- ~ {5 A( F{
" q* V, }4 k [& H struct msgtype msg; 8 H5 v6 e+ I& `! u Z
key_t key; 3 c& i8 H; O! m i
int msgid; {, S5 G5 M% N( z9 G
/ l. }0 H" I* D0 _ \
if((key=ftok(MSG_FILE,'a'))==-1) - ^; i" t6 [) r/ _$ G" ]
{ ) ?. G; {! s% ~% T+ Z
fprintf(stderr,"Creat Key Error:%s\n", strerror(errno)); # |. C, @2 [5 l# T9 u
exit(1);
) q$ l6 o" X- Q- j7 L }
3 V1 r0 D. l* {' `% u
- v/ b5 v; D4 r8 \ if((msgid=msgget(key, PERM|IPC_CREAT|IPC_EXCL))==-1)
2 I9 x& s0 U" R+ s( O4 \7 P {
1 {* l& C2 ]# U3 A4 J9 v/ ^ fprintf(stderr, "Creat Message Error:%s\n", strerror(errno));
0 I7 A/ R. U/ M- Y5 _7 ]( G4 K exit(1);
. U$ l3 m, o1 m }
6 T. m: n# m6 E @ X printf("msqid = %d\n", msgid); % {% L6 B0 H- r3 p. R# {
while(1) 6 N* d; p: Z4 d8 |6 j
{
" {7 W( n- t I6 M msgrcv(msgid, &msg, sizeof(struct msgtype), 1, 0); ' G; G9 f5 e/ M% h
fprintf(stderr,"Server Receive:%s\n", msg.buffer);
6 m `% v; T. R f) l8 m' L msg.mtype = 2; 6 I$ q1 F+ U$ \+ C" _
msgsnd(msgid, &msg, sizeof(struct msgtype), 0); 2 L+ d0 C: ~& K# L A# v. V
}
" I# G& e# q# R6 l exit(0);
. U: U: @/ |" q9 x; o' F} 6 ~0 V/ P2 G+ n0 u& e' o/ V
C代码& p( R" L3 E' A! c
/* msgclient.c */ & h" S3 u; z6 j* D1 K
0 G( x* b, N; j2 z* [
#include <stdio.h>
& a v( p% {$ a3 r$ p3 A#include <stdlib.h> - D( K ]; b/ Y4 t2 F- d3 f
#include <string.h>
4 ~1 E* {2 H f2 t; l#include <errno.h> 3 \+ ?1 S3 r! T
#include <sys/types.h>
" ^: l z( q/ D+ \#include <sys/ipc.h>
% I6 Z7 Z8 a* r8 j4 P#include <sys/msg.h> 7 D/ `) f( I! }! A
#include <sys/stat.h> 2 x! T' P* h# _' M1 B" y Y: t
- s* @5 e0 K# O( a" _" M
#define MSG_FILE "msgserver.c" F M, c' {0 G' M
#define BUFFER 255 3 l, B, E" O) a' `, R4 C: i+ _
#define PERM S_IRUSR|S_IWUSR : x8 l5 s1 y1 [+ _
) C' ^& b( V5 O- @' c. ]
struct msgtype {
' a; U! u( m* s6 `. K# d long mtype; : F' ~; R& ? k- H3 E
char buffer[BUFFER+1];
& x4 k1 o1 d! ?/ D}; 6 g' j2 F8 z$ \7 ^6 k
3 @' ~5 }; z9 ?: g
int main(int argc, char **argv) / j/ ~6 w6 X1 X& p
{
1 N; C$ J' L: t1 @4 U1 T$ \6 U struct msgtype msg;
2 ` c* S) T" J# d3 n D! S key_t key; ! K2 i* b M% V& ^' {' F
int msgid; - v. u+ N, M/ }. L0 J
- U. M" m' U: z if(argc != 2) ; n$ {' Q3 d5 t' G' e
{
5 |7 c& D6 Y/ F0 |. j2 s3 L1 A% Y fprintf(stderr,"Usage:%s string\n", argv[0]);
f0 y' E' J F exit(1); 3 y) K. K5 B) y: e
} # c# g" |7 _+ a% J: n6 f* U
4 X6 u- C' c. {( X& D8 d
if((key=ftok(MSG_FILE,'a'))==-1) 9 K1 @; R {& J7 K1 H
{
" h L7 Y3 U5 p/ y/ [5 A fprintf(stderr,"Creat Key Error:%s\n", strerror(errno)); $ p1 ~4 X1 F% S0 p0 q2 ^1 }
exit(1);
5 J' r5 K" }. [$ r6 a% T6 g } y5 I3 L3 K1 p6 g& f4 e, d& F% y
! g3 R2 X0 T5 v% s, J
if((msgid=msgget(key, PERM))==-1)
9 m" b* Y8 a8 Y2 W' J% l1 E$ A% D { $ f: ?- h8 U( I$ {
fprintf(stderr,"Creat Message Error:%s\n", strerror(errno)); 3 v8 i, N) U! o3 Q; w
exit(1); $ L; {% \: q6 i. l- e: B
}
$ K8 S5 e9 Q( ~/ s' |4 D2 h+ _ ! j& c7 X5 ~$ N, Q* { ^1 I6 J" a$ }
msg.mtype = 1; ) o1 O/ m2 R& s/ Z2 L% b5 M
strncpy(msg.buffer, argv[1], BUFFER);
/ J- R u. w3 M0 ` msgsnd(msgid, &msg, sizeof(struct msgtype), 0);
. S- j( B8 _; ^; `1 E% o% \ memset(&msg, '\0', sizeof(struct msgtype));
4 u. N* w5 a0 j$ U, B5 }) M7 @ v/ I msgrcv(msgid, &msg, sizeof(struct msgtype), 2, 0); + x: }5 z- A3 V; E
fprintf(stderr, "Client receive:%s\n", msg.buffer); ; {, ]% m% ~7 F, { n
exit(0); 6 Z: F1 L: l1 U
} |
|