mercredi 25 octobre 2023

Mmapping shared memory in C giving Invalid argument error when I change the offset to anything nonzero

I'm trying to open a shared memory file, write in it, fork to new processes, then call execl to run other components, then in those components I want to access the shared memory created in the parent. But I dont want the entire structure, only part of it. So I use offsetof to find the offset and pass that into mmap. Problem is, it gives an Invalid argument error when I change the offset to anything other than zero. It works fine with zero.

I've made a minimal dummy program with the same error, and the code for the larger program is too big. `

#include<stdio.h>
#include<unistd.h>
#include<sys/mman.h>
#include<stddef.h>
#include<fcntl.h>

typedef struct test
{
    int moo;
    char hello;
}Lil;


typedef struct bigstruct
{
    int a;
    char cow;
    Lil lil;
}Big;

int main()
{
    int fd = shm_open("/moomor", O_CREAT | O_RDWR, 0666);
    if(fd == -1)
    {
        perror("shmopen");
        return 1;
    }
    ftruncate(fd, sizeof(Big));
    Big * big = (Big *)mmap(NULL, sizeof(Big), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(big == MAP_FAILED)
    {
        perror("mmap");
        return 1;
    }
    big->lil.hello = 'h';
    printf("hello: %c\n", big->lil.hello); //prints correctly
    munmap(big, sizeof(Big)); //tried it with this in and out

    int offset = offsetof(Big, lil); //gives offset of eight
    printf("Offset: %d\n", offset);
    Lil *lily = (Lil *)mmap(NULL, sizeof(Lil), PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
    if(lily == MAP_FAILED)
    {
        perror("mmap"); //this causes an error
        goto cleanup;
    }
    printf("HH: %c\n", lily->hello);
    munmap(lily, sizeof(Lil));

    cleanup:
    shm_unlink("/moomor");
    close(fd);
}

Aucun commentaire:

Enregistrer un commentaire