Question

I have written this code to make a posix message queue. But I am receiving an error "Function not implemented".

Q1. Is it a platform related issue ? [Am using Ubuntu 10.10] I read somewhere that I need to rebuild my kernel to enable message queues !?

Q2. I also read something about starting the mqueue server before actually using message queues ?

Can someone please explain..

#include <mqueue.h>     /* message queue stuff */
#include <iostream>
#include <unistd.h>     /* for getopt() */
#include <errno.h>      /* errno and perror */
#include <fcntl.h>      /* O_flags */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(int argc, char **argv)
{

mqd_t msgQueueDescriptor;
mq_attr attr;

char Msg[]="msg";

attr.mq_maxmsg = 10;
attr.mq_msgsize = sizeof(Msg);
attr.mq_flags = 0;

msgQueueDescriptor = mq_open("/myQueue", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH , attr );
cout << msgQueueDescriptor << " " << errno << " " << strerror(errno);
mq_close(msgQueueDescriptor);

return 0;
}
Was it helpful?

Solution

I think I have realized what the problem was, or rather a mistake from side.

This is what I read from here -

[ In reference to mq_open( ) ]

Returns: A valid message queue descriptor if the queue is successfully created, or -1 (errno is set).

So, I should be checking the value for errno only when an error actually occurs!. But in the above code I am just printing the value irrespective of an error occured or not, and thus it is printing the error message corresponding to some garbage value stored in errno.

So my code should be something like this -

if ((msgQueueDescriptor = mq_open("/myQueue", O_RDWR|O_CREAT, 0664 ,NULL ) == -1))
{
    cout << msgQueueDescriptor << " " << errno << " " << strerror(errno);
}
else
{
   cout << "All is well" ;
} 

Did I just made a fool outa myself :p

PS: As far as message queues being enabled on Ubuntu 10.10 is concerned, I checked the flags as mentioned by "n.m.", they are very much enabled, and I am able to use the message queues now. Thanks all of you - larsmans, VJovic, n.m., Joachim Pileborg, jørgensen.

About my second question

Q2. I also read something about starting the mqueue server before actually using message queues ?

I think that is a requirement specifically on QNX.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top