Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating Posix message queue with attribute get error (invalid argument)

1,226 views
Skip to first unread message

Hill

unread,
Sep 3, 2009, 9:51:00 PM9/3/09
to
Why not Following code always get "Invalid argument" error?

$ ./a.out /tmp.333

Wrong!
: Invalid argument


#include <mqueue.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <iostream>
using namespace std;

#define FILE_MODE S_IRUSR|S_IWUSR

struct mq_attr attr;

int main(int argc, char** argv){
int c, flags;
mqd_t mqd;
flags = O_RDWR|O_CREAT;

if( argc != 2){

printf("usage: mqcreate <name>\n");
return -1;
}

attr.mq_maxmsg = 512;
attr.mq_msgsize = 512;

mqd = mq_open( argv[argc -1 ], flags, FILE_MODE, &attr);

if(mqd < 0){
perror("Wrong!\n");
return -1;
}

mq_close(mqd);
exit(0);
}

red floyd

unread,
Sep 4, 2009, 1:28:33 AM9/4/09
to
Hill wrote:
> Why not Following code always get "Invalid argument" error? Who can
> help me out?

Did you have a C++ language question? Unix syscalls are not part of
the C++ language. Please try comp.unix.programmer.

Replied to there, and followup set to there.

Dmitry V. Krivenok

unread,
Sep 4, 2009, 2:50:35 AM9/4/09
to
Hill wrote:
> Why not Following code always get "Invalid argument" error?
>
> $ ./a.out /tmp.333
>
> Wrong!
> : Invalid argument
>

Hi Hill!

It's common problem when working with POSIX MQ.
Your system disallows you to create MQ containing 512 messages, since
system-wide limit is 10 by default.

You can solve your problem by changing hard limit as follows:


krivenok@olimpico_work 10:44:27 /tmp $ g++ -lrt -Wall mq.cpp
mq.cpp: In function 'int main(int, char**)':
mq.cpp:16: warning: unused variable 'c'
krivenok@olimpico_work 10:44:37 /tmp $ ./a.out /tmp.333
Wrong!
: Invalid argument
krivenok@olimpico_work 10:44:40 /tmp $ sudo sh -c 'echo 512 >
/proc/sys/fs/mqueue/msg_max'
krivenok@olimpico_work 10:44:46 /tmp $ ./a.out /tmp.333
krivenok@olimpico_work 10:44:47 /tmp $


POSIX message queues are well-documented and I suggest you to read
man 2 mq_open and man 7 mq_overview carefully.

Hill

unread,
Sep 4, 2009, 3:18:10 AM9/4/09
to
On Sep 4, 2:50 pm, "Dmitry V. Krivenok" <krive...@orangesystem.ru>
wrote:

Thanks Dmitry very much for the kindly help. Now this issue is fixed.
Following code can work well.

#include <mqueue.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <iostream>
using namespace std;

#define FILE_MODE S_IRUSR|S_IWUSR

struct mq_attr attr;

int main(int argc, char** argv){
int c, flags;
mqd_t mqd;
flags = O_RDWR|O_CREAT;

if( argc != 2){

printf("usage: mqcreate <name>\n");
return -1;
}

attr.mq_flags = O_NONBLOCK;
attr.mq_maxmsg = 5;

0 new messages