Début avec la bibliothèque ncurses

This commit is contained in:
Philippe Pittoli 2011-03-20 17:21:34 +01:00
parent 200957af25
commit fb06534b3f
4 changed files with 106 additions and 40 deletions

View File

@ -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)

View File

@ -10,25 +10,27 @@
#include <errno.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <curses.h>
#include <ctype.h>
#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 ;
}

View File

@ -1,3 +1,4 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@ -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);
}

View File

@ -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
*/