-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparc.c
228 lines (188 loc) · 7.06 KB
/
parc.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdbool.h>
//constante pour le nombre de threads clients max, la journée dans le parc, nb attractions
#define MAX_CLIENTS 100
#define MAX_ATTRACTIONS 10
//#define DUREE_JOURNEE 60
//gestion du temps
//time_t fin; // en seconde
//time_t debut;
pthread_mutex_t mutexEntree = PTHREAD_MUTEX_INITIALIZER;
typedef struct {
int numero;
int satisfaction;
int patience;
pthread_t thread;
} client;
typedef struct {
int numero;
bool libre;
int capacite;
int duree;
int clientIn;
int file;
sem_t semaphore;
} attraction;
attraction attractions[MAX_ATTRACTIONS];
int caisse = 0;
int caisseJour=0;
int nbClients;
int nbClientsIn = 0;
int nbClientsOut = 0;
int nbClientsInAllee = 0;
float satisfactionTotal = 0;
bool start = false;
void affichage(){
// system("clear");
printf("Client(s) dans le parc : %d\nFile d'attente d'entrée : %d\nNombre de clients dans les alléees : %d\n", nbClientsIn, nbClients-nbClientsIn-nbClientsOut, nbClientsInAllee);
for(int i=0; i<MAX_ATTRACTIONS; i++){
if (!attractions[i].libre){
printf("Attraction n°%d : %d clients / %d clients \nFile d'attente de l'attraction : %d clients\n",attractions[i].numero, attractions[i].clientIn,attractions[i].capacite, attractions[i].file);
}
else{
printf("Attraction n°%d : %d clients \n",attractions[i].numero, attractions[i].clientIn);
}
}
printf("-------------------------------\n");
}
void process_attraction(int idat, client* c) {
//printf("Je rentre\n");
if (attractions[idat].libre){
attractions[idat].clientIn++;
sleep(rand()%7);
attractions[idat].clientIn--;
}
else{
attractions[idat].file++;
time_t debutAttente = time(NULL);
sem_wait(&attractions[idat].semaphore);
// sleep(11);
time_t finAttente = time(NULL);
if (finAttente-debutAttente > c->patience){
// printf("Client n°%d : %d secondes d'attente pour rentrer dans l'attraction\n", c->numero, finAttente-debutAttente);
c->satisfaction -= 10;
// printf("Client n°%d : %d satisfaction\n", c->numero, c->satisfaction);
} else {
// printf("Client content n°%d : %d secondes d'attente pour rentrer dans l'attraction, patience %d\n", c->numero, finAttente-debutAttente, c->patience);
//hausse de la satisfaction du client uniquement si l'attente est beaucoup plus courte (2x) que son niveau de patience
if (finAttente-debutAttente < (c->patience/2)) {
c->satisfaction += 1;
if (c->satisfaction > 100) {
c->satisfaction = 100;
} else if (c->satisfaction < 0) {
c->satisfaction = 0;
}
}
// printf("Client content n°%d : %d satisfaction\n", c->numero, c->satisfaction);
}
attractions[idat].file--;
attractions[idat].clientIn++;
//printf("Attraction n° %d en cours\n", idat);
sleep(attractions[idat].duree);
//printf("Attraction n° %d finie\n", idat);
sem_post(&attractions[idat].semaphore);
attractions[idat].clientIn--;
}
}
void *process_client(void *arg) {
pthread_mutex_lock(&mutexEntree);
//printf("Je suis le client n° %d et je rentre dans le parc\n", ((client*)arg)->numero);
start = true;
caisseJour = caisseJour+30;
nbClientsIn++;
//printf("J'ai payé les 30 euros !\n");
pthread_mutex_unlock(&mutexEntree);
while (true){
int choix = rand()%3;
switch (choix)
{
case 0:
//printf("Je suis le client n° %d et je sors du parc\n", ((client*)arg)->numero);
nbClientsIn--;
nbClientsOut++;
pthread_exit(NULL);
break;
case 1: ;
int attractNum = rand()%MAX_ATTRACTIONS;
//printf("Je suis le client n° %d et je vais à l'attraction n° %d\n", ((client*)arg)->numero, attractNum);
process_attraction(attractNum, (client*)arg);
//printf("Je suis le client n° %d et je sors à l'attraction n° %d\n", ((client*)arg)->numero, attractNum);
//Passe en suite dans l'allée à la fin de son attraction
case 2: ;
nbClientsInAllee++;
int howLong = rand()%5;
// printf("Je suis le client n° %d et je reste dans l'allée %d secondes\n", ((client*)arg)->numero, howLong);
sleep(howLong);
nbClientsInAllee--;
break;
}
}
}
int main(int argc, char const *argv[]) {
char prochainJour = 'n';
srand(time(NULL));
nbClients = rand()%MAX_CLIENTS+10;
int nbJour = 0;
client clients[nbClients];
// fin = time(NULL) + 180;
// debut = time(NULL);
//initialisation des attractions et des clients
for (int i = 0; i < MAX_ATTRACTIONS; i++) {
attractions[i].numero = i;
attractions[i].libre = rand()%2;
attractions[i].capacite = rand()%10+1;
attractions[i].duree = rand()%10+1;
attractions[i].clientIn = 0;
sem_init(&attractions[i].semaphore, 0, attractions[i].capacite);
}
//initialisation des clients
for (int i = 0; i < nbClients; i++) {
// sleep(0.01);
clients[i].numero = i;
clients[i].satisfaction = 100;
clients[i].patience = rand()%10+1;
}
do {
nbJour++;
caisseJour = 0;
//seul les clients satisfaits reviennent, s'ils ont une satisfaction < 60, ils ne reviendront pas
for (int i = 0; i < nbClients; i++) {
if (clients[i].satisfaction > 50) {
pthread_create(&clients[i].thread, NULL, &process_client, &clients[i]);
} else {
printf("Client n°%d ne reviendra pas : satisfaction : %d\n", clients[i].numero, clients[i].satisfaction);
}
}
//fonction d'affichage
while (nbClientsIn > 0 || !start) {
affichage();
sleep(1);
// debut = time(NULL);
}
for (int i = 0; i < nbClients; i++) {
pthread_join(clients[i].thread, NULL);
}
affichage();
printf("Nombre de clients aujourd'hui: %d\n", nbClients);
caisse= caisse+caisseJour;
nbClientsIn = 0;
nbClientsOut = 0;
nbClientsInAllee = 0;
start = false;
satisfactionTotal = 0;
for (int i = 0; i < nbClients; i++) {
// sleep(0.01);
satisfactionTotal += clients[i].satisfaction;
}
printf("Fin journée n° %d, Chiffre d'affaires du jour : %d €\n", nbJour, caisseJour);
printf("Satisfaction moyenne : %f\n", satisfactionTotal/(float)nbClients);
printf("Jour suivant ? (o/n) \n");
scanf(" %c", &prochainJour);
} while (prochainJour == 'o');
printf("Caisse du parc au bout de %d jour(s): %d € \n",nbJour, caisse);
return 0;
}