与struct和malloc共享内存fork

时间:2018-03-03 17:47:01

标签: c struct malloc ipc shared-memory

我遇到了fork生成的父进程和子进程之间的共享内存问题。 我知道如何使用原始类型的共享内存,如here所述。

虽然我不知道如何共享包含可以通过malloc分配的指针的结构。

例如,假设我从上一个链接中获取了以下代码并进行了一些修改。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h> 
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/wait.h>

typedef struct Data {
    char * name;
} Data;

void error_and_die(const char *msg) {
    perror(msg);
    exit(EXIT_FAILURE);
}

int main(int argc, char *argv[]) {
    int r;

    const char *memname = "sample";
    const size_t region_size = sysconf(_SC_PAGE_SIZE);

    int fd = shm_open(memname, O_CREAT | O_TRUNC | O_RDWR, 0666);
    if (fd == -1)
        error_and_die("shm_open");

    r = ftruncate(fd, region_size);
    if (r != 0)
        error_and_die("ftruncate");

    Data * data = (Data *) malloc(sizeof(data));
    data->name=(char *) malloc(100*sizeof(char));   

    void *ptr = mmap(0, region_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == MAP_FAILED)
        error_and_die("mmap");
    close(fd);

    ptr=(Data *) data; 

    pid_t pid = fork();

    if (pid == 0) {
        //u_long *d = (u_long *) ptr;
        //*d = 0xdbeebee;
        data->name="bob";
        printf("CHILD child wrote %s\n", (*(Data *)ptr).name);
        exit(0);
    }
    else {
        int status;
        waitpid(pid, &status, 0);
        //printf("child wrote %#lx\n", *(u_long *) ptr);
        printf("PARENT child wrote %s\n", (*(Data *)ptr).name);
    }

    r = munmap(ptr, region_size);
    if (r != 0)
        error_and_die("munmap");

    r = shm_unlink(memname);
    if (r != 0)
        error_and_die("shm_unlink");

    return 0;
}

我想在父进程和子进程之间更改结构内部的数据以允许IPC。 我该怎么办?

谢谢

1 个答案:

答案 0 :(得分:0)

我自己解决了这个guide并使用 shmget,shmat,smhd t和 shmctl 而不是 shm_open,mmap,munmap shm_unlink

通过这种方式,我可以像使用malloc一样管理动态共享内存,其优点是内存在进程之间可见。

我在这里将代码报告为未来的提示。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>

typedef struct Person {
    char * name;
    int * numbers;
} Person;

typedef struct Data {
    Person * persons;
} Data;

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

    Data * data;
    Person * person;

    key_t mykey1=5500;
    key_t mykey2=5501;
    key_t mykey3=5502;
    key_t mykey4=5503;
    key_t mykey5=5504;
    int mem_id;

    mem_id=shmget(mykey1,sizeof(Person),IPC_CREAT|0666);
    if (mem_id<0) {
        perror("error shmget");
    }
    person=(Person*)shmat(mem_id,(void*)0,0);
    if(person == (Person*)(-1)) {
        perror("error shmat");
    }

    mem_id=shmget(mykey2,(100*sizeof(char)),IPC_CREAT|0666);
    if (mem_id<0) {
        perror("error shmget 2");
    }

    person->name=(char *)shmat(mem_id,(void*)0,0);
    if (person->name == (char *)(-1)) {
        perror("error shmat 2");
    }

    mem_id=shmget(mykey3,(10*sizeof(int)),IPC_CREAT|0666);
    if (mem_id<0) {
        perror("error shmget 3");
    }

    person->numbers=(int *)shmat(mem_id,(void*)0,0);
    if (person->numbers == (int *)(-1)) {
        perror("error shmat 3");
    }

    mem_id=shmget(mykey4,sizeof(Data),IPC_CREAT|0666);
    if (mem_id<0) {
        perror("error shmget 4");
    }
    data=(Data*)shmat(mem_id,(void*)0,0);
    if(data == (Data*)(-1)) {
        perror("error shmat 4");
    }

    mem_id=shmget(mykey5,(10*sizeof(int)),IPC_CREAT|0666);
    if (mem_id<0) {
        perror("error shmget 5");
    }

    data->persons=(Person *)shmat(mem_id,(void*)0,0);
    if (data->persons == (Person *)(-1)) {
        perror("error shmat 5");
    }    

    pid_t pid = fork(); 
    if (pid == 0) {
        person->name="bob";
        for(int i=0; i<10; i++) {
            person->numbers[i]=i;       
        }
        data->persons[0]=*person;
        data->persons[1].name="alice";
        printf("CHILD child wrote %s\n", person->name);
        exit(0);
    }
    else {
        int status;
        waitpid(pid, &status, 0);
        printf("PARENT child wrote %s\n", person->name);
        for(int i=0; i<10; i++) {
            printf("%d\n",person->numbers[i]);      
        }

        printf("PARENT child wrote %s\n", data->persons[0].name);
        for(int i=0; i<10; i++) {
            printf("%d\n",data->persons[0].numbers[i]);     
        }
        printf("PARENT child wrote %s\n", data->persons[1].name);
    }
    shmdt(person);
    shmdt(data);
    shmctl(mem_id,IPC_RMID,0); 

    return 0;
}