简体   繁体   English

使用mmap从共享内存读取-分段错误

[英]Reading From Shared Memory Using mmap - Segmentation Fault

I'm still trying to wrap my head around shared memory. 我仍在努力围绕共享内存。 What I'm trying to accomplish is to have an array of pods. 我要完成的工作是拥有一系列豆荚。 Each pod will also contain an array of keyValue. 每个吊舱还将包含一个keyValue数组。

typedef struct {
   char key[256];
   char value[256];
}keyValue;

typedef struct {
   keyValue **arr;
   int count;
}pod;

int fd;

int main(int argc, char **argv) {
   int kv_store_create(char *name) {
       return shm_open(name, O_CREAT|O_RDWR, S_IRWXU);
   }

   void kv_store_write(char *key1, char *value1) {

      static pod (*str)[28];

      ftruncate(fd, sizeof(str));

      str = (pod(*)[28])mmap(NULL, sizeof(str), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

      for (int i = 0; i < 28; i++) {
         str[i]->arr = (keyValue **)malloc(28 * sizeof(keyValue));
         for(int j = 0; j < 28; j++) {
            str[i]->arr[j] = (keyValue *)malloc(256 * sizeof(keyValue));
         }
       }

       strncpy(str[0]->arr[0]->key, key1, strlen(key1));
       strncpy(str[0]->arr[0]->value, value1, strlen(value1));
       str[0]->count = 1;
    }

   fd = kv_store_create("sharedmem");

   kv_store_write("key", "value");

So at this point, I have a keyValue in a pod, and if I read the shared memory from the same file, I have no issues. 因此,在这一点上,我在pod中有一个keyValue,并且如果我从同一个文件中读取共享内存,则不会有任何问题。

The issue arises when I try to read from another process. 当我尝试从另一个进程中读取时,就会出现问题。 I have the following file 我有以下文件

int main(int argc, char **argv) {

    typedef struct {
        char key[256];
        char value[256];
    }keyValue;

    typedef struct {
        keyValue **arr;
        int count;
    }pod;


    int fd = shm_open("sharedmem", O_RDWR, 0);
    if (fd < 0) {
        printf("Error... opening shm\n");
    }

    struct stat s;

    if (fstat(fd, &s) == -1) {
        printf("Error fstat\n");
    }

    pod (*str2)[28];

    str2 = (pod(*)[28])mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);

    printf("%s", str2[0]->arr[0]->key); 

}

The printf is causing a seg fault and I believe it is trying to access part of a memory that has nothing allocated, while that printf would actually print in my first file. printf导致段错误,我相信它正在尝试访问未分配任何内存的部分,而该printf实际上会在我的第一个文件中打印。

I'm trying to figure out why is spitting out an error and what route should I take to be able to share an array of structs between two processes 我试图弄清楚为什么会吐出一个错误,以及我应该走什么路线才能在两个进程之间共享结构数组

Thanks! 谢谢!

What you say is: 你说的是:

Each pod will also contain an array of keyValue. 每个吊舱还将包含一个keyValue数组。

But that's not the case, because a pod is: 事实并非如此,因为豆荚是:

typedef struct {
   keyValue **arr;
   int count;
}pod;

which doesn't contain an array of KeyValue . 其中不包含KeyValue数组。 It contains a pointer to an array of KeyValue* , but the KeyValue objects themselves are neither in the pod nor in the memory pointed to by arr . 它包含一个指向KeyValue*数组的指针,但是KeyValue对象本身既不在pod也不在由arr指向的内存中。 So they definitely are not in the memory you are sharing between the two processes. 因此,它们绝对不在您在两个进程之间共享的内存中。

So the reading process gets a pod which contains a pointer to an address in the owning process ; 因此,读取过程将获得一个pod ,该pod中包含指向拥有过程中的地址的指针; obviously, that pointer is completely meaningless in the reader since processes do not share memory so having an address of an object in another process is more or less equivalent to having a random number. 显然,该指针在读取器中完全没有意义,因为进程不共享内存,因此在另一个进程中拥有对象的地址或多或少等于具有随机数。

In short, you have to make sure the shared memory region actually contains all of the objects you want to share, not just pointers to them. 简而言之,您必须确保共享内存区域实际上包含您要共享的所有对象,而不仅仅是指向它们的指针。 And since it is unlikely that mmap will return the same address in the two processes, it will not be useful to insert pointers in the shared memory even to objects which are placed in the shared memory. 而且,由于mmap在两个进程中不太可能返回相同的地址,因此即使在共享内存中插入指向插入对象的指针也无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM