0.消息队列
1.特点
①.消息队列是System V IPC对象的一种;
②.消息队列是由消息队列ID来唯一表示;
③.消息队列就是一个消息的列表。用户可以在消息队列中添加消息、读取消息等;
④.消息队列可以按照类型来发送/接收消息,不同类型的消息是分别存储的
2.注意
①.通信双方首先要定义好统一的消息格式
②.用户根据应用需求定义结构体类型
③.首成员类型为long,代表消息类型(正整数)
④.其他成员都属于消息正文
3.格式
typedef struct
{
/* 1.首成员必须为long型 */
long mtype; //消息的类型(必须为正整数)
/* 2.之后的成员属于正文 */
char mtext[64];
.
.
.
}MSG;
/* 获得正文的字节大小 */
#define LEN (sizeof(MSG) - sizeof(long))
一.消息队列使用骤
0.生成key值 - ftok()
1.打开/创建消息队列 - msgget
功能:
打开/创建消息队列;
成功时返回(消息队列的id),失败时返回(EOF),并设置errno
参数:
key:和消息队列关联的key、IPC_PRIVATE(设置成私有的)、ftok生成的key
若多个进程要操作同一个消息队列,则key值要一样
msgflg:标志位,一般置为 IPC_CREAT | 0666(消息队列不存在,则创建,存在就打开)
int msgget(key_t key,int msgflg);
int main(void)
{
int msgid;
key_t key;
/* 生成key */
key = ftok(".",'q');
if(-1 == key)
{
perror("ftok");
exit(-1);
}
/* 创建消息队列 */
msgid = msgget(key,IPC_CREATE | 0666);
if(0 > msgid)
{
perror("msgget");
exit(-1);
}
return 0;
}
2.向消息队列发送消息 - msgsnd
功能:
向消息队列发送消息
成功返回(0),失败返回(-1)并设置errno
参数:
msgid:消息队列的id
msgp:消息缓冲区地址,其存放的就是要发送的消息
size:指定要发送的消息的正文长度
msgflg:标志位,用于设置发送方式
0:发送成功了才返回,否则阻塞等待发送完成
IPC_NOWAIT:不用等待发送成功也返回
int msgsnd(int msgid,const void *msgp,size_t size,int msgflg);
typedef struct
{
long mtype;
char mtext[64];
}MSG;
#define LEN (sizeof(MSG) - sizeof(long))
int main(void)
{
int msgid;
key_t key;
MSG buf;
/* 1.定义消息的类型 */
buf.mtype = 100;
fgets(buf.mtext,64,stdin);
/* 2.发送消息 */
msgsnd(msgid,&buf,LEN,0);
return 0;
}
3.从消息队列接收消息 - msgrcv
功能:
从消息队列接收指定类型的消息;
成功时返回(接收到的消息长度),失败时返回(-1);
参数:
msgid:消息队列的id
msgp:消息接收缓冲区
size:指定接收的消息长度
msgtype:指定消息的类型
msgtype > 0:只接收指定的类型的消息
msgtype = 0:接收消息队列中最早的消息
msgtype < 0:按照优先级接收(不常用)
msgflg:标志位
0:进程阻塞,直到有消息,或者消息队列被删除了,或者被信号打断
IPC_NOWAIT:不阻塞等待,没读到消息就返回0。
int msgrcv(int msgid,void *msgp,size_t size,long msgtype,int msgflg);
typedef struct
{
long mtype;
char mtext[64];
}MSG;
#define LEN (sizeof(MSG) - sizeof(long))
int main(void)
{
int msgid;
key_t key;
MSG buf;
/* 指定接收类型为200的消息,接收LEN个字节 */
if(0 > msgrcv(msgid,&buf,LEN,200,0))
{
perror("msgrcv");
exit(-1);
}
return 0;
}
4.控制消息队列 - msgctl
功能:
控制消息队列;
成功时返回(0),失败返回(-1);
参数:
msgid:消息队列的id
cmd:控制指令
IPC_STAT:获取消息队列的属性,用msgid_ds *buf接收
IPC_SET:设置消息队列的属性,先用buf填充再传入
IPC_RMID:立刻删除队列
buf:存放消息队列属性的地址
int msgctl(int msgid,int cmd,struct msgid_ds *buf);
三.代码示例
目标:两个进程通过消息队列轮流将键盘输入的字符串发给对方,接收并打印对方发送的消息。
1.流程图
2.代码
1.msg_clientA
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
/* 1.定义消息的格式 */
typedef struct
{
long mtype; //消息的类型
char mtext[64];
}MSG;
#define LEN (sizeof(MSG) - sizeof(long))
#define TypeA 100
#define TypeB 200
int main(void)
{
int msgid;
key_t key;
MSG buf;
/* 2.生成key */
key = ftok(".",'q');
if(-1 == key)
{
perror("ftok");
exit(-1);
}
/* 3.创建/获取消息队列 */
msgid = msgget(key,IPC_CREAT | 0666);
if(0 > msgid)
{
perror("msgget");
exit(-1);
}
while(1)
{
/* 4.指定发送的类型后发送,类型应指定为对方的类型 */
buf.mtype = TypeB;
printf("input >\n");
fgets(buf.mtext,64,stdin);
msgsnd(msgid,&buf,LEN,0);
if(0 == strcmp(buf.mtext,"quit\n"))
break;
/* 5.指定要接收的类型后接收,类型应指定为自己的消息类型 */
if(0 > msgrcv(msgid,&buf,64,TypeA,0))
{
perror("msgrcv");
exit(-1);
}
if(0 == strcmp(buf.mtext,"quit\n"))
{
msgctl(msgid,IPC_RMID,0);
exit(0);
}
printf("recv from clientB :%s\n",buf.mtext);
}
return 0;
}
2.msg_clientB
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
/* 1.定义消息的格式 */
typedef struct
{
long mtype; //消息的类型
char mtext[64];
}MSG;
#define LEN (sizeof(MSG) - sizeof(long))
#define TypeA 100
#define TypeB 200
int main(void)
{
int msgid;
key_t key;
MSG buf;
/* 2.生成key */
key = ftok(".",'q');
if(-1 == key)
{
perror("ftok");
exit(-1);
}
/* 3.创建/获取消息队列 */
msgid = msgget(key,IPC_CREAT | 0666);
if(0 > msgid)
{
perror("msgget");
exit(-1);
}
while(1)
{
/* 4.指定要接收的类型后接收,类型应指定为自己的消息类型 */
if(0 > msgrcv(msgid,&buf,64,TypeB,0))
{
perror("msgrcv");
exit(-1);
}
/* 7.由最后一个进程删除消息队列 */
if(0 == strcmp(buf.mtext,"quit\n"))
{
msgctl(msgid,IPC_RMID,0);
exit(0);
}
printf("recv from clientA :%s\n",buf.mtext);
/* 5.指定发送的类型后发送,类型应指定为对方的类型 */
buf.mtype = TypeA;
printf("input >\n");
fgets(buf.mtext,64,stdin);
msgsnd(msgid,&buf,LEN,0);
if(0 == strcmp(buf.mtext,"quit\n"))
break;
}
return 0;
}