Elaborato_SO/src/custom_shm.c

95 lines
2.3 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-06-18 15:32:52 +02:00
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.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
tile_t *shmServerAt();
2023-05-29 11:37:43 +02:00
void setupServerShm() {
2023-06-18 15:32:52 +02:00
_SHMID = getShmidServer();
2023-05-29 12:16:36 +02:00
_BOARD = shmServerAt();
2023-06-18 15:32:52 +02:00
for (int i = 0; i < _ROWS * _COLLUMS; i++) {
2023-05-29 11:49:35 +02:00
_BOARD[i] = EMPTY;
}
2023-05-29 11:37:43 +02:00
}
2023-05-29 13:12:38 +02:00
void setupClientShm() {
printf(PCLIENT "Setting up shm\n");
2023-06-18 15:32:52 +02:00
_SHMID = getShmidClient();
2023-05-29 13:12:38 +02:00
_BOARD = shmClientAt();
}
2023-06-18 15:32:52 +02:00
int getShmidClient() {
2023-06-18 16:13:05 +02:00
return shmget(ftok(KEYFILE, SHMKEY), _ROWS * _COLLUMS * sizeof(tile_t), IPC_CREAT | S_IRUSR | S_IWUSR);
2023-06-18 15:32:52 +02:00
}
int getShmidServer() {
key_t key = ftok(KEYFILE, SHMKEY);
int shmid = shmget(key, _ROWS * _COLLUMS * sizeof(tile_t), IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
2023-05-14 22:13:55 +02:00
if (shmid == -1) {
2023-06-18 15:32:52 +02:00
// if errno was set to EEXIST, that means the memory location is alredy allocated
// the server fork and the child remove the memory
if (errno == EEXIST) {
char buf[100];
2023-06-18 16:01:17 +02:00
sprintf(buf, "The board alredy exit, I'll remove it now\n");
2023-06-18 15:32:52 +02:00
warningMsg(buf);
pid_t pid = fork();
if (pid == 0) {
char str_key[16];
sprintf(str_key, "0x%08x", key);
char *vec[] = {"/bin/ipcrm", "-M", str_key, NULL};
if ((execv("/bin/ipcrm", vec)) == -1) {
errExit("execv", "getShmidServer");
}
}
while (wait(0) == -1 && errno == EINTR);
return getShmidServer();
}
errExit("shmget", "getShmidServer");
2023-05-14 18:51:32 +02:00
}
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-06-18 15:54:51 +02:00
tile_t *shmServerAt() {
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);
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-06-18 15:54:51 +02:00
void shmDt(void *shm_ptr) {
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");
}
}