c++之linuxIPC(六)--消息队列(posix)

注意

名字必须为根目录

如果权限问题:error:: Permission denied

sudo mkdir /dev/mqueue  
$ sudo mount -t mqueue none /dev/mqueue

测试代码

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <mqueue.h>
#include <string.h>
#include <iostream>
#include <string>
#define myreturn(msga) {printf("%s\n",msga);return -1;}

int main(int argc,char**argv)
{
    if(argc<2)myreturn("argc<2");

    char*mq_name="/mymq123";
    mqd_t mq_id=mq_open(mq_name,O_CREAT|O_RDWR,0666,NULL);
    if(mq_id<=0)perror("mq_open error ");

    mq_attr mqAttr;
    mq_getattr(mq_id,&mqAttr);
    //mq_setattr(mq_id,&mqAttr,NULL);

    std::cout<<"mq_flags:"<<mqAttr.mq_flags<<std::endl;
    std::cout<<"mq_maxmsg:"<<mqAttr.mq_maxmsg<<std::endl;
    std::cout<<"mq_msgsize:"<<mqAttr.mq_msgsize<<std::endl;
    std::cout<<"mq_curmsgs:"<<mqAttr.mq_curmsgs<<std::endl;


    if(strcmp("-send",argv[1])==0)
    {
        printf("this is mq send mode\n");
        std::string base_str="test index ";
        for(int i=0;i<10;i++)
        {
            std::string send_str=base_str+std::to_string(i)+" for mq";
            int ret= mq_send(mq_id,send_str.c_str(),send_str.length(),0);
            printf("send msg:%s,send len:%d,ret:%d\n",send_str.c_str(),send_str.length(),ret);
            if(ret<0)perror("error");
            usleep(500);
        }
        printf("send over\n");

    } else if(strcmp("-recv",argv[1])==0)
    {
        int error_inddex=0;
        printf("this is mq recv mode\n");
        int buff_len=mqAttr.mq_msgsize;
        char *buff=new char[buff_len];
        while (1)
        {
            memset(buff,0,buff_len);
            size_t len_buff=32;
            ssize_t len=mq_receive(mq_id,buff,buff_len,NULL);
            printf("recv len:%d,recv msg: %s\n",len,buff);
            if(len<0)perror("error:");


            if(len<=0)
            {
                error_inddex++;
            }else
            {
                error_inddex=0;
            }

            if(error_inddex>15)break;

            usleep(500);
        }
        printf("recv over\n");
    }else
    {
        myreturn("mode error");
    }

    mq_close(mq_id);
    //mq_unlink(mq_name);
    return 0;
}

测试结果

 ./a.out -recv
mq_flags:0
mq_maxmsg:10
mq_msgsize:8192
mq_curmsgs:0
this is mq recv mode
recv len:19,recv msg: test index 0 for mq
recv len:19,recv msg: test index 1 for mq
recv len:19,recv msg: test index 2 for mq
recv len:19,recv msg: test index 3 for mq
recv len:19,recv msg: test index 4 for mq
recv len:19,recv msg: test index 5 for mq
recv len:19,recv msg: test index 6 for mq
recv len:19,recv msg: test index 7 for mq
recv len:19,recv msg: test index 8 for mq
recv len:19,recv msg: test index 9 for mq
^C


./a.out -send
mq_flags:0
mq_maxmsg:10
mq_msgsize:8192
mq_curmsgs:0
this is mq send mode
send msg:test index 0 for mq,send len:19,ret:0
send msg:test index 1 for mq,send len:19,ret:0
send msg:test index 2 for mq,send len:19,ret:0
send msg:test index 3 for mq,send len:19,ret:0
send msg:test index 4 for mq,send len:19,ret:0
send msg:test index 5 for mq,send len:19,ret:0
send msg:test index 6 for mq,send len:19,ret:0
send msg:test index 7 for mq,send len:19,ret:0
send msg:test index 8 for mq,send len:19,ret:0
send msg:test index 9 for mq,send len:19,ret:0
send over

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值