80 lines
1.8 KiB
C
80 lines
1.8 KiB
C
|
#include <sys/types.h>
|
||
|
#include <unistd.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <signal.h>
|
||
|
|
||
|
/*
|
||
|
//ce bloc de code vient d'un exercice de tp que j'ai recuperer
|
||
|
int set_signal_handler(int signo, void (*handler)(int)) {
|
||
|
struct sigaction sa;
|
||
|
sa.sa_handler = handler; // call `handler` on signal
|
||
|
sigemptyset(&sa.sa_mask); // don't block other signals in handler
|
||
|
sa.sa_flags = SA_RESTART; // restart system calls
|
||
|
return sigaction(signo, &sa, NULL);
|
||
|
}
|
||
|
|
||
|
void sig_hand(int sig) {
|
||
|
}*/
|
||
|
|
||
|
void commander_une_pizza(int* p){
|
||
|
int i;
|
||
|
|
||
|
printf("pizza ordered!\n");
|
||
|
for (i=1; i<=8; i++){
|
||
|
write(p[1],&i,sizeof(int));
|
||
|
}
|
||
|
printf("pizza delivered!\n");
|
||
|
//kill(getppid(), SIGUSR1);
|
||
|
}
|
||
|
|
||
|
|
||
|
void prendre_une_part_de_pizza(int numEtudiant, int* p){
|
||
|
int numPartPizza;
|
||
|
read(p[0],&numPartPizza,sizeof(int));
|
||
|
printf("student %d takes piece %d\n",numEtudiant, numPartPizza);
|
||
|
//rendre atomique cette partie (mais je n'y arrive pas)
|
||
|
if (numPartPizza == 8){
|
||
|
commander_une_pizza(p);
|
||
|
}
|
||
|
else{
|
||
|
//kill(getppid(), SIGUSR1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void reviser_et_manger_la_part_de_pizza(void){
|
||
|
sleep(3);
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(int argc, char ** argv){
|
||
|
int Nbetudiant;
|
||
|
int i;
|
||
|
int pid;
|
||
|
int p[2];
|
||
|
|
||
|
if (argc != 2) {
|
||
|
printf("Usage: %s <NbEtudiants>\n",argv[0]);
|
||
|
exit(1);
|
||
|
}
|
||
|
|
||
|
Nbetudiant = strtol(argv[1],NULL,0);
|
||
|
//set_signal_handler(SIGUSR1,sig_hand);
|
||
|
|
||
|
pipe(p);
|
||
|
commander_une_pizza(p);
|
||
|
|
||
|
for (i=0; i<Nbetudiant; i++){
|
||
|
pid = fork();
|
||
|
if (pid == 0){
|
||
|
while (1){
|
||
|
prendre_une_part_de_pizza(i, p);
|
||
|
reviser_et_manger_la_part_de_pizza();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
while (1){
|
||
|
// le professeur dort pendant que les eleves revises
|
||
|
sleep(1);
|
||
|
}
|
||
|
}
|