linux mq_open忽略mq_msgsize属性 [英] linux mq_open ignores mq_msgsize attribute

查看:434
本文介绍了linux mq_open忽略mq_msgsize属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,以前以为我很理智,现在不太确定.
我正在尝试创建一个消息队列,该消息队列的mq_msgsize属性为8192以外的其他属性,这似乎是默认设置.我在下面附加了我的代码-它具有许多显示值的printf.如果您能指出我做错了什么,我将永远感激不已.

All, used to think that I was sane, now not so sure.
I am trying to create a message queue whose mq_msgsize attribute is OTHER than 8192, which seems to be the default. I have attached my code below -- it has a number of printf's showing the value. If you can point out what I doing wrong, I will be eternally grateful.

bool Subscriber::Subscribe( void )
{
   mqd_t  qid;
   bool   brv = false;
   msg_topic_t topic = this->GetTopic();
   struct mq_attr q_attr;
   int rv = 0;

   if (VALID_TOPIC( topic ))
   {
        if (this->GetName().length() > 0)
        {
            string qnamestr = this->GetName();
            if (qnamestr[0] != '/')
            {   
                qnamestr = "/" + qnamestr; 
                this->SetName(qnamestr);
            }
            const char * qname = (const char *) qnamestr.c_str();

            q_attr.mq_msgsize = 256;
            q_attr.mq_curmsgs = 0;
            q_attr.mq_flags = O_NONBLOCK;
            q_attr.mq_maxmsg = 10;

            qid = mq_open( qname, O_RDONLY|O_CREAT, 0644, &q_attr );
            if ((mqd_t) -1 != qid)
            {   
                rv = mq_getattr(qid, &q_attr );
                if (rv != 0)
                {   perror(" get_attr1 failed: "); }
                printf(" queue size is now: %d\n", q_attr.mq_msgsize);
                if (q_attr.mq_msgsize > 1024)
                {
                    struct mq_attr old_attr;

                    q_attr.mq_msgsize = 1024;
                    rv = mq_setattr( qid, &q_attr, &old_attr);
                    if (rv != 0)
                    {   perror(" could not update message size: "); }
                    rv = mq_getattr(qid, &q_attr );
                    if (rv != 0)
                    {   perror(" get_attr2 failed: "); }
                    printf(" queue size is now: %d\n", q_attr.mq_msgsize);
                }
                this->SetOutboxID( qid );
                brv = true; 
                DLOG(INFO) << " qid = " << qid << endl;
                MessageCenter * mc = MessageCenter::GetInstance();
                mc->AddSubscriber( (Subscriber *) this );
            }
        }
    }
   drain_queue( this->GetOutboxID());
    return( brv );
}

输出看起来像这样: 队列大小现在是:8192 队列大小现在是:8192 队列大小现在为:8192

The output looks like this: queue size is now: 8192 queue size is now: 8192 queue size is now: 8192

谢谢!

推荐答案

该联机帮助页说:

消息时设置mq_maxmsg和mq_msgsize字段 队列由mq_open(3)创建

The mq_maxmsg and mq_msgsize fields are set when the message queue is created by mq_open(3)

唯一可以修改的属性是 mq_flags中的O_NONBLOCK标志. newattr中的其他字段是 忽略了.

The only attribute that can be modified is the setting of the O_NONBLOCK flag in mq_flags. The other fields in newattr are ignored.

这篇关于linux mq_open忽略mq_msgsize属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆