On arrive presque à faire quelque-chose
parent
fb06534b3f
commit
ed687937a7
|
@ -33,6 +33,17 @@ int main( int argc, char **argv)
|
|||
shm_key = (key_t) atoi(argv[1]);
|
||||
MEMP * memoireP;
|
||||
|
||||
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 */
|
||||
|
||||
if((shmid = shmget(shm_key, sizeof(MEMP), IPC_CREAT|IPC_EXCL|0766)) == -1)
|
||||
{ perror("shmget"); exit(EXIT_FAILURE);}
|
||||
|
||||
|
@ -61,34 +72,48 @@ int main( int argc, char **argv)
|
|||
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)
|
||||
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)
|
||||
{
|
||||
w= islower(c) ? f_haut : f_bas;
|
||||
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;
|
||||
|
||||
endwin() ;
|
||||
|
||||
// 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); }
|
||||
|
||||
|
@ -96,8 +121,13 @@ int main( int argc, char **argv)
|
|||
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
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
WINDOW *creation_fenetre(int n,int d,char *t)
|
||||
{
|
||||
|
|
|
@ -13,7 +13,7 @@ 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, mutex_glob, continuer = 0, i=0;
|
||||
int mutex_data, mutex_tpa, sem_global, continuer = 0, i=0;
|
||||
char c;
|
||||
MSG message;
|
||||
key_t sem_key_data = MUTEX_DATA;
|
||||
|
@ -28,9 +28,10 @@ int main( int argc, char **argv)
|
|||
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((sem_global = open_sem( sem_key_glob)) == -1) { perror("open_sem"); exit(EXIT_FAILURE); }
|
||||
|
||||
P(mutex_glob);
|
||||
// sem_global est un sémaphore à MAX_PROD entrants.
|
||||
P(sem_global);
|
||||
P(mutex_tpa);
|
||||
for(i = 0; i < MAX_PROD && memoireP->tpa[i] != -1 ; i++);
|
||||
V(mutex_tpa);
|
||||
|
@ -38,23 +39,25 @@ int main( int argc, char **argv)
|
|||
if(memoireP->tpa[i] != -1) { exit(EXIT_FAILURE); }
|
||||
memoireP->tpa[i] = 0;
|
||||
|
||||
memoireP->tpa[i] = getpid();
|
||||
|
||||
// On quitte le producteur en tappant 0 (zéro)
|
||||
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->f[memoireP->tete].idp = i;
|
||||
memoireP->tete = memoireP->tete +1;
|
||||
}
|
||||
|
||||
V(mutex_data);
|
||||
}
|
||||
// On a fini, on remet la valeur de l'indice du processus à -1 dans le tableau des producteurs
|
||||
P(mutex_tpa);
|
||||
memoireP->tpa[i] = -1;
|
||||
V(mutex_tpa);
|
||||
V(mutex_glob);
|
||||
|
||||
// On a fini, on libère l'accès à un autre producteur
|
||||
V(sem_global);
|
||||
return (EXIT_SUCCESS);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue