61 lines
1.2 KiB
C
61 lines
1.2 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;
|
|
|
|
int getShmid();
|
|
tile_t *shmServerAt();
|
|
|
|
void setupServerShm() {
|
|
_SHMID = getShmid();
|
|
_BOARD = shmServerAt();
|
|
|
|
for (int i=0; i < _ROWS * _COLLUMS; i++) {
|
|
_BOARD[i] = EMPTY;
|
|
}
|
|
}
|
|
|
|
int getShmid() {
|
|
int shmid = shmget(ftok(KEYFILE, 'h'), _ROWS * _COLLUMS * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
|
|
if (shmid == -1) {
|
|
errExit("shmget", "getShmid");
|
|
}
|
|
|
|
|
|
return shmid;
|
|
}
|
|
|
|
tile_t *shmServerAt() {
|
|
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) { //FIXME: sono tutte var globali
|
|
if (shmdt(shm_ptr) == -1) {
|
|
errExit("shmdt", "shmDt");
|
|
}
|
|
}
|
|
|
|
void shmServerRm(int shmid) {
|
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
|
|
errExit("shmctl", "shmServerRm");
|
|
}
|
|
} |