Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send struct in mq_send

I am using POSIX IPC and according to the documentation - http://man7.org/linux/man-pages/man3/mq_send.3.html

mq_send() method only sends char* data and mq_recv() recieves only character data. However, I want to send a custom struct to my msg queue and on the receiving end, I want to get the struct.

sample struct:

struc Req
{
  pid_t pid;
  char data[4096];
}

So, does anyone know how to accomplish this in C lang?

like image 970
Vinit Sharma Avatar asked Apr 13 '14 16:04

Vinit Sharma


1 Answers

You just need to pass the address of the struct and cast it to the appropriate pointer type: const char * for mq_send and char * for mq_receive.

typedef struct Req
{
  pid_t pid;
  char data[4096];
} Req;

Req buf;

n = mq_receive(mqdes0, (char *) &buf, sizeof(buf), NULL);

mq_send(mqdes1, (const char *) &buf, sizeof(buf), 0);
like image 82
Duck Avatar answered Sep 21 '22 11:09

Duck