消息队列通过名字字面意思理解就是队列排队-和平常超市买东西排队付款一样结构,消息队列与FIFO很相似,都是一个队列结构,都可以有多个进程往队列里面写信息,多个进程从队列中读取信息。但FIFO需要读、写的两端事先都打开,才能够开始信息传递工作。而消息队列可以事先往队列中写信息,需要时再打开读取信息。
msgget创建消息队列
int msgget(key_t key, int msgflg);
brief:创建消息列表,创建的消息列表在Linux内核,比管道好,因为是全双工,消息队列的每条消息,退出程序后依然存在(读走就没)msgctl来删除对应的id号
param:key,一个健值,如果另一个进程想访问这个消息队列,就必须通过这个key来访问,一般用ftok来获取key,msgflg,给消息队列的权限
key配合文件节点的索引值,加上一个0-255的数生成一个数,这个数就可以作为msgget的id,ls -i(查看文件夹的索引值)
return:创建成功返回消息队列标识符,创建失败返回-1
msgsnd/msgrcv收发消息
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
brief:往消息队列发消息
param:msgqid,创建进程得到的id号,msgp,把msgp的内容发给消息列表,msgsz,发多少个字节,msgflg,=0,当消息队列满了时,会堵塞,发不进去
return:成功=0,失败-1
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
brief:把消息队列的消息读出来
param:msgqid,创建进程得到的id号,msgp,把消息列表的内容读到msgp,msgsz,读多少个字节,msgtyp,即你要读那条消息,因为magget通过key创建一个消息列表,就会产生一个id,但是Linux内核有很多条消息队列,且每条消息队列是由链表组成,你要通过msgtyp你设置的消息号,根据消息号来读取对应的消息,msgflg,=0,没读到消息就会堵塞
return:成功=0,失败-1
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
struct mybuf
{
long mtype;//必须先发消息号,再发数据
char buf[128];
};
void main()
{
pid_t msgpid;
struct mybuf sendbuf = {888,"Li jian hua"};
int key;
struct mybuf readbuf;
key = ftok(".",1);
msgpid = msgget(key,IPC_CREAT|0600);
if(msgpid == -1)
{
printf("create lose!");
exit(-1);
}
printf("msgid:%d\n",msgpid);
msgsnd(msgpid,&sendbuf,strlen(sendbuf.buf),0);
msgrcv(msgpid,&readbuf,sizeof(readbuf.buf),988,0);
printf("readbuf:%s\n",readbuf.buf);
}
--------------------------------------------------------------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
struct mybuf
{
long mtype;
char buf[128];
};
void main()
{
pid_t msgpid;
struct mybuf sendbuf = {988,"Li zhen"};
struct mybuf readbuf;
int key;
key = ftok(".",1);
msgpid = msgget(key,IPC_CREAT|0600);
if(msgpid == -1)
{
printf("create lose!");
exit(-1);
}
printf("msgid:%d\n",msgpid);
msgrcv(msgpid,&readbuf,sizeof(readbuf.buf),888,0);
printf("readbuf:%s\n",readbuf.buf);
msgsnd(msgpid,&sendbuf,strlen(sendbuf.buf),0);
}
msgctl删除消息列表id
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
brief:删除消息队列的id
param:msgqid,即消息列表的id,cmd,用IPC_RMID,进行删除,buf,不用写NULL
return:成功0,error-1
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
void main()
{
int i;
for(i = 0;i < 8;++i )
{
msgctl(i,IPC_RMID,NULL);//这里因为我有8个要删除的id
}
}
输出结果:
删除0-7的消息队列id