53 lines
1.1 KiB
C
53 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <sys/shm.h>
|
|
#include <sys/stat.h>
|
|
#include <custom_shm.h>
|
|
|
|
#include <errExit.h>
|
|
#include <forza4.h>
|
|
|
|
int _SHMID;
|
|
|
|
void setupServerShm() {
|
|
_SHMID = getShmid(_ROWS, _COLLUMS);
|
|
_BOARD = shmServerAt(_SHMID);
|
|
}
|
|
|
|
int getShmid(int row, int col) {
|
|
int shmid = shmget(ftok(KEYFILE, 'z'), row * col * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
|
|
if (shmid == -1) {
|
|
errExit("shmget", "getShmid");
|
|
}
|
|
|
|
return shmid;
|
|
}
|
|
|
|
tile_t * shmServerAt(int shmid) {
|
|
tile_t *board = (tile_t *)shmat(shmid, NULL, 0);
|
|
if (board == SHMERR) {
|
|
errExit("shmat", "shmServerAt");
|
|
}
|
|
|
|
return board;
|
|
}
|
|
|
|
tile_t * shmClientAt(int shmid) {
|
|
tile_t *board = (tile_t *)shmat(shmid, NULL, SHM_RDONLY);
|
|
if (board == SHMERR) {
|
|
errExit("shmat", "shmClientAt");
|
|
}
|
|
|
|
return board;
|
|
}
|
|
|
|
void shmDt(void *shm_ptr) {
|
|
if (shmdt(shm_ptr) == -1) {
|
|
errExit("shmdt", "shmDt");
|
|
}
|
|
}
|
|
|
|
void shmServerRm(int shmid) {
|
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
|
|
errExit("shmctl", "shmServerRm");
|
|
}
|
|
} |