Linux进程间通信--消息队列

消息队列通过名字字面意思理解就是队列排队-和平常超市买东西排队付款一样结构,消息队列与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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No Iverson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值