-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.c
226 lines (174 loc) · 5.97 KB
/
test1.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
#include "declare.h"
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
//declare
pthread_mutex_t mutexTel, mutexRev, mutexMess, mutexCook, mutexOven, mutexDel;
pthread_cond_t condTel, condCook, condOven, condDel;
int av_tel = Ntel;
int av_cook = Ncook;
int av_oven = Noven;
int av_del = Ndeliverer;
int total_rev = 0;
int sales_marg = 0, sales_pepp = 0, sales_special = 0;
int success = 0;
int failed =0;
typedef struct {
unsigned int seed;
int id;
struct timespec start_time;
} Thread_args;
void* manage_order(void* args) {
Thread_args* arg = (Thread_args*)args;
int id = arg->id;
int seed = arg->seed;
struct timespec start_time = arg->start_time;
free(arg);
int pitses = rand_r(&seed) % (Norderhigh - Norderlow + 1 ) + Norderlow;
//telephone
pthread_mutex_lock(&mutexTel);
while (av_tel == 0) {
pthread_cond_wait(&condTel, &mutexTel);
}
av_tel--;
printf("tilefona\n");
pthread_mutex_unlock(&mutexTel);
//number of pizzas;
int special =0;
int marg = 0;
int pep = 0;
for (int i =0; i < pitses; i++) {
int prob = rand_r(&seed) % 100;
if (prob < Pm) marg++;
else if (prob < Pm + Pp) pep++;
else special++;
}
//payment
int pay_time = rand_r(&seed) % (Tpaymenthigh - Tpaymentlow + 1) + Tpaymentlow;
sleep(pay_time);
int is_failure = rand_r(&seed) % 100;
if (is_failure < Pfail) {
pthread_mutex_lock(&mutexMess);
printf("Order number %d failed\n", id);
pthread_mutex_unlock(&mutexMess);
pthread_mutex_lock(&mutexTel);
av_tel++;
pthread_cond_signal(&condTel);
pthread_mutex_unlock(&mutexTel);
pthread_mutex_lock(&mutexRev);
failed++;
pthread_mutex_unlock(&mutexRev);
return NULL; // gia na stamtisei h sunarthsh
//pthread_exit(NULL);
}
//calculate revenue
int order_revenue = marg*Cm + pep*Cp + special*Cs;
pthread_mutex_lock(&mutexRev);
total_rev += order_revenue;
sales_marg += marg;
sales_special += special;
sales_pepp += pep;
success++;
pthread_mutex_unlock(&mutexRev);
pthread_mutex_lock(&mutexMess);
printf("Order with number %d succeeeded: %d margherita, %d pepperoni, %d special\n", id, marg, pep, special);
pthread_mutex_unlock(&mutexMess);
pthread_mutex_lock(&mutexTel);
av_tel++;
printf("mutexTEL\n");
pthread_cond_signal(&condTel);
pthread_mutex_unlock(&mutexTel);
//cooking
pthread_mutex_lock(&mutexCook);
while (av_cook == 0) {
pthread_cond_wait(&condCook, &mutexCook);
}
av_cook--;
pthread_mutex_unlock(&mutexCook);
sleep(Tprep * pitses);
pthread_mutex_lock(&mutexCook);
av_cook++;
pthread_cond_signal(&condCook);
pthread_mutex_unlock(&mutexCook);
pthread_mutex_lock(&mutexOven);
while (av_oven < pitses) {
pthread_cond_wait(&condOven, &mutexOven);
}
av_oven -= pitses;
pthread_mutex_unlock(&mutexOven);
sleep(Tbake);
pthread_mutex_lock(&mutexOven);
av_oven += pitses;
pthread_cond_broadcast(&condOven);
pthread_mutex_unlock(&mutexOven);
// Delivery
pthread_mutex_lock(&mutexDel);
while (av_del == 0) {
pthread_cond_wait(&condDel, &mutexDel);
}
av_del--;
pthread_mutex_unlock(&mutexDel);
sleep(Tpack * pitses);
int delivery_time = rand_r(&seed) % (Tdelhigh - Tdellow + 1) + Tdellow;
sleep(delivery_time);
struct timespec end_time;
clock_gettime(CLOCK_REALTIME, &end_time);
int total_time = end_time.tv_sec - start_time.tv_sec;
pthread_mutex_lock(&mutexMess);
printf("Order with number %d delivered in %d minutes\n", id, total_time);
pthread_mutex_unlock(&mutexMess);
pthread_mutex_lock(&mutexDel);
av_del++;
pthread_cond_signal(&condDel);
pthread_mutex_unlock(&mutexDel);
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: %s <number_of_customers> <seed>\n", argv[0]);
return -1;
}
int Ncust = atoi(argv[1]);
int seed = atoi(argv[2]);
pthread_t threads[Ncust];
pthread_mutex_init(&mutexTel, NULL);
pthread_mutex_init(&mutexRev, NULL);
pthread_mutex_init(&mutexMess, NULL);
pthread_mutex_init(&mutexCook, NULL);
pthread_mutex_init(&mutexOven, NULL);
pthread_mutex_init(&mutexDel, NULL);
pthread_cond_init(&condTel, NULL);
pthread_cond_init(&condCook, NULL);
pthread_cond_init(&condOven, NULL);
pthread_cond_init(&condDel, NULL);
for (int i = 0; i < Ncust; i++) {
Thread_args* args = malloc(sizeof(Thread_args));
args->id = i + 1;
args->seed = seed +1;
clock_gettime(CLOCK_REALTIME, &args->start_time);
pthread_create(&threads[i], NULL, manage_order, args);
sleep(rand() % (Torderhigh - Torderlow + 1) + Torderlow);
}
for (int i = 0; i < Ncust; i++) {
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(&mutexTel);
pthread_mutex_destroy(&mutexRev);
pthread_mutex_destroy(&mutexMess);
pthread_mutex_destroy(&mutexCook);
pthread_mutex_destroy(&mutexOven);
pthread_mutex_destroy(&mutexDel);
pthread_cond_destroy(&condTel);
pthread_cond_destroy(&condCook);
pthread_cond_destroy(&condOven);
pthread_cond_destroy(&condDel);
printf("Total revenue: %d euros\n", total_rev);
printf("Margherita pizzas sold: %d\n", sales_marg);
printf("Pepperoni pizzas sold: %d\n", sales_pepp);
printf("Special pizzas sold: %d\n", sales_special);
printf("Successful orders: %d\n", success);
printf("Failed orders: %d\n", failed);
return 0;
}