send struct in mq_send

后端 未结 2 1525
既然无缘
既然无缘 2020-12-10 14:18

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() re

相关标签:
2条回答
  • 2020-12-10 15:02

    You can use memcpy as follows:

    char * data; //Do appropriate allocation.
    
    memcpy(data, &req, sizeof(req)));
    

    On receiving you copy back received data into struct.

    memcpy(&rec, data, sizeof(rec)));
    
    0 讨论(0)
  • 2020-12-10 15:12

    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);
    
    0 讨论(0)
提交回复
热议问题