52 lines
1.0 KiB
C
52 lines
1.0 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(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");
|
|
}
|
|
|
|
for (int i=0; i < _ROWS * _COLLUMS; i++) {
|
|
board[i] = 0;
|
|
}
|
|
|
|
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");
|
|
}
|
|
} |