2

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 );
}

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

Thank you!

Billy Pilgrim
  • 1,842
  • 3
  • 22
  • 32

1 Answers1

1

The manpage says:

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

and

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.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • You are reading different man pages than I. mine says: The mq_flags field contains flags associated with the open message queue description. This field is initialized when the queue is created by mq_open(3). The only flag that can appear in this field is O_NONBLOCK. so, yes, partially right -- the mq_flags field. It goes on :The mq_maxmsg and mq_msgsize fields are set when the message queue is created by mq_open(3). The mq_maxmsg field is an upper limit on the number of messages that may be placed on the queue using mq_send(3). The mq_msgsize field is an upper limit on the size of messages – Billy Pilgrim Sep 25 '12 at 19:26
  • What about next two paragraphs? – Michael Krelin - hacker Sep 25 '12 at 20:15
  • Because my manpage contains all the information you posted. This one seems to match the one on my system: http://linux.die.net/man/3/mq_getattr – Michael Krelin - hacker Sep 25 '12 at 20:17
  • Okay, I see that (sneaky of them). However, when the queue is created, I also specify a size smaller than the default. Is it not possible to create a message queue with a msg_size less than 8K? – Billy Pilgrim Sep 25 '12 at 20:43
  • 2
    Well, let me start with saying that I have no idea ;-) But could it be that you already created the queue once and subsequent `mq_open`s are just opening? Can you try to mount `mqueue` filesystem to see if it's there or try to `mq_unlink` it before recreating with a desired size? – Michael Krelin - hacker Sep 25 '12 at 20:48
  • You get the wizard award! After rebooting my box, it worked! Thank you! (Not sure how I award you -- accept your answer? Thanks again! – Billy Pilgrim Sep 25 '12 at 20:57