katwalatapan 0 Light Poster

Hello,

Could anyone please explain a reason why data could not be received from a POSIX message queue using mq_receive(), called from a different file. Following is my code for a test that i'm running to try and transfer data from one file to another.

main.c

#include "recfun.h"


int main()
{
		
		datastruct *data;
		char *qname="/queue";
		qdes=mq_open(qname, O_RDWR|O_CREAT|O_NONBLOCK, 0777, NULL);
		if(qdes==-1)
		{
			printf("Cannot open queue\n");
		}
		data=(datastruct*)malloc(sizeof(datastruct));
		data->i=3;
		data->c="3 is the data";
		printf("data->i=%d\n", data->i);
		printf("data->c=%s\n", data->c);
		if(mq_send(qdes, (char *)data, 128, 0)==-1)
		{
			printf("Cannot send data onto the queue\n");
		}
		recv_fun(qdes);
		free(data);
		mq_close(qdes);
		mq_unlink(qname);

		return (0);
}

recfun.h

#include <stdio.h>
#include <stdlib.h>
#include <mqueue.h>
#include <string.h>

typedef struct
{
    int i;
    char *c;
}datastruct;
static mqd_t qdes;
void recv_fun(mqd_t qdes);

recfun.c

#include "recfun.h"

void recv_fun(mqd_t qdes)
{
    datastruct *data1;
    data1=(datastruct *)malloc(sizeof(datastruct));
    if(mq_receive(qdes, (char *)&data1, 128, 0)==-1)
    {
        printf("Cannot receive data from queue\n");
    }
    printf("data1->i=%d\n", data1->i);
    printf("data1->c=%s\n", data1->c);
    free(data1);
}

Please do let me know if there could be any reason why I receive "Message too long" error from mq_receive, even when I have allocated sufficient memory.

Thank you.

Regards,

Tapan.