Reputation: 183
I have a structure like
struct board{
char name;
int values[37];
}board
Imagine a game where there are several players playing at a single table and they all make different bets on different positions of the values array.The name of the board is unique and a player enters a game specifying the board name. If 2 or more players enter the same board name they all join the same game.
I need to put this structure into shared memory and access/modify the contents of "values"
from different processes at the same time (semaphores are not a problem). I managed to copy the structure in a piece of shared memory but I don't how to access the name to see if a board already exists and things like that.
This is for a school project and I'm a little desperate.... pleeease help and thanks. By the way, the shared memory I want references to the POSIX os
Upvotes: 2
Views: 1811
Reputation: 104080
The POSIX shared memory functions are already written with this sort effort in mind:
int shm_open(const char *name, int oflag, mode_t mode);
If you use "/onica_game_<name>"
for your *name
parameter, you can easily specify which shared memory segments to attach to for your shared games. (Incidentally, a single char
is a bit limiting for game names. You might want to use char name[32];
or something larger to give people an opportunity to name their games something more meaningful.)
I suggest prefixing the name with onica_game_
because the namespace for POSIX shared memory segments is system-wide.
Upvotes: 4