Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 POSIX IPC 并根据文档 - http://man7.org/linux/man-pages/man3/mq_send.3.html
mq_send() 方法只发送 char* 数据,而 mq_recv() 只接收字符数据。但是,我想向我的 msg 队列发送一个自定义结构,在接收端,我想获取该结构。
示例结构:
struc Req { pid_t pid; char data[4096]; }
那么,有谁知道如何在 C 语言中实现这一点?
您只需要传递结构的地址并将其转换为适当的指针类型: const char * formq_send和 char * for mq_receive。
mq_send
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);