1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "djikistra.h"
#define CLE 1
#define CL 0
char c;
int end=0;
int sem,sem1;
int ent;
void *fonc_lecture(){
char test;
for(;;){
P(sem1); //si le thread affichage est en execution, lecture se bloque*/
printf("\nRentrez un caratactere\n");
scanf("%c",&test);
c=test;
printf("c =%c\n",c);
if(c=='F'){
printf("la lettre F");
pthread_exit(NULL);
}
else{
//liberer la SC
V(sem);}
}
}
void *fonc_affichage(){
for(;;){
printf("\nje me bloque\n");
P(sem);
printf("\nje me débloque\n");
if(c=='F')
pthread_exit(NULL);
printf("%c",c);
//liberer la SC
printf("\nje débloque lecture");
V(sem1);
}
}
int main(void){
pthread_t p1;
pthread_t p2;
pthread_create(&p1,NULL,fonc_lecture,NULL);
pthread_create(&p2,NULL,fonc_affichage,NULL);
sem = sem_create(CLE,0);
printf("Creation du sémaphore %d \n",sem);
sem1= sem_create(CL,1);
pthread_join(p2,NULL);
pthread_join(p1,NULL);
sem_delete(sem);
return(0);
} |
Partager