From 31d46721525c4f95f84111b4bcefc5ead8bfa1fc Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Mon, 28 Mar 2011 09:01:21 +0200 Subject: [PATCH] =?UTF-8?q?Gestion=20d'arr=C3=AAt=20du=20consommateur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C_Language/TP_SYS/tp_sema/consommateur.c | 29 ++++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/C_Language/TP_SYS/tp_sema/consommateur.c b/C_Language/TP_SYS/tp_sema/consommateur.c index aa8fb72..12fc2f3 100644 --- a/C_Language/TP_SYS/tp_sema/consommateur.c +++ b/C_Language/TP_SYS/tp_sema/consommateur.c @@ -11,6 +11,7 @@ #include #include +#include #include #include #include "consommateur.h" @@ -18,6 +19,7 @@ #include "sema.h" #include "constantes.h" +void quitter(int signal); WINDOW *creation_fenetre(int n,int d,char *t); typedef struct prod_s { @@ -25,15 +27,15 @@ typedef struct prod_s WINDOW w; } PROD; +int shmid, shm_key, i = 0; +int mutex_data, mutex_tpa; +key_t sem_key_data = MUTEX_DATA; +key_t sem_key_tpa = MUTEX_TPA; + int main( int argc, char **argv) { if(argc < 2) { printf("Usage : %s nSHM \n", argv[0]); exit(EXIT_FAILURE); } - int shmid, shm_key, i = 0; - int mutex_data, mutex_tpa; - key_t sem_key_data = MUTEX_DATA; - key_t sem_key_tpa = MUTEX_TPA; - shm_key = (key_t) atoi(argv[1]); MEMP * memoireP; MEMP temp; @@ -43,6 +45,10 @@ int main( int argc, char **argv) WINDOW *w ; char c ; + signal(SIGHUP, quitter); + signal(SIGINT, quitter); + signal(SIGQUIT, quitter); + initscr() ; /* initialisation (obligatoire) de curses */ noecho() ; /* suppression de l'echo des caracteres tapes*/ cbreak() ; /* lecture non bufferisee */ @@ -164,3 +170,16 @@ WINDOW *creation_fenetre(int n,int d,char *t) wrefresh(w) ; return w ; } + +void quitter(int signal) +{ + if(shmctl(shmid, IPC_RMID, 0) < 0) + { perror("shmctl"); exit(EXIT_FAILURE); } + + if(mutex_data >= 0) { del_sem(sem_key_data); } + if(mutex_tpa >= 0) { del_sem(sem_key_tpa); } + + endwin() ; + printf("FIN.\n"); + exit(EXIT_SUCCESS); +}