I’m stuck at start point with my POSIX queue:
what (I think) should be blocking isn’t blocking at all, and the loop keeps spinning.
I am trying to have a process blocked on a mq_receive until some message arrives, but looks like the call always receives an empty message from the queue(but there are not yet any clients sending messages). The queue is correctly opened with the default settings of mq_flags=0.
I am running Ubuntu 12.04.
Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
int main(int argc, char **argv) {
mqd_t qd;
qd = mq_open("/tempqueue", O_RDONLY | O_CREAT, 0666, NULL);
if (qd == (mqd_t) -1){
printf("Problemz");
return 1;
}else{
printf("Coda creata\n");
}
char buf[400];
while(1){
mq_receive(qd, buf, 400, NULL);
printf("Ricevo: %s.\n", buf);
}
mq_close(qd);
mq_unlink("/tempqueue");
}
Check the return value!
It’s almost certainly “-1” … and which point you can call “perror()” or check “errno” for the underlying error: