sigterm
This commit is contained in:
parent
87af494b2c
commit
a31aedcdc4
|
@ -1,6 +1,7 @@
|
|||
#ifndef STRUCTURES_H
|
||||
#define STRUCTURES_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
// semaphore
|
||||
union semun {
|
||||
|
@ -48,6 +49,7 @@ typedef struct {
|
|||
long mtype; // type 3
|
||||
int id;
|
||||
char name_player[MAX_NAME];
|
||||
pid_t pid;
|
||||
} player_names_t;
|
||||
|
||||
#endif
|
|
@ -8,6 +8,7 @@
|
|||
#include <sys/sem.h>
|
||||
#include <sys/shm.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <custom_sem.h>
|
||||
#include <structures.h>
|
||||
|
@ -24,7 +25,7 @@ void sigHandlerClient(int sig) {
|
|||
sig = SIGTERM;
|
||||
}
|
||||
|
||||
if (sig == SIGTERM) {
|
||||
if (sig == SIGTERM || sig == SIGHUP) {
|
||||
// shm
|
||||
if (boardClient) {
|
||||
printf("\n");
|
||||
|
@ -54,6 +55,7 @@ int main(int argc, char *argv[]){
|
|||
sigfillset(&mySet);
|
||||
sigdelset(&mySet, SIGINT);
|
||||
sigdelset(&mySet, SIGTERM);
|
||||
sigdelset(&mySet, SIGHUP);
|
||||
sigprocmask(SIG_SETMASK, &mySet, NULL);
|
||||
|
||||
if (signal(SIGINT, sigHandlerClient) == SIG_ERR) {
|
||||
|
@ -109,7 +111,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
|
||||
// mando il mio nome al server
|
||||
player_names_t name = {.mtype = 3, .id = msg.player_id};
|
||||
player_names_t name = {.mtype = 3, .id = msg.player_id, .pid = getpid()};
|
||||
strcpy(name.name_player, argv[1]);
|
||||
if (msgsnd(msgid, &name, sizeof(player_names_t) - sizeof(long), 0) == -1) {
|
||||
errExit("msgsnd", "mandare nome");
|
||||
|
|
|
@ -23,21 +23,27 @@ int shmid = 0;
|
|||
|
||||
tile_t *board = NULL;
|
||||
|
||||
pid_t pids[2];
|
||||
|
||||
void sigHandlerServer(int sig) {
|
||||
if (sig == SIGINT) {
|
||||
//TODO:
|
||||
//TODO:sigdelset(&mySet, SIGTERM);
|
||||
|
||||
|
||||
|
||||
sig = SIGTERM; // per il secondo SIGINT basta modificare il sig in SIGTERM
|
||||
}
|
||||
|
||||
if (sig == SIGTERM) {
|
||||
//TODO: killare figli
|
||||
if (sig == SIGTERM || sig == SIGHUP) {
|
||||
printf("\n");
|
||||
printfServer("Terminating players");
|
||||
kill(pids[0], SIGTERM);
|
||||
kill(pids[1], SIGTERM);
|
||||
|
||||
// msgq
|
||||
if (msgid) {
|
||||
printf("\n");
|
||||
printfServer("Deleting msg\n");
|
||||
printfServer("Deleting msg");
|
||||
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
|
||||
errExit("msgctl", "sigHandlerServer");
|
||||
}
|
||||
|
@ -68,6 +74,8 @@ void sigHandlerServer(int sig) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
@ -84,6 +92,7 @@ int main(int argc, char *argv[]){
|
|||
sigfillset(&mySet);
|
||||
sigdelset(&mySet, SIGINT);
|
||||
sigdelset(&mySet, SIGTERM);
|
||||
sigdelset(&mySet, SIGHUP);
|
||||
sigprocmask(SIG_SETMASK, &mySet, NULL);
|
||||
|
||||
if (signal(SIGINT, sigHandlerServer) == SIG_ERR) {
|
||||
|
@ -151,8 +160,9 @@ int main(int argc, char *argv[]){
|
|||
//FIXME: forse non serve perche tanto deve aspettare che i player gli mandono un messaggio con il loro nome (a cose servono i semafori se ci sono i messaggi che fanno l'attesa non in polling aaaaaaaaa)
|
||||
semOp(semid, 2, -1);
|
||||
|
||||
player_names_t names; // lo so si possono usare nomi piu chiari ma problema tuo (mi riferisco a te e me futuro)
|
||||
player_names_t names;
|
||||
char name[2][MAX_NAME];
|
||||
pids[2];
|
||||
|
||||
// ricevuta nome primo client
|
||||
if (msgrcv(msgid, &names, sizeof(player_names_t) - sizeof(long), 3, 0) == -1) {
|
||||
|
@ -160,6 +170,7 @@ int main(int argc, char *argv[]){
|
|||
}
|
||||
|
||||
strcpy(name[names.id], names.name_player);
|
||||
pids[names.id] = names.pid;
|
||||
|
||||
printfServer("Player ");
|
||||
printf("%s connected\n", name[names.id]);
|
||||
|
@ -186,6 +197,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
int turns_left = input.rows * input.collums;
|
||||
int turn = 0;
|
||||
tile_t result;
|
||||
move_t move;
|
||||
|
||||
do {
|
||||
|
@ -204,10 +216,10 @@ int main(int argc, char *argv[]){
|
|||
printf("turns left: %d\n", turns_left - 1);
|
||||
|
||||
|
||||
} while (--turns_left && !checkWin(board, move.move, input.rows, input.collums));
|
||||
} while (--turns_left && !(result = checkWin(board, move.move)));
|
||||
|
||||
|
||||
if (!turns_left && checkWinAll(board, input.rows, input.collums) == 0) {
|
||||
if (!result) {
|
||||
//TODO: draw
|
||||
} else {
|
||||
//TODO: win
|
||||
|
|
Loading…
Reference in New Issue