C mq_receive() with struct 在尝试访问时导致段错误

C mq_receive() with struct causes seg fault when trying to access

所以我有两个连接到消息队列的程序,一个以结构的形式向另一个发送消息。但是,当我在收到该结构后尝试访问该结构时,出现了分段错误。

我不知道我需要做什么才能在发送后访问该结构。

这是我的发件人代码:

#include <stdio.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

typedef struct {
  char path[2048];
  char shm_name[50];
  size_t shm_s;
  char sem_send_name[50];
    char sem_recv_name[50];
} cache_request;

static void showAttr(mqd_t fd)
{
    struct mq_attr attr;
    mq_getattr(fd, &attr);
    printf("maxmsg = %ld\n", attr.mq_maxmsg);
    printf("msgsize = %ld\n", attr.mq_msgsize);
    printf("curmsgs = %ld\n", attr.mq_curmsgs);
}

int main()
{
    mqd_t fd;
    int ret;

    struct mq_attr attr;
    int flags = O_RDWR | O_CREAT;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 3;
    attr.mq_msgsize = 2216;
    attr.mq_curmsgs = 0;
    fd = mq_open("/mq", flags,(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),&attr );
    if (fd < 0) {
        printf("open failed %d\n", fd);
        exit(EXIT_FAILURE);
    }
    printf("open ok\n");
    sleep(1);
    showAttr(fd);
    cache_request* msg = (cache_request*)malloc(sizeof(cache_request));
    strcpy(msg->path,"ok\n");
    strcpy(msg->shm_name, "ex1\n");
    msg->shm_s = 250;
    strcpy(msg->sem_send_name," ex2");
    strcpy(msg->sem_recv_name, "ex3");
    printf("hmm %s\n", msg->shm_name);
    printf("hmm %ld\n", msg->shm_s);
    //res = mq_receive(fd, (char*) &msg, sizeof(cache_request), NULL);
    int res = mq_send(fd, (const char*) &msg, sizeof(cache_request), 0);
    if (res < 0) {
          printf ("   Error %d (%s) on server mq_send.\n",
              errno, strerror (errno));
          mq_close(fd);
          mq_unlink("/mq");
          exit (1);
      }
    sleep(10);
    ret = mq_close(fd);
    if (ret != 0) {
        printf("open failed\n");
        exit(EXIT_FAILURE);
    }
    printf("close ok\n");
    sleep(20);
    mq_unlink("/mq");
    return 0;
}

这是接收者:

#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
#include <errno.h>
#include <unistd.h>

/*
gcc [file] -lrt
*/

typedef struct {
  char path[2048];
  char shm_name[50];
  size_t shm_s;
  char sem_send_name[50];
    char sem_recv_name[50];
} cache_request;


static void showAttr(mqd_t fd)
{
    struct mq_attr attr;

    mq_getattr(fd, &attr);

    printf("maxmsg = %ld\n", attr.mq_maxmsg);
    printf("msgsize = %ld\n", attr.mq_msgsize);
    printf("curmsgs = %ld\n", attr.mq_curmsgs);

}

int main()
{
    mqd_t fd;
    int ret;




    mq_unlink("/mq");
    struct mq_attr attr;
    int flags = O_RDWR;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 3; // ***
    attr.mq_msgsize = 141;
    attr.mq_curmsgs = 0;

    while((fd = mq_open("/mq", O_RDWR)) == -1){
      printf("Couldnt connect to message queue in cache\n");
      sleep(2);
    }

    if (fd < 0) {
        printf("open failed %d\n", fd);
        exit(EXIT_FAILURE);
    }
    printf("open ok\n");

    //sleep(5);

    showAttr(fd);
    char* rsp_msg = (char*)malloc(2216);
    int res = mq_receive(fd, (char*) &rsp_msg, 2216, NULL);
    printf("recieved: %d\n", res);
        printf("should be %ld\n", sizeof(cache_request));
    if (res < 0) {
          printf ("   Error %d (%s) on server mq_receive.\n",
              errno, strerror (errno));
          mq_close(fd);
          mq_unlink("/mq");
          exit (1);
      }
    cache_request* msg = (cache_request*)rsp_msg;
    printf("shm_s: %ld\n", msg->shm_s); //THIS is where the seg fault happens
        printf("shm_name: %s\n", msg->shm_name);
    ret = mq_close(fd);
    if (ret != 0) {
        printf("close failed\n");
        exit(EXIT_FAILURE);
    }
    printf("close ok\n");

    return 0;
}

int res = mq_send(fd, (const char*) &msg, sizeof(cache_request), 0);

应该是msg,而不是&msg。您要发送 msg 指向的数据,而不是指针本身。

int res = mq_receive(fd, (char*) &rsp_msg, 2216, NULL);

同样。

此外,最好使用 sizeof(cache_request) 而不是硬编码 2216