|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
3 J; e5 Y1 u1 U/ p6 Z+ C* [( k, g/ I; l
消息队列是消息的链接表,包括Posix消息队列system V消息队列。消息队列用于运行于同一台机器上的进程间通信,它
& o* I2 j: b. C) v' x S0 ~% f+ x9 D$ e和管道很相似,有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。消息队列克服了# }$ l/ @# m9 x1 i1 X |
信号承载信息量少,管道只能承载无格式字节流以及缓冲区大小受限等缺点。 我们可以用流管道或者套接口的方式来取代它。. w/ @* m `8 R
. i8 x y5 P$ U( T+ ~2 b5 m查询系统消息队列:ipcs -q8 q, _; }3 s- G5 F$ \5 E
0 Q5 ?7 o1 |: d9 d#include <sys/types.h>
^0 s/ S* V7 T$ C2 O
, S+ A# D9 v: {& `2 K+ `0 O# I#include <sys/ipc.h>
! J. Z0 w7 X. C3 I/ z) M/ C7 J! E4 K8 Z5 i- f0 @- ^( W
#include <sys/msg.h>8 ^5 @ H* H/ x2 a: \
( v% R, a2 e% E
int msgctl(int msqid, int cmd, struct msqid_ds *buf);# h$ f0 c& a8 G5 d0 `8 z1 p! P
- W! d+ o) Z4 X m/ @0 A: s" O
int msgget(key_t key, int msgflg);
- _3 D, C4 m( z6 u( V7 ?6 O, {& s- C
int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);/ H. o$ {; y, X; H& @
6 j% k+ r9 P5 R! Q$ yint msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);
) V2 |; W3 R0 ?% Z3 x/ j4 W% b Q2 w& q4 [+ ?& }
: M! h8 a0 _7 ?2 o8 }
4 `: _- k5 ~1 mC代码
, L" b- `* m1 j/*msgserver.c*/
' E) E% ]3 U" b$ D& U 7 F0 [; u7 J, l5 @/ q
#include <stdlib.h> $ D! L. U# t! H) \) Q$ M
#include <string.h>
( U o- t" e) Z5 t$ i#include <errno.h> & Y1 x1 ~/ d0 Z7 n, b) o
#include <sys/types.h>
x% e: u) \: W. i#include <sys/ipc.h> ( s( C4 M. j1 V# Z5 h
#include <sys/msg.h>
# q& ]7 z K) C0 ]1 h#include <sys/stat.h>
) K' u; M/ X4 V: s3 Y: W
: c8 V( x( C( s) C5 L" h#define MSG_FILE "msgserver.c"
5 E3 v8 ~: S* T0 r# K#define BUFFER 255
- O2 B) p# m3 n+ ^7 U, e#define PERM S_IRUSR|S_IWUSR
+ Z7 X: t! M$ Y& h1 D2 X/* 服务端创建的消息队列最后没有删除,我们要使用ipcrm命令来删除的 */ ( h/ C1 q( m% p; l
/* ipcrm -q <msqid> */
' N& |+ t9 J6 x0 {7 v9 r
% ~5 _9 C B4 X: X( `* k) H4 [struct msgtype
% l+ D, N1 x7 j: N{ 0 \- y5 k8 G' S$ F
long mtype; 2 }$ ?' H6 R. x; g5 W3 H
char buffer[BUFFER+1];
* |) M" a B# W( p( J};
% D/ o( \; P# {6 C7 ]
4 ^; O1 R' d+ b8 O; P* g2 m% Hint main() * N) x& D `' c2 A
{
- p- ~1 l. A# u: x struct msgtype msg; - \7 a2 ~9 T. j" B
key_t key;
& h+ O9 H2 d. A& K. H. n int msgid;
N" t) i. M; l
! k8 p) P7 I4 K: `+ i; ^8 \2 } if((key=ftok(MSG_FILE,'a'))==-1)
8 |7 O2 p+ T# x {
+ X( T" {: K+ f5 b t L fprintf(stderr,"Creat Key Error:%s\n", strerror(errno));
" ] o: m6 y# J3 }2 z; _ exit(1);
, c q( w+ ^5 h% ] } # _! F$ i& z, k6 I
4 p; F$ l! _, U$ V2 E
if((msgid=msgget(key, PERM|IPC_CREAT|IPC_EXCL))==-1)
- S: e* B# i/ i {
1 ?* `% f& B$ o/ e4 ?: N" a8 V7 C fprintf(stderr, "Creat Message Error:%s\n", strerror(errno));
! W- D5 \% o1 [" T6 a/ r* R6 ~ exit(1); ! w$ v; Z2 z3 |5 z/ W* U
} " |2 I2 E. ?9 E4 d; U
printf("msqid = %d\n", msgid);
7 i( c: m/ B4 N8 U5 }+ s! i! V- t while(1)
, T% Z& L s* a9 I9 g { * k/ ]" Z5 A* a! G% B
msgrcv(msgid, &msg, sizeof(struct msgtype), 1, 0);
, C) j' `% g% U" ^" X) u fprintf(stderr,"Server Receive:%s\n", msg.buffer);
3 ^9 r4 N$ \) c0 } msg.mtype = 2;
& B: T$ E1 f4 z* {, P msgsnd(msgid, &msg, sizeof(struct msgtype), 0);
. _, }' ~1 ?( u- q } " x! J# @3 T2 ^! ^
exit(0);
3 {+ q& c8 E! E0 G3 E: g}
% z9 {1 \: e1 o( p8 |( ZC代码; K, i/ g2 X- ?
/* msgclient.c */ ; Z* x( r% A! B( p; r0 V
1 A' C+ C( U5 [* {) K#include <stdio.h> : G2 _; h$ ]+ O' z8 J. P( W% v
#include <stdlib.h>
+ A% l5 G! x, B N#include <string.h> & r. Y ]# E, d m/ x( ^
#include <errno.h>
" N$ r( m0 V% ]+ T4 E! V#include <sys/types.h> / r8 w! \$ h' L8 `9 ?
#include <sys/ipc.h>
# M) n8 ^' K& S+ S' f# m9 T#include <sys/msg.h>
) t& s0 u/ [9 u* t5 I#include <sys/stat.h>
: D' r" ?8 C0 |$ y ( ]0 W0 U9 j" C
#define MSG_FILE "msgserver.c" / W1 l; e/ F9 T. s7 i
#define BUFFER 255 & ?5 Y: b, O" ~5 R& ?" S' N; R- W" S
#define PERM S_IRUSR|S_IWUSR
[1 ~& w& M" g0 R( U
+ v- A$ ~ e% l$ n" }struct msgtype {
* @: U3 P( ~* o) }4 b& p long mtype;
1 _- k( J' P" @! ] char buffer[BUFFER+1]; 7 G0 }4 M/ m4 e0 _% c" o- F
};
2 N9 F2 M# x) ]1 n , U) b& t) N8 g8 l/ F1 B& }) ~
int main(int argc, char **argv) 0 V8 g" I4 p# l1 S# Z
{
% R5 u5 ~1 w( m4 o# t' ~+ T% G struct msgtype msg; " }# v2 `( @) m# o: v
key_t key; ; V. N' n* I& s" P( _9 j7 N1 s/ ~
int msgid;
& m4 g. B9 A: U, l2 |; I7 d7 V
" m$ ^4 @: ~) x3 L- m if(argc != 2) ! u: x4 C b. [" U$ X( `/ E# B
{
' Y( D1 P1 p/ Y- V: a- O2 \0 M fprintf(stderr,"Usage:%s string\n", argv[0]);
" L8 I7 S. r& t: E exit(1);
, [$ n! E: p( l, l8 p9 q } 2 E9 d* W' r. u/ X' R
+ |9 V3 i; B) G if((key=ftok(MSG_FILE,'a'))==-1) # h3 q: o- w: {# f; r4 V
{ # w7 f b8 Y* q! u/ ]* M
fprintf(stderr,"Creat Key Error:%s\n", strerror(errno)); + _8 M+ g2 T+ D! r* p4 ` _
exit(1);
! f% m9 a- F% g( n7 W6 u' [ } - j/ {" r6 }, X+ N6 U0 L7 L
; b% t- n' n8 V' `8 A
if((msgid=msgget(key, PERM))==-1) - l! H% ^: d# _' h. Y( U
{
8 V6 a- y" O, F) m. I+ u& U fprintf(stderr,"Creat Message Error:%s\n", strerror(errno));
1 b! {0 N- _! `& f7 v- e1 E! Z0 A exit(1); 1 W& L) b2 C. v7 v: w
}
9 J1 a/ v7 x# k h1 ]8 F 0 A) t# L# j2 f- U l$ U
msg.mtype = 1; 9 x8 P9 y1 Z9 @8 {! r6 N: `/ ]
strncpy(msg.buffer, argv[1], BUFFER);
2 A% p6 x: Z. T- U msgsnd(msgid, &msg, sizeof(struct msgtype), 0);
: L4 w& z5 w- i; j memset(&msg, '\0', sizeof(struct msgtype));
1 X: C# f5 Z+ E2 ^1 D msgrcv(msgid, &msg, sizeof(struct msgtype), 2, 0);
* t& p4 I, ~) a! y3 n' I. k fprintf(stderr, "Client receive:%s\n", msg.buffer); 9 V6 m6 |4 p0 O) q' ?9 d7 g$ H- J
exit(0);
( j9 ^+ U1 A3 T1 P8 B- Z/ G} |
|