在Posix Message Queue中发送和接收结构

时间:2019-03-31 03:03:33

标签: c linux mq

我遇到异常: file1.c:mq_send:消息太长

该错误似乎很容易解释,但我无法弄清楚。

我有结构和文件:

typedef struct {
char path[5011];
char sharedMemSegmentNameBuf[30];
} messageQueueStruct;

file1.c

#include <sys/mman.h>

#define MAX_CACHE_REQUEST_LEN 5041
    messageQueueStruct mqStruct;
    int maxPathLength = MAX_CACHE_REQUEST_LEN - sizeof(mqStruct.sharedMemSegmentNameBuf);
strncpy(mqStruct.path, path, maxPathLength);
strncpy(mqStruct.sharedMemSegmentNameBuf, sharedMemSegmentNameBuf, sizeof(mqStruct.sharedMemSegmentNameBuf));
if (mq_send(qdCache, (const char *) &mqStruct, sizeof(mqStruct), 0) == -1)
{
    perror("handle_with_cache: mq_send");
    exit(1);
}

file2.c

    #include <sys/mman.h>

    #define MAX_CACHE_REQUEST_LEN 5041
        struct mq_attr attributes;
        attributes.mq_maxmsg = MAX_MESSAGES;
        attributes.mq_msgsize = MAX_CACHE_REQUEST_LEN;

       messageQueueStruct receiveStruct;
       int maxPathLength = MAX_CACHE_REQUEST_LEN - sizeof(receiveStruct.sharedMemSegmentNameBuf);

    messageQueueStruct receiveStruct;
    if (mq_receive(qdCache, (char *) &receiveStruct, MAX_CACHE_REQUEST_LEN, NULL) == -1)
    {
        perror("process request: mq_receive");
        exit(1);
    }

在我看来,尺寸在接收/发送侧排列整齐。我也仅使用char []缓冲区进行了测试,并且可以正常工作。 我看过的一些参考文献:

send struct in mq_send

https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/cs361/html/MessPassMQ.html

0 个答案:

没有答案