2011-03-17 16:37:36 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2011-03-19 12:29:52 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/sem.h>
|
2011-03-20 01:03:54 +01:00
|
|
|
#include <sys/shm.h>
|
2011-03-20 17:21:34 +01:00
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
#include <ctype.h>
|
2011-03-19 12:29:52 +01:00
|
|
|
#include "consommateur.h"
|
2011-03-17 16:57:53 +01:00
|
|
|
#include "types.h"
|
2011-03-19 12:29:52 +01:00
|
|
|
#include "sema.h"
|
|
|
|
#include "constantes.h"
|
2011-03-17 16:37:36 +01:00
|
|
|
|
2011-03-20 17:21:34 +01:00
|
|
|
WINDOW *creation_fenetre(int n,int d,char *t);
|
|
|
|
|
2011-03-17 16:37:36 +01:00
|
|
|
int main( int argc, char **argv)
|
|
|
|
{
|
2011-03-20 01:03:54 +01:00
|
|
|
if(argc < 2) { printf("Usage : %s nSHM \n", argv[0]); exit(EXIT_FAILURE); }
|
2011-03-19 12:29:52 +01:00
|
|
|
|
2011-03-20 17:21:34 +01:00
|
|
|
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;
|
2011-03-19 12:29:52 +01:00
|
|
|
|
2011-03-20 17:21:34 +01:00
|
|
|
shm_key = (key_t) atoi(argv[1]);
|
2011-03-20 01:03:54 +01:00
|
|
|
MEMP * memoireP;
|
2011-03-17 17:31:26 +01:00
|
|
|
|
2011-03-20 23:27:26 +01:00
|
|
|
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 */
|
|
|
|
|
2011-03-20 01:03:54 +01:00
|
|
|
if((shmid = shmget(shm_key, sizeof(MEMP), IPC_CREAT|IPC_EXCL|0766)) == -1)
|
|
|
|
{ perror("shmget"); exit(EXIT_FAILURE);}
|
|
|
|
|
|
|
|
if((memoireP = (MEMP *) shmat(shmid, 0 , 0766)) == (void *) -1)
|
|
|
|
{ perror("shmat"); exit(EXIT_FAILURE); }
|
|
|
|
|
2011-03-20 01:21:44 +01:00
|
|
|
if((mutex_data = creat_sem( sem_key_data, 1)) == -1)
|
2011-03-20 01:03:54 +01:00
|
|
|
{ perror("creat_sem"); exit(EXIT_FAILURE); }
|
|
|
|
|
2011-03-20 01:21:44 +01:00
|
|
|
if((mutex_tpa = creat_sem( sem_key_tpa, 1)) == -1)
|
2011-03-20 01:03:54 +01:00
|
|
|
{ perror("creat_sem"); exit(EXIT_FAILURE); }
|
|
|
|
|
2011-03-20 17:21:34 +01:00
|
|
|
if((mutex_glob = creat_sem( sem_key_glob, 1)) == -1)
|
|
|
|
{ perror("creat_sem"); exit(EXIT_FAILURE); }
|
|
|
|
|
2011-03-20 01:03:54 +01:00
|
|
|
P(mutex_data);
|
|
|
|
|
|
|
|
memoireP->tete = 0;
|
|
|
|
memoireP->queue = 0;
|
2011-03-20 17:21:34 +01:00
|
|
|
|
2011-03-20 01:03:54 +01:00
|
|
|
V(mutex_data);
|
|
|
|
|
2011-03-20 01:21:44 +01:00
|
|
|
P(mutex_tpa);
|
2011-03-20 17:21:34 +01:00
|
|
|
for( i = 0; i < MAX_PROD ; i++)
|
2011-03-20 01:03:54 +01:00
|
|
|
memoireP->tpa[i] = -1;
|
2011-03-20 01:21:44 +01:00
|
|
|
V(mutex_tpa);
|
2011-03-20 01:03:54 +01:00
|
|
|
|
|
|
|
|
2011-03-20 17:21:34 +01:00
|
|
|
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") ;
|
|
|
|
|
2011-03-20 23:27:26 +01:00
|
|
|
sleep(5);
|
|
|
|
// aDesProd => est-ce qu'il y a des producteur (sinon on quitte)
|
|
|
|
// si = 0 alors on continue
|
|
|
|
int aDesProd;
|
|
|
|
int numTete = 0;
|
|
|
|
|
|
|
|
while (1)
|
2011-03-20 17:21:34 +01:00
|
|
|
{
|
2011-03-20 23:27:26 +01:00
|
|
|
aDesProd = 0;
|
|
|
|
P(mutex_data);
|
|
|
|
if(numTete != memoireP->tete)
|
|
|
|
{
|
|
|
|
numTete = memoireP->tete;
|
|
|
|
c = memoireP->f[numTete].c;
|
|
|
|
switch(memoireP->f[numTete].idp)
|
|
|
|
{
|
|
|
|
case 0 : w = f_haut;
|
|
|
|
break;
|
|
|
|
case 1 : w = f_milieu1;
|
|
|
|
break;
|
|
|
|
case 2 : w = f_milieu2;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
w = f_bas;
|
|
|
|
}
|
|
|
|
waddch(w,c) ;
|
|
|
|
wrefresh(w) ;
|
|
|
|
}
|
|
|
|
V(mutex_data);
|
|
|
|
// On vérifie qu'il y ait toujours des producteurs
|
|
|
|
for(i = 0 ; i < MAX_PROD && aDesProd == 0 ; i++ )
|
|
|
|
if(memoireP->tpa[i] != -1)
|
|
|
|
aDesProd = 1;
|
|
|
|
|
|
|
|
// S'il n'y a plus de producteurs, on quitte
|
|
|
|
if(aDesProd == 0)
|
|
|
|
{
|
|
|
|
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); }
|
|
|
|
|
|
|
|
endwin() ;
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
sleep(1); // Ralentissement volontaire du programme
|
2011-03-20 17:21:34 +01:00
|
|
|
}
|
2011-03-20 23:27:26 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2011-03-17 16:37:36 +01:00
|
|
|
}
|
|
|
|
|
2011-03-20 17:21:34 +01:00
|
|
|
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 ;
|
|
|
|
}
|