$ ./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);
}
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.
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.
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;