Elaborato_SO/src/custom_shm.c

62 lines
1.2 KiB
C
Raw Normal View History

2023-05-14 22:13:55 +02:00
#include <stdio.h>
2023-05-14 18:51:32 +02:00
#include <sys/shm.h>
#include <sys/stat.h>
#include <custom_shm.h>
2023-05-14 22:24:57 +02:00
2023-05-14 18:51:32 +02:00
#include <errExit.h>
2023-05-25 14:28:45 +02:00
#include <forza4.h>
2023-05-14 18:51:32 +02:00
2023-05-23 15:47:34 +02:00
int _SHMID;
2023-05-29 12:16:36 +02:00
int getShmid();
tile_t *shmServerAt();
2023-05-29 11:37:43 +02:00
void setupServerShm() {
2023-05-29 12:16:36 +02:00
_SHMID = getShmid();
_BOARD = shmServerAt();
2023-05-29 11:49:35 +02:00
for (int i=0; i < _ROWS * _COLLUMS; i++) {
_BOARD[i] = EMPTY;
}
2023-05-29 11:37:43 +02:00
}
2023-05-29 12:16:36 +02:00
int getShmid() {
2023-05-29 12:25:03 +02:00
int shmid = shmget(ftok(KEYFILE, 'h'), _ROWS * _COLLUMS * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
2023-05-14 22:13:55 +02:00
if (shmid == -1) {
2023-05-14 18:51:32 +02:00
errExit("shmget", "getShmid");
}
2023-05-14 21:43:25 +02:00
2023-05-14 21:44:03 +02:00
return shmid;
2023-05-14 18:51:32 +02:00
}
2023-05-29 13:06:22 +02:00
tile_t *shmServerAt() { //FIXME: si possono fare void
2023-05-29 12:16:36 +02:00
tile_t *board = (tile_t *)shmat(_SHMID, NULL, 0);
2023-05-25 14:28:45 +02:00
if (board == SHMERR) {
2023-05-14 22:13:55 +02:00
errExit("shmat", "shmServerAt");
}
2023-05-26 10:58:57 +02:00
2023-05-25 14:28:45 +02:00
return board;
2023-05-15 10:52:18 +02:00
}
2023-05-14 22:13:55 +02:00
2023-05-29 13:06:22 +02:00
tile_t * shmClientAt() {
tile_t *board = (tile_t *)shmat(_SHMID, NULL, SHM_RDONLY);
perror("perche");
2023-05-25 14:28:45 +02:00
if (board == SHMERR) {
2023-05-14 22:13:55 +02:00
errExit("shmat", "shmClientAt");
}
2023-05-25 14:28:45 +02:00
return board;
2023-05-14 22:13:55 +02:00
}
2023-05-29 12:16:36 +02:00
void shmDt(void *shm_ptr) { //FIXME: sono tutte var globali
2023-05-15 09:14:34 +02:00
if (shmdt(shm_ptr) == -1) {
2023-05-14 22:13:55 +02:00
errExit("shmdt", "shmDt");
}
}
void shmServerRm(int shmid) {
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
errExit("shmctl", "shmServerRm");
}
}