Bryan Shtjefni
Bryan Shtjefni

Reputation: 95

Passing a struct to a process through shared memory in C

For a uni assignment I am trying to create a structure in shared memory, write some data in it, and then let the child read it. I've come up with something but, since I am new to C, I am getting some errors that I can't quite understand, here is the code:

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);

}

The errors I'm getting are those:

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;

I tried to declare the dimension of the variables in the struct and the variables on the father to be the same thinking that it would help, but it didn't. Does anyone know how to solve this? If you also have some example code that would be appreciated.

Upvotes: 1

Views: 3324

Answers (1)

farbiondriven
farbiondriven

Reputation: 2468

I tried with this test application and it works:

#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);
}

Upvotes: 1

Related Questions