Tomasz Kasperek
Tomasz Kasperek

Reputation: 1187

Copying array of structs into shared memory

I have multiple processes operating on the same data. I want to keep the data inside shared memory. Problem is it seems I can't save into it (or retrieve).

My whole code for it is here: https://github.com/kasperekt/LLGame/blob/master/server_src/game_state.c#L74

but it seems that the problem is inside these functions:

typedef struct game_state {
  int resources;
  int wins;
  army_t *army;
} game_state_t;

...

static game_state_t *players[2] = { NULL, NULL };
static game_state_t **mem_state = NULL;

...

void attach_state() {
  mem_state = get_memory_data(0);
  players[0] = mem_state[0];
  players[1] = mem_state[1];
}

void save_state() {
  if (mem_state == NULL) {
    mem_state = get_memory_data(0);
  }

  mem_state[0] = players[0];
  mem_state[1] = players[1];
  detach_memory_data(mem_state);
}

And example function which works on this data:

void increment_resources(int player_id) {
  attach_state();
  const int workers_count = players[player_id]->army->workers;
  players[player_id]->resources += RESOURCES_STEP + (workers_count * 5);
  save_state();
}

How I should save it into memory? How does it work? I can't find answer for this.

Maybe this code will also help:

game_state_t **get_memory_data(char *shmaddr) {
  const int shmat_flag = 0;
  return shmat(memory_id, shmaddr, shmat_flag);
}

void detach_memory_data(game_state_t **data) {
  if (shmdt(data) == -1) {
    perror("Error detaching memory: ");
    exit(1);
  };
}

Upvotes: 1

Views: 831

Answers (1)

Umamahesh P
Umamahesh P

Reputation: 1244

You have game_state_t variable. If you are storing multiple states, you only need single dimension array. Here is the sample.

game_state_t *memstate;
...
memstate = (game_state_t *) malloc( n * sizeof(game_state_t)); /* alloc memory for n states */

shmptr = shmat(...);

memcpy(shmptr, memstate, size);  /* size for example n * size of(game_state_t)); */

OR Simply use only the shared memory.

memstate = shmat(...);

As indicated in the comments, the pointer members of the structure need to point to the shared memory.

Example;

memstate.x = memstate + offset; /* use different offsets based on your usage */ 

Upvotes: 1

Related Questions