0

I'm doing an university project and I was asked to create a kind of game between processes: they have to capture some flags placed over a shared matrix, similar to a chessboard. I represented the board using a struct and cells as a matrix but, after allocating the shared memory segment, I cannot attach it correctly because when I try to access to matrix cells I get "segmentation fault", in fact I see that matrix pointer is zero. So, how do I attach the matrix after the whole board is allocated?

Here's the code of the functions I wrote to allocate/attach the board:

int allocate_board(int width, int height){
    size_t size;
    int shm_id;
    key_t shm_key;
    char current_path[PATH_MAX];
    getcwd(current_path, sizeof(current_path));
    shm_key = ftok(current_path, 1);
    size = sizeof(board) + ( sizeof(cell) * height * width );
    shm_id = shmget(shm_key, size, IPC_CREAT | 0660);
    if ( shm_id < 0 ){
        printf("Cannot allocate the game board, aborting.\n");
        exit(1);
    }
    return shm_id;
}

board* get_board(int shm_id){
    void* shm_ptr;
    board* game_board;
    shm_ptr = shmat(shm_id, NULL, 0);
    if ( (int)shm_ptr == -1 ){
        printf("Cannot attach the shared memory segment, aborting.\n");
        printf("Reported error: %s.\n", strerror(errno));
        exit(2);
    }
    game_board = (board *)shm_ptr;
    return game_board;
}

Here's the structs definition:

typedef struct {
    entity_type_t entity_type;
    owner_id_t owner_id;
    float flag_score;
} cell;

typedef struct {
    int width;
    int height;
    sem_t* semaphore;
    cell** cells;
} board;

Here's how I try to access to board's cells:

game_board->width = some_width;
game_board->height = some_height;
game_board->cells[0][0].flag_score = some_score;
5
  • 2
    You do not have arrays. You have pointers which makes this completely different thing. You cannot have sensible pointers in shared memory, period. Each process has its own memory layout so the pointers of one would not make sense in another process. Commented Jan 11, 2020 at 23:12
  • Perhaps you want to define your board so that it has a flexible array member at the end. Commented Jan 11, 2020 at 23:14
  • stackoverflow.com/a/41471343/918959 <= like so. Unfortunately you cannot use this with 2-d indexing but you can use cells[x + y * game_board->width] then. Commented Jan 11, 2020 at 23:16
  • Note that the sem_t * presents a similar problem. You should be storing the sem_t object itself in shared memory, or else using a named semaphore that is separate from your board structure. Commented Jan 11, 2020 at 23:55
  • Also, you are using SysV shared memory together with POSIX semaphores. There's nothing inherently wrong with that, but it's stylistically troubling. I would suggest using POSIX shared memory instead. Commented Jan 11, 2020 at 23:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.