c structure in shared memory

倖福魔咒の 提交于 2019-12-12 17:12:33

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8073704/c-structure-in-shared-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!