共享内存中的c结构 [英] c structure in shared memory

查看:67
本文介绍了共享内存中的c结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似

struct board{
   char name;
   int values[37];
}board

想象一个游戏,在一张桌子上有几个玩家在玩,他们都在value数组的不同位置下了不同的赌注.棋盘的名称是唯一的,玩家进入指定棋盘名称的游戏.如果2个或更多玩家输入相同的棋盘名称,则他们都将加入同一游戏.

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.

这是一个学校项目,我有点绝望..请帮助和感谢.顺便说一句,我要引用POSIX操作系统的共享内存

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

推荐答案

已经在编写POSIX共享内存函数时考虑了这种努力:

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);

如果将"/onica_game_<name>"用作*name参数,则可以轻松指定要为共享游戏附加的共享内存段. (顺便说一句,单个char限制了游戏名称.您可能希望使用char name[32];或更大的名称,使人们有机会为自己的游戏命名更有意义.)

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.)

我建议在名称前加上onica_game_,因为POSIX共享内存段的命名空间是系统范围的.

I suggest prefixing the name with onica_game_ because the namespace for POSIX shared memory segments is system-wide.

这篇关于共享内存中的c结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆