Elaborato_SO/src/custom_shm.c

48 lines
981 B
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-14 22:13:55 +02:00
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) {
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-14 22:13:55 +02:00
tile_t * shmServerAt(int shmid) {
2023-05-25 14:28:45 +02:00
tile_t *board = (tile_t *)shmat(shmid, NULL, 0);
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
tile_t * shmClientAt(int shmid) {
2023-05-25 14:28:45 +02:00
tile_t *board = (tile_t *)shmat(shmid, NULL, SHM_RDONLY);
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-15 09:14:34 +02:00
void shmDt(void *shm_ptr) {
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");
}
}