简体   繁体   English

如何在共享内存中的矩阵上写?

[英]How do write on a matrix in a shared memory?

I am trying to write a matrix of strings in a piece of shared memory. 我正在尝试在一块共享内存中编写字符串矩阵。 I'm constantly getting a segmentation fault error. 我不断收到细分错误错误。 Why do I keep getting a segmentation fault error? 为什么会不断出现细分错误错误?

/* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
#include <semaphore.h>  /* Semaphore */

#include <wait.h>
#include <sys/stat.h>
#include <fcntl.h>      // Para as variáveis O_CREAT, O_EXCL etc
#include <sys/mman.h>
#include <time.h>

#define str_len 10

typedef struct{
   char **posix;
} line;

int main(void){

    int fd;
    line *m;
    int data_size = sizeof(line)*4;
    //char m[4][3][str_len];
    //int i,j;

    if ((fd = shm_open("/shm_ex13s", O_CREAT|O_RDWR,S_IRUSR|S_IWUSR)) < 0) {    // Abrir o objeto da memória partilhada
        fprintf(stderr, "ERROR: No shm_open()\n");
        exit(EXIT_FAILURE);
    }                                                                       
    ftruncate(fd, data_size);                                               // Ajustar o tamanho da memória partilhada                                                                  
    m = (line *) mmap(NULL, data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);  // Mapear a memória partilhada


    m->posix[0] = "ASDAS";

    //printf("%s\n", m[0][0]);

    // Desfaz mapeamento
    munmap(m, data_size);
    // Fecha o descritor devolvido pelo shm_open
    close(fd);
    // O Leitor apaga a memória Partilhada do Sistema 
    shm_unlink("/shm_ex13s"); 
    exit(EXIT_SUCCESS);

    exit(EXIT_SUCCESS);
 }

You cannot share pointers between processes. 您不能在进程之间共享指针。 Each process has its own virtual memory. 每个进程都有自己的虚拟内存。 Addresses cannot be shared. 地址不能共享。

Futhermore with instruction 还有指导

mmap(NULL, data_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

you are creating a shared memory of 4 line variable, where pointer of pointers are uninitialized. 您正在创建一个4 line变量的共享内存,其中指针的指针未初始化。

Accessing it and assigning it with 访问并分配给

m->posix[0] = "ASDAS";

it is Undefined Behavior : you are assigning to posix[0] that is not inizialized. 这是未定义的行为 :您正在分配未初始化的posix[0]

Finally you cannot do what you think to do. 最后,您无法做您想做的事。 What you can do is to create a shared memory and copy your literal strings into it, you cannot referring to them due to the fact that you cannot share pointers between processes. 您可以做的是创建一个共享内存并将文字字符串复制到其中,由于您无法在进程之间共享指针,因此您无法引用它们。

For example you can: 例如,您可以:

char *string = "ASDAS";
char *m = (line *) mmap(NULL, strlen(string)+1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

strcpy(m, string, strlen(string));

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

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