|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
9 x4 @, w: T5 M6 y: k; g8 [- ?消息队列是消息的链接表,包括Posix消息队列system V消息队列。消息队列用于运行于同一台机器上的进程间通信,它
& d' Y. K; v4 x) O% k6 i和管道很相似,有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。消息队列克服了! A- q' `% r% k2 D* z/ @2 |$ w
信号承载信息量少,管道只能承载无格式字节流以及缓冲区大小受限等缺点。 我们可以用流管道或者套接口的方式来取代它。
; L' {9 j; Z$ _6 x; d0 v: f
! l3 F. j6 N# x' U查询系统消息队列:ipcs -q
& z) w: z+ B+ k' F9 \8 l' E
5 \1 |. K! M% C, d#include <sys/types.h>
3 v8 z$ T; B( p2 Q% i
5 u# w% [4 N% I9 k#include <sys/ipc.h>
+ \! y5 O& L; r2 p9 B6 f) z2 b5 w$ o8 D/ L9 |; V B1 W8 F
#include <sys/msg.h>
, m* y0 J( F5 g( n! A- q) Y6 Y
8 U; o4 f- z( Dint msgctl(int msqid, int cmd, struct msqid_ds *buf);: o9 l) ^. z8 f! o
P) ]6 O1 A3 x0 f- ]9 n. i) Nint msgget(key_t key, int msgflg);# h( r' d* o" ^. ?: n9 l F5 v
9 i- h2 D, h+ {' Gint msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);, C+ G' K/ C' l' O" s: [5 _) U
! J; n0 }, e$ Oint msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);
[$ k8 z, I9 T( m
0 ]: E. B* A2 _2 P
# r" W8 w0 }( b& n3 u; t) L, I. k3 h
C代码
& q9 G1 o2 t- U& ~# A8 n/*msgserver.c*/ 8 b* h! G2 ]+ C
; p; T' n1 V; k#include <stdlib.h> ; w8 h3 r @& X& t9 t+ N
#include <string.h>
4 i6 C4 ?$ E6 b6 Z( h! \4 |6 A#include <errno.h> : M1 I9 S6 w0 Q8 H4 S
#include <sys/types.h>
& o5 `) L0 s6 o# {1 H/ _! Y8 E# i#include <sys/ipc.h>
& y" |& H2 u' u9 ^#include <sys/msg.h> 4 c7 E; d" k' Z( y- E
#include <sys/stat.h>
* B" o% H9 } S* q! c# ?! O6 f
$ y5 j8 |4 k! Z- K# U1 u( u#define MSG_FILE "msgserver.c" 4 h. P; ` B, d1 ~/ V
#define BUFFER 255 q: v- b# J+ S1 R
#define PERM S_IRUSR|S_IWUSR , Z: o9 o! t5 p% @
/* 服务端创建的消息队列最后没有删除,我们要使用ipcrm命令来删除的 */ 3 t1 Q$ ^$ Y0 K6 n" S
/* ipcrm -q <msqid> */
# Z4 ^3 N% B& O$ e ( V1 |6 U1 \5 S
struct msgtype 5 T2 S9 B& p( m. L1 H
{
( G5 X4 D* e. Q; {5 E1 G2 R long mtype;
' y! z! Z, P5 @ W: K) J char buffer[BUFFER+1]; # `& j; j+ T- S0 k+ t
};
$ k. K' r: ?% P 3 y7 I/ v/ n) D
int main()
' H& ~9 X C: S! t{
- q$ B3 ~" U! `8 z1 e struct msgtype msg;
: I( ~: _7 u% T/ O( L key_t key; 2 C* [/ x1 E) T6 l
int msgid;
% `* a, e+ z3 ?) I: Q# e % j1 C. X& z8 p: I6 S: w
if((key=ftok(MSG_FILE,'a'))==-1)
: R5 s6 }$ ^; D& k7 J2 o {
7 b" J- W- {2 _0 s% _ fprintf(stderr,"Creat Key Error:%s\n", strerror(errno));
7 @& s. Y* W: V; d1 i exit(1);
! i& p( R/ N, q3 G } + [3 h* B Y+ j# \( Z! G% ]
) [4 v3 N. W" [4 C6 Q, C
if((msgid=msgget(key, PERM|IPC_CREAT|IPC_EXCL))==-1) 5 h8 R& u7 G. E7 u) O$ |
{ % H( F0 Q, v! K0 s: v9 R. e( K7 c- f
fprintf(stderr, "Creat Message Error:%s\n", strerror(errno));
; ^4 w3 n% i; S. I$ [0 o, Y exit(1);
' I, ^$ ]7 T/ ^# j8 L' u } : i6 X% E, p8 R% _
printf("msqid = %d\n", msgid); - s" W# }! _0 t D
while(1) T1 @- ?3 S# s8 m( k. f
{
2 w# H4 i# @- l' y" [5 i msgrcv(msgid, &msg, sizeof(struct msgtype), 1, 0);
. x( W4 }" r' y" F" A fprintf(stderr,"Server Receive:%s\n", msg.buffer); # F- y* T) N1 E% k4 M- h$ u% P8 W
msg.mtype = 2;
' J+ n. K8 ^5 @9 V msgsnd(msgid, &msg, sizeof(struct msgtype), 0); ; l, @, B0 a; ?7 h- V" |4 X
}
/ n, i1 \% N* Z0 e9 ]% }& g1 c exit(0);
; t9 I1 J# O3 R2 K' W1 F% |" N# w} / B! ?# P# _. b# t
C代码/ v, E, H- n0 \1 a4 R. y
/* msgclient.c */ ( m; w6 {* t6 q& N- V, O- C
- b* ?5 `0 ~- |9 ^/ s1 }' b#include <stdio.h>
% L5 m) \6 W: B2 k2 k#include <stdlib.h>
3 b9 C; S# B9 M! X9 @6 y* g4 T$ o#include <string.h>
, u( _/ h+ r3 T4 D) {6 Y- q$ ]! ]#include <errno.h>
. P$ N `+ Q# x9 X: e# J- A#include <sys/types.h> ! ^% S% W) |( M+ t
#include <sys/ipc.h> , _( u, U1 ^. d y+ J8 c
#include <sys/msg.h>
7 e' ~8 I0 W' ?: v5 A9 \#include <sys/stat.h>
+ X) y+ I& C+ n l) [% \+ k 5 ^! a( a$ @- G6 ?
#define MSG_FILE "msgserver.c"
4 l3 K, n; i$ {" N1 b, h- ^9 p8 w#define BUFFER 255
/ V, m6 S$ j, k3 V( w#define PERM S_IRUSR|S_IWUSR ) I: K0 O9 P: f% l' X: z
+ }$ L7 i2 P- r g2 f; t* V6 I
struct msgtype { - Z7 ~$ b9 j/ a8 x
long mtype;
+ c7 x* a+ v1 [4 O) V" n char buffer[BUFFER+1];
% v& h8 {8 x# h! Z1 v};
! g& V# y- N& a , l& o8 r! t% ^2 F
int main(int argc, char **argv) . r9 y B! N1 H
{
4 t' R, P8 c j4 c6 G+ T4 M struct msgtype msg;
: [" ^7 ~9 ^+ f key_t key; 8 ?; k/ c- n! U6 _7 _* S" d
int msgid;
- |4 s$ G) J7 C( i/ ~% c! I
% I6 {& Q9 u+ G z7 v8 b1 q if(argc != 2)
3 M; _3 p! s2 y( ~2 r0 c { 6 z8 q" A) c- r6 [: }
fprintf(stderr,"Usage:%s string\n", argv[0]); 2 ^; R8 D+ m O1 M6 n
exit(1); # d& g1 ^2 F ?* x
}
6 Q( F% C0 \& S( Y4 p2 s
7 a9 E1 @' n+ W if((key=ftok(MSG_FILE,'a'))==-1) * C$ [2 S/ s; H* b' d( x6 j( |( N) q; |
{
% t* B3 _5 [) \0 J9 v fprintf(stderr,"Creat Key Error:%s\n", strerror(errno)); + D, B, z5 m# ^! S8 b% Z9 S7 ]9 N
exit(1);
# O* O# p9 J4 S4 _( G$ m7 B } * \$ z+ X" X2 F0 m( _+ ~
' {0 B: X& Q0 h1 {$ j7 M, i/ @5 a
if((msgid=msgget(key, PERM))==-1) 8 \( {# K* g! D
{ " K3 ~% z; m: b, U# J, R
fprintf(stderr,"Creat Message Error:%s\n", strerror(errno)); 6 ` B( `/ U2 Y2 F
exit(1); ) G8 Q: V1 w [
} $ E# b) \; Q# E, R3 X/ u) m; @4 g
( o( k$ w2 h R) I3 f5 D
msg.mtype = 1;
0 L. m% Z: m! y strncpy(msg.buffer, argv[1], BUFFER);
N8 a8 A- ~/ ^ f" X msgsnd(msgid, &msg, sizeof(struct msgtype), 0); 9 ^4 H& Y* f4 a% D1 X
memset(&msg, '\0', sizeof(struct msgtype));
) K# p! z; u0 _) ^ msgrcv(msgid, &msg, sizeof(struct msgtype), 2, 0);
6 J9 w7 b- J& ^+ c fprintf(stderr, "Client receive:%s\n", msg.buffer); . B( t$ y/ I& ?
exit(0);
5 k/ _! P7 x4 r; {2 {} |
|