2
我在Linux中使用IPC的消息队列(类似于this后)编写了一个简单的C程序。为了简单起见,在相同的过程中调用mq_send
和mq_receive
。通过消息队列发送可变大小缓冲区
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <mqueue.h>
#define QUEUE_NAME "/test_queue2"
int main(int argc, char **argv)
{
/* initialize the queue attributes */
struct mq_attr attr;
attr.mq_flags = 0;
attr.mq_maxmsg = 10;
attr.mq_msgsize = 30;
attr.mq_curmsgs = 0;
/* create the message queue */
mqd_t mq = mq_open(QUEUE_NAME, O_CREAT | O_WRONLY, 0644, &attr);
if (mq < 0) {
printf("error in mq_open 1");
exit(1);
}
/* send the message */
int rc = mq_send(mq, "mani123", 8, 0); // need to include the null character too!
if (rc < 0) {
printf("error in mq_send");
exit(1);
}
// ---------------------------------------------------------------
mqd_t mq2 = mq_open(QUEUE_NAME, O_RDONLY);
if (mq2 < 0) {
printf("error in mq_open 2: %s", strerror(errno));
exit(1);
}
char rcvmsg[50];
rc = mq_receive(mq2, rcvmsg, 50, 0);
if (rc < 0) {
printf("error in mq_receive");
exit(1);
}
printf("%s", rcvmsg);
return 0;
}
我使用消息队列发送/接收常量字符串。现在我想重复这个通用缓冲区(char数组)。我使用mq_send
发送缓冲区内容,但我的问题是mq_receive
如何获得发送缓冲区的确切大小?我应该分别发送缓冲区大小吗?
@ManiAm:这个答案符合你的需求吗?您还有其它问题么? – chqrlie
感谢您的回答。我同意发送字符串时,不需要发送大小。但是在一个通用缓冲区(由uint8_t组成)中,mq_receive如何知道缓冲区的大小是多少? – ManiAm
@ManiAm:'mq_send'将消息长度存储在消息队列结构中,'mq_receive'从那里获取消息长度。发送一个字符串并不重要,'mq_send'不会对消息的内容做任何假设,对于任何类型的内容(无论是“char”,“uint8_t”还是其他任何内容)都是如此。你甚至可以传递一个结构的地址,只要它不包含任何指针,因为指针值对接收端来说是没有意义的。 – chqrlie