From fb06534b3f571e07fa439d37f2eb34a00d976692 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 20 Mar 2011 17:21:34 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20avec=20la=20biblioth=C3=A8que=20nc?= =?UTF-8?q?urses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C_Language/TP_SYS/tp_sema/Makefile | 6 +- C_Language/TP_SYS/tp_sema/consommateur.c | 76 ++++++++++++++++++++---- C_Language/TP_SYS/tp_sema/producteur.c | 54 +++++++++++------ C_Language/TP_SYS/tp_sema/sema.h | 10 +--- 4 files changed, 106 insertions(+), 40 deletions(-) diff --git a/C_Language/TP_SYS/tp_sema/Makefile b/C_Language/TP_SYS/tp_sema/Makefile index 79b9683..b55386a 100644 --- a/C_Language/TP_SYS/tp_sema/Makefile +++ b/C_Language/TP_SYS/tp_sema/Makefile @@ -1,11 +1,11 @@ CC = gcc CONS = consommateur PROD = producteur -CFLAGS = -Wall -g -std=c99 +CFLAGS = -lncurses -Wall -g -std=c99 -D_XOPEN_SOURCE -D_SVID_SOURCE COMMUN = sema.o global.o all: $(COMMUN) consommateur.o producteur.o - $(CC) consommateur.o -o $(CONS) sema.o - $(CC) producteur.o -o $(PROD) sema.o + $(CC) consommateur.o -o $(CONS) sema.o $(CFLAGS) + $(CC) producteur.o -o $(PROD) sema.o $(CFLAGS) sema.o : sema.c sema.h $(CC) -o $@ -c $< $(CFLAGS) diff --git a/C_Language/TP_SYS/tp_sema/consommateur.c b/C_Language/TP_SYS/tp_sema/consommateur.c index f0ad78f..00c4b1c 100644 --- a/C_Language/TP_SYS/tp_sema/consommateur.c +++ b/C_Language/TP_SYS/tp_sema/consommateur.c @@ -10,25 +10,27 @@ #include #include #include + +#include +#include #include "consommateur.h" #include "types.h" #include "sema.h" #include "constantes.h" +WINDOW *creation_fenetre(int n,int d,char *t); + int main( int argc, char **argv) { - - int shmid, shm_key; - int mutex_data, mutex_tpa; - key_t sem_key_data; - key_t sem_key_tpa; - if(argc < 2) { printf("Usage : %s nSHM \n", argv[0]); exit(EXIT_FAILURE); } - shm_key = (key_t) atoi(argv[1]); - sem_key_tpa = MUTEX_TPA; - sem_key_data = MUTEX_DATA; + int shmid, shm_key, i = 0; + int mutex_data, mutex_tpa, mutex_glob; + key_t sem_key_data = MUTEX_DATA; + key_t sem_key_tpa = MUTEX_TPA; + key_t sem_key_glob = MUTEX_GLOB; + shm_key = (key_t) atoi(argv[1]); MEMP * memoireP; if((shmid = shmget(shm_key, sizeof(MEMP), IPC_CREAT|IPC_EXCL|0766)) == -1) @@ -43,26 +45,78 @@ int main( int argc, char **argv) if((mutex_tpa = creat_sem( sem_key_tpa, 1)) == -1) { perror("creat_sem"); exit(EXIT_FAILURE); } + if((mutex_glob = creat_sem( sem_key_glob, 1)) == -1) + { perror("creat_sem"); exit(EXIT_FAILURE); } + P(mutex_data); memoireP->tete = 0; memoireP->queue = 0; + V(mutex_data); P(mutex_tpa); - for(int i = 0; i < MAX_PROD ; i++) + for( i = 0; i < MAX_PROD ; i++) memoireP->tpa[i] = -1; V(mutex_tpa); sleep(10); + + const char CTRL_D = 4 ; + + int NB_FENETRES = 4; + WINDOW *f_haut, *f_bas, *f_milieu1, *f_milieu2 ; + WINDOW *w ; + char c ; + + initscr() ; /* initialisation (obligatoire) de curses */ + noecho() ; /* suppression de l'echo des caracteres tapes*/ + cbreak() ; /* lecture non bufferisee */ + + f_haut = creation_fenetre(LINES/NB_FENETRES,0,"F_HAUT") ; + f_milieu1 = creation_fenetre(LINES/NB_FENETRES,LINES - 3 * (LINES/NB_FENETRES),"F_MILIEU1") ; + f_milieu2 = creation_fenetre(LINES/NB_FENETRES,LINES - 2 * (LINES/NB_FENETRES),"F_MILIEU2") ; + f_bas = creation_fenetre(LINES/NB_FENETRES, LINES - (LINES/NB_FENETRES),"F_BAS") ; + + while (( c = wgetch(f_bas)) != CTRL_D) + { + w= islower(c) ? f_haut : f_bas; + waddch(w,c) ; + wrefresh(w) ; + } + + endwin() ; + 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); } + if(mutex_glob >= 0) { del_sem(sem_key_glob); } exit(EXIT_SUCCESS); } +WINDOW *creation_fenetre(int n,int d,char *t) +{ + WINDOW *cadre ; /* la fenetre pour le cadre */ + WINDOW *w ; /* la fenetre de dialogue */ + + /* creation du cadre */ + + cadre= newwin(n,COLS,d,0) ; + box(cadre,0,0) ; + mvwprintw(cadre,0,COLS/2-strlen(t)/2,t) ; + wrefresh(cadre) ; + + /* creation de la fenetre de dialogue */ + + w= newwin(n-2,COLS-2,d+1,1) ; + idlok(w,TRUE) ; + scrollok(w,TRUE) ; /* mise en place du defilement (scrolling) */ + wclear(w) ; + wrefresh(w) ; + return w ; +} diff --git a/C_Language/TP_SYS/tp_sema/producteur.c b/C_Language/TP_SYS/tp_sema/producteur.c index 881ef9a..8db1297 100644 --- a/C_Language/TP_SYS/tp_sema/producteur.c +++ b/C_Language/TP_SYS/tp_sema/producteur.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,33 +13,48 @@ int main( int argc, char **argv) { if(argc < 2) { printf("Usage %s numIPC\n", argv[0]); exit(EXIT_FAILURE); } MEMP *memoireP; - int mutex_data, mutex_tpa; - key_t sem_key_data; - key_t sem_key_tpa; + int mutex_data, mutex_tpa, mutex_glob, continuer = 0, i=0; + char c; + MSG message; + key_t sem_key_data = MUTEX_DATA; + key_t sem_key_glob = MUTEX_GLOB; + key_t sem_key_tpa = MUTEX_TPA; int shmid; int shm_key = atoi(argv[1]); -/* création ou lien avec une memoireP partagée */ shmid = shmget(shm_key, sizeof(MEMP), 0766 | IPC_CREAT); - if (shmid == -1) { perror("shmget"); return (EXIT_FAILURE); } + if (shmid == -1) { perror("shmget"); exit(EXIT_FAILURE); } - if((memoireP = (MEMP *) shmat(shmid, 0 , 0766)) == (void *) -1) - { perror("shmat"); exit(EXIT_FAILURE); } - - if((mutex_data = creat_sem( sem_key_data, 1)) == -1) - { perror("creat_sem"); exit(EXIT_FAILURE); } + if((memoireP = (MEMP *) shmat(shmid, 0 , 0766)) ==(void *) -1) { perror("shmat"); exit(EXIT_FAILURE); } + if((mutex_data = open_sem( sem_key_data)) == -1) { perror("open_sem"); exit(EXIT_FAILURE); } + if((mutex_tpa = open_sem( sem_key_tpa)) == -1) { perror("open_sem"); exit(EXIT_FAILURE); } + if((mutex_glob = open_sem( sem_key_glob)) == -1) { perror("open_sem"); exit(EXIT_FAILURE); } - if((mutex_tpa = creat_sem( sem_key_tpa, 1)) == -1) - { perror("creat_sem"); exit(EXIT_FAILURE); } - - - P(mutex_data); - - V(mutex_data); + P(mutex_glob); + P(mutex_tpa); + for(i = 0; i < MAX_PROD && memoireP->tpa[i] != -1 ; i++); + V(mutex_tpa); -/* utilisation */ -// printf("memoireP[0] = %d\n", memoireP[0]++ ); + if(memoireP->tpa[i] != -1) { exit(EXIT_FAILURE); } + memoireP->tpa[i] = 0; + memoireP->tpa[i] = getpid(); + + while((c = getc(stdin)) != '0') + { + P(mutex_data); + if(((memoireP->queue -1) % MAX_BUF) != (memoireP->tete % MAX_BUF) ) + { + memoireP->f[memoireP->tete].c = c; + memoireP->f[memoireP->tete].idp = getpid(); + memoireP->tete = memoireP->tete +1; + } + + V(mutex_data); + } + P(mutex_tpa); + V(mutex_tpa); + V(mutex_glob); return (EXIT_SUCCESS); } diff --git a/C_Language/TP_SYS/tp_sema/sema.h b/C_Language/TP_SYS/tp_sema/sema.h index d0b4689..c4820f9 100644 --- a/C_Language/TP_SYS/tp_sema/sema.h +++ b/C_Language/TP_SYS/tp_sema/sema.h @@ -1,6 +1,9 @@ void erreur(char *s); int open_sem(key_t cle); +/* recherche le semaphore cle + retourne l'identificateur du semaphore >=0 ou -1 si erreur +*/ void P(int semid); void V(int semid); @@ -20,10 +23,3 @@ int del_sem(key_t cle); -1 si erreur */ -/* recherche le semaphore cle - retourne l'identificateur du semaphore >=0 ou -1 si erreur -*/ - - - -