2023-04-13 00:18:35 +02:00
|
|
|
#include "userlib/syscall.h"
|
|
|
|
#include "userlib/libnachos.h"
|
|
|
|
|
|
|
|
|
|
|
|
const int N = 3;
|
|
|
|
int iplein = 0;
|
|
|
|
int ivide = 0;
|
|
|
|
int tab[3];
|
|
|
|
SemId svide;
|
|
|
|
SemId splein;
|
|
|
|
|
|
|
|
void producteur() {
|
|
|
|
for(int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
n_printf("batir une information\n");
|
|
|
|
P(svide);
|
|
|
|
iplein = (iplein + 1) % N;
|
2023-05-07 16:56:38 +02:00
|
|
|
// n_printf("communique une information : %d\n", i);
|
2023-04-13 00:18:35 +02:00
|
|
|
tab[iplein] = i;
|
|
|
|
V(splein);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void consommateur() {
|
2023-05-09 23:24:43 +02:00
|
|
|
int sum = 0;
|
2023-04-13 00:18:35 +02:00
|
|
|
for(int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
P(splein);
|
|
|
|
ivide = (ivide +1) % N;
|
|
|
|
n_printf("recevoir une information\n");
|
|
|
|
int info = tab[ivide];
|
|
|
|
V(svide);
|
2023-05-09 23:24:43 +02:00
|
|
|
sum += info;
|
2023-05-07 16:56:38 +02:00
|
|
|
// n_printf("exploiter l'information : %d\n", info);
|
2023-04-13 00:18:35 +02:00
|
|
|
}
|
2023-05-09 23:24:43 +02:00
|
|
|
Exit(sum);
|
2023-04-13 00:18:35 +02:00
|
|
|
}
|
2023-05-07 16:02:48 +02:00
|
|
|
|
|
|
|
int main() {
|
|
|
|
svide = SemCreate("producteur", N);
|
|
|
|
splein = SemCreate("consommateur", 0);
|
|
|
|
ThreadId producteurTh = threadCreate("producteur", producteur);
|
|
|
|
ThreadId consommateurTh = threadCreate("consommateur", consommateur);
|
|
|
|
Join(producteurTh);
|
|
|
|
Join(consommateurTh);
|
2023-05-07 16:56:38 +02:00
|
|
|
Shutdown();
|
2023-05-07 16:02:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|