通过C中的共享内存将结构传递给进程

时间:2017-12-09 12:43:33

标签: c struct shared

对于uni赋值我试图在共享内存中创建一个结构,在其中写入一些数据,然后让孩子读取它。我想出了一些东西但是,因为我是C的新手,我得到一些我不太明白的错误,这里是代码:

typedef struct file
{
    int pid;
    char tipo[2];
    char nome[20];
    unsigned long genoma;
    char shmPtr[];
} file_entry;

int main (void)
{

int shmid;
int n;
file_entry *entries;

if (fork() == 0) {
    if ((shmid = shmget(20441, sizeof(file_entry) + 256, IPC_CREAT | 0666)) == -1) {
        printf("shmget");
        exit(2);
    }

    entries = (file_entry*) shmat(shmid, 0, 0);
    if (entries->shmPtr == (char *) -1) {
        printf("problem2");
        exit(2);
    }

    printf("\nChild Reading ....\n\n");
    printf("%c\n", entries->shmPtr[0]);
    printf("%c\n", entries->shmPtr[1]);
    printf("%c\n", entries->shmPtr[2]);
    printf("%c\n", entries->shmPtr[3]);
    putchar('\n');
    printf("\nDone\n\n");
} else {
    if ((shmid = shmget(20441, sizeof(file_entry) + 256, IPC_CREAT | 0666)) == -1) {
        printf("problem3");
        exit(2);
    }

    entries = (file_entry *) shmat(shmid, 0, 0);
    if (entries->shmPtr == (char *) -1) {
        printf("problem4");
        exit(2);
    }
    printf("done attachment");

    int pidD = 1234;
    char tipoD[2] = "A";
    char nomeD[20] = "NOMEINDIVIDUO";
    unsigned long genomaD = 10000;


    entries->shmPtr[0]= pidD;
    entries->shmPtr[1]=tipoD;
    entries->shmPtr[2]=nomeD;
    entries->shmPtr[3]= genomaD;


    wait(NULL);
    shmdt(&shmid);
}
exit(0);

}

我得到的错误是那些:

shared memory example 2.c:74:27: warning: incompatible pointer to integer conversion assigning to 'char' from 'char [2]' [-Wint-conversion]
    entries->shmPtr[1]=tipoD;
                      ^~~~~~
shared memory example 2.c:75:27: warning: incompatible pointer to integer conversion assigning to 'char' from 'char [20]' [-Wint-conversion]
    entries->shmPtr[2]=nomeD;

我试图声明结构中变量的维度和父亲的变量是相同的,认为它会有所帮助,但事实并非如此。有谁知道如何解决这个问题?如果您还有一些可以欣赏的示例代码。

1 个答案:

答案 0 :(得分:1)

我试过这个测试应用程序并且它可以工作:

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct file
{
    int pid;
    char tipo[2];
    char nome[20];
    unsigned long genoma;
} file_entry;

int main (void)
{

    int shmid;
    int n;
    file_entry *entries;

    if (fork() == 0) {
        if ((shmid = shmget(20441, sizeof(file_entry) + 256, IPC_CREAT | 0666)) == -1) {
            printf("shmget");
            exit(2);
        }

        entries = (file_entry*) shmat(shmid, 0, 0);
        if (entries == NULL) {
            printf("problem2");
            exit(2);
        }

        printf("\nChild Reading ....\n\n");
        printf("%d\n", entries->pid);
        printf("%s\n", entries->tipo);
        printf("%s\n", entries->nome);
        printf("%lu\n", entries->genoma);
        putchar('\n');
        printf("\nDone\n\n");
    } else {
        if ((shmid = shmget(20441, sizeof(file_entry) + 256, IPC_CREAT | 0666)) == -1) {
            printf("problem3");
            exit(2);
        }

        entries = (file_entry *) shmat(shmid, 0, 0);
        if (entries == NULL ) {
            printf("problem4");
            exit(2);
        }
        printf("done attachment");

        int pidD = 4001;
        char tipoD[2] = "A";
        char nomeD[20] = "sfds";
        unsigned long genomaD = 10000;


        entries->pid  = pidD;
        sprintf(entries->tipo,"%s",tipoD);
        sprintf(entries->nome,"%s",nomeD);
        entries->genoma= genomaD;


        wait(NULL);
        shmdt(&shmid);
    }
    exit(0);
}