Hello,
I tried to compile a posix mq_send and mq_receive application. The msg is sent on the queue successfully, but during the receive part, an error occurs. I have attached the send and receive part of the code, please do suggest any corrections.
mq_send
#include <mqueue.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#define HI_PRIO 31
#define MSG_SIZE 16
//#define MSG "test"
#define MQ_NAME "/msgqueue"
int main(void)
{
mqd_t mqPXId; /* msg queue descriptor */
char *msg = "test";
/* open msg queue; should already exist with default attributes */
if ((mqPXId = mq_open (MQ_NAME, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL)) ==-1)
{
printf ("sendTask: mq_open failed: %s\n", strerror(errno));
return 0;
}
/* try writing to queue */
if (mq_send (mqPXId, msg, MSG_SIZE, HI_PRIO) == -1)
{
printf ("sendTask: mq_send failed\n");
return 0;
}
else
{
printf ("sendTask: mq_send succeeded, msg sent: %s\n",msg);
}
return 0;
}
mq_receive
#include <stdio.h>
#include <mqueue.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#define MSG_SIZE 16
#define MQ_NAME "/msgqueue"
int main(void)
{
mqd_t mqPXId; /* msg queue descriptor */
char *msg; /* msg buffer */
unsigned int prio; /* priority of message */
/* open message queue using default attributes */
if ((mqPXId = mq_open (MQ_NAME, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL))
== -1)
{
printf ("receiveTask: mq_open failed\n");
return 0;
}
/* try reading from queue */
if (mq_receive (mqPXId,(char *)&msg,MSG_SIZE, &prio) == -1)
{
printf ("receiveTask: mq_receive failed: %s\n",strerror(errno));
return 0;
}
else
{
printf ("receiveTask: Msg of priority %d received:\n\t\t%s\n",
prio, msg);
}
return 0;
}