消息队列不像共享内存的流程,它不需要信号通知。
消息队列的另一个特点:信息读取完自动删除;可以阻塞
msgget函数: 消息队列创建
头文件: #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
函数原型: int msgget(key_t key, int flag);
参数: key,和消息队列关联的key值
flag,消息队列的访问权
返回: 成功,消息队列ID
出错:-1
例: msgid=msgget(IPC_PRIVATE,0777);
msgctl函数: 消息队列控制
头文件: #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
原型: int msgctl(int msgqid, int cmd, struct msgqid_ds *buf)
参数: msgid,消息队列ID
cmd,IPC_STAT,读取消息队列的属性,并将其保存在buf指向的缓冲区
IPC_SET,设置消息队列属性,这个值取自buf参数
IPC_RMID,从系统中删除消息队列
buf,消息队列缓冲区
返回: 成功0,出错-1
例: msgctl(msgid,IPC_RMID,NULL);
msgsnd函数: 发送消息队列
头文件: #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
函数原型: int msgsnd(int msqid, const void *msgp, size_t size, int flag)
参数: msqid,消息队列ID
msgp,指向消息的指针
常用消息结构msgbuf如下:
struct msgbuf{
long mtype; //消息类型
car mtext[N]; //消息正文
}
size,发送的消息正文字节数
flag,IPC_NOWAIT,消息没有发送完函数也会立即返回
0,直到发送完函数才返回
返回:成功0,出错-1
例: msgsnd(msgid,(void *)&sendbuf,strlen(sendbuf.voltage),0);
msgrcv函数: 接收消息
头文件: #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
函数原型: int msgrcv(int msgid, void *msgp, size_t size, long msgtype, int flag)
参数: msgid,消息队列号
msgp,接收消息的缓冲区
size,要接收的消息字节数
msgtype,0:接收消息队列中第一个消息
>0,接收消息队列中第一个类型为msgtype的消息
<0,接收消息队列中类型值不大于msgtype的绝对值切类型值又最小的消息
flag,0:若无消息函数一直阻塞
IPC_NOWAIT,若没消息,进程立即返回ENOMSG
返回: 成功,接收到的消息长度
出错-1
例: msgrcv(msgid,(void *)&recvbuf,124,100,0);
Server.c
#include "sys/types.h"
#include "sys/msg.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct msgbuf
{
long type;
char voltage[124];
char ID[4];
};
int main()
{
int msgid;
int readret;
int key;
int pid;
struct msgbuf sendbuf,recvbuf;
key=ftok("./a.c",'a');
if(key < 0)
{
printf("creat key failure\n");
return -2;
}
msgid=msgget(key,IPC_CREAT|0777);
if(msgid <0)
{
printf("creat message queue failure\n");
return -1;
}
printf("creat message queue sucess msgid=%d\n",msgid);
system("ipcs -q");
pid=fork();
if(pid > 0) //parent process write 100
{
sendbuf.type=100;
//write message queue
while(1)
{
memset(sendbuf.voltage,0,124);//clear send buffer
printf("please input message:\n");
fgets(sendbuf.voltage,124,stdin);
//start write message to message queue
msgsnd(msgid,(void *)&sendbuf,strlen(sendbuf.voltage),0);
}
}
if(pid == 0)//child process read 200
{
while(1)
{
memset(recvbuf.voltage,0,124);
msgrcv(msgid,(void *)&recvbuf,124,200,0);
printf("receive message from message queue:%s",recvbuf.voltage);
}
}
msgctl(msgid,IPC_RMID,NULL);
system("ipcs -q");
return 0;
}
运行结果
creat message queue sucess msgid=65537
------ Message Queues --------
key msqid owner perms used-bytes messages
0x00000000 32768 alex 777 0 0
0x61016518 65537 alex 777 0 0
please input message:
receive message from message queue:hello
hi
Client.c
#include "sys/types.h"
#include "sys/msg.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct msgbuf
{
long type;
char voltage[124];
char ID[4];
};
int main()
{
int msgid;
int readret;
int key;
int pid;
struct msgbuf sendbuf,recvbuf;
key=ftok("./a.c",'a');
if(key < 0)
{
printf("creat key failure\n");
return -2;
}
msgid=msgget(key,IPC_CREAT|0777);
if(msgid <0)
{
printf("creat message queue failure\n");
return -1;
}
printf("creat message queue sucess msgid=%d\n",msgid);
system("ipcs -q");
pid=fork();
if(pid == 0) //child process write 200
{
sendbuf.type=200;
//write message queue
while(1)
{
memset(sendbuf.voltage,0,124);//clear send buffer
printf("please input message:\n");
fgets(sendbuf.voltage,124,stdin);
//start write message to message queue
msgsnd(msgid,(void *)&sendbuf,strlen(sendbuf.voltage),0);
}
}
if(pid > 0)//parent process read 100
{
while(1)
{
memset(recvbuf.voltage,0,124);
msgrcv(msgid,(void *)&recvbuf,124,100,0);
printf("receive message from message queue:%s",recvbuf.voltage);
}
}
msgctl(msgid,IPC_RMID,NULL);
system("ipcs -q");
return 0;
}
运行结果
creat message queue sucess msgid=65537
------ Message Queues --------
key msqid owner perms used-bytes messages
0x00000000 32768 alex 777 0 0
0x61016518 65537 alex 777 0 0
please input message:
hello
please input message:
receive message from message queue:hi