funzioni msgq

This commit is contained in:
Lorenzo Bianchi 2023-05-22 10:07:57 +02:00
parent f0e9f75c31
commit a0dd1587ad
6 changed files with 36 additions and 37 deletions

View File

@ -1,20 +1,8 @@
- magari fare che (alcune delle) print vengano messe su tutti e tre i terminali
(motivo per cui ho messo le funzioni in nonsodovemetterle)
(forse basta tipo duplicare il fd ma sono stanco ci penso domani)
- sistemare include (FARE SOLO ALLA FINE ALTRIMENTI SPRECO DI TEMPO COME LA TUA VITA)
- invece di fare tutti if == -1 si potrebbe fare come semOp e creare una funzione identica
che fa gia il controllo (ha come svantaggio che non puoi specifare dove va in errore con l'errexit)
(anche se forse basta passare la/le stringe ma vedi riga 3) EDI APPROVA
- testare chiusura figli perche i miei terminali si chiudono da soli
- fixare questo perche non funziona:
sigint_count--;
if (sigint_count > 0) {
printfServer("Press again Ctrl^C to exit ");
printf("(whitin %d sec)\n", TIME_TO_RESET);
alarm(TIME_TO_RESET);
} else {
}
- al momomento i client non sanno ancora le dimensioni del campo (le hanno ricevute ma non settate)
(in realta non so perche funzioni la checkwin al momento)
- sistemare se usare printfServer/Client

View File

@ -35,6 +35,8 @@ typedef struct {
int winner; // -1 se draw
} game_end_t;
void openMsgq();
void sndId();
void rcvMsg(msg_t *msg);
void rcvMove(move_t *move);

View File

@ -10,8 +10,9 @@ union semun {
unsigned short * array;
};
#define KEYFILE "./LICENSE"
// shared memory
#define KEYFILE "../LICENSE"
#define SHMERR (void *)-1
typedef struct {

View File

@ -34,32 +34,28 @@ int main(int argc, char *argv[]){
// MSGQ
printfServer("Setting up msgq");
//TODO: forse bisogna fare il reset della shm, perche in teoria mette a 0 quando crea
//ma nel caso in cui il campo non si fosse chiuso per qualche motivo forse quando lo riapre non lo azzera
key_t msgKey = ftok(KEYFILE, 'M');
_MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
if (_MSGQID == -1){
errExit("msgget", "F4Server");
}
// msg
msg_t msg = {
.mtype = 1,
.server_in = input,
.player_id = 0
};
openMsgq();
msg_t msg = { // FIXME: non riesco a metterli in una funzione :(
.mtype = 1,
.server_in = input,
.player_id = 0
};
// msgsnd
sndMsg(&msg);
msg.player_id = 1;
sndMsg(&msg);
sndMsg(&msg);
msg.player_id = 1;
sndMsg(&msg);
//
// SHM
//TODO: forse bisogna fare il reset della shm, perche in teoria mette a 0 quando crea
//ma nel caso in cui il campo non si fosse chiuso per qualche motivo forse quando lo riapre non lo azzera
printfServer("Setting up shm");
shmid = getShmid(input.rows, input.collums);
board = shmServerAt(shmid);
//
// SEM
//
printfServer("Setting up sem");
key_t semKey = ftok(KEYFILE, 'S');

View File

@ -1,9 +1,20 @@
#include <sys/msg.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdio.h>
#include <custom_msgq.h>
#include <errExit.h>
void openMsgq() {
key_t msgKey = ftok(KEYFILE, 'M');
_MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
if (_MSGQID == -1){
errExit("msgget", "F4Server");
}
}
void msgRcv(void *msgp, size_t size, long mtype) {
int res;
do {
@ -11,7 +22,7 @@ void msgRcv(void *msgp, size_t size, long mtype) {
} while (errno == EINTR);
if (res == -1) {
errExitMsg("msgrcv");
errExit("msgrcv", "msgRcv"); //FIXME: mi ha dato un errore quando ho chiuso prima i figli e fatto ctrl+c * 2, da controllare
}
}

View File

@ -10,12 +10,13 @@
#include <structures.h>
#include <nonsodovemetterle.h>
#include <errExit.h>
#include <custom_msgq.h>
static int sigint_count = 2;
void setSignal(int sig);
void siginthandler2(int sig) {
void sigIntHandler2(int sig) {
kill(getpid(), SIGTERM);
}
@ -42,7 +43,7 @@ void sigHandlerServer(int sig) {
if (sig == SIGINT) {
printfServer("Press again Ctrl^C to exit ");
printf("(whitin %d sec)\n", TIME_TO_RESET);
signal(SIGINT, siginthandler2);
signal(SIGINT, sigIntHandler2);
alarm(TIME_TO_RESET);
}