-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj2.c
289 lines (254 loc) · 7.38 KB
/
proj2.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <semaphore.h>
#include <time.h>
#include <sys/mman.h>
#include <stdarg.h>
#define OK_CODE 0
#define ERR_CODE 1
#define POCET_ARG 5
#define OUTPUT_FILE "proj2.out"
#define MAX_SKIERS 20000
#define MAX_BUS_STOPS 10
#define MAX_BUS_CAPACITY 100
#define MAX_SKIER_WAIT 10000
#define MAX_BUS_DELAY 1000
typedef struct
{
int stop_count;
int counter;
int in_bus;
int skiing;
int *at_stop;
sem_t bus;
sem_t *stop_sem;
sem_t action_mutex;
sem_t bus_finished;
sem_t bus_can_leave;
FILE *log_file;
} SharedData;
SharedData *data;
int init_resources(int Z)
{
data = (SharedData *)mmap(NULL, sizeof(SharedData), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
data->at_stop = (int *)mmap(NULL, sizeof(int) * Z, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
data->stop_sem = (sem_t *)mmap(NULL, sizeof(sem_t) * Z, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
data->stop_count = Z;
data->counter = 0;
data->skiing = 0;
data->in_bus = 0;
if (data == MAP_FAILED ||
data->at_stop == MAP_FAILED ||
data->stop_sem == MAP_FAILED)
{
fprintf(stderr, "Error creating shared mem\n");
return ERR_CODE;
}
sem_init(&data->bus, 1, 0); // post first
for (int i = 0; i < Z; i++)
{
sem_init(&data->stop_sem[i], 1, 0); // post first
}
sem_init(&data->action_mutex, 1, 1);
sem_init(&data->bus_finished, 1, 0); // post first
sem_init(&data->bus_can_leave, 1, 0); // post first
data->log_file = fopen(OUTPUT_FILE, "w");
if (data->log_file == NULL)
{
fprintf(stderr, "Error opening log file for write\n");
return ERR_CODE;
}
setbuf(data->log_file, NULL);
return OK_CODE;
}
void sem_destroy_all()
{
// rip bozo 🚦s ya wouldn't be missed
sem_destroy(&data->bus);
for (int i = 0; i < data->stop_count; i++)
{
sem_destroy(&data->stop_sem[i]);
}
sem_destroy(&data->action_mutex);
sem_destroy(&data->bus_finished);
sem_destroy(&data->bus_can_leave);
}
void free_all()
{
sem_destroy_all();
fclose(data->log_file);
munmap(data->at_stop, sizeof(int) * data->stop_count);
munmap(data->stop_sem, sizeof(sem_t) * data->stop_count);
munmap(data, sizeof(SharedData));
}
void log_action(const char *format, ...)
{
sem_wait(&data->action_mutex);
va_list args;
va_start(args, format);
char new_format[256];
sprintf(new_format, "%d: %s\n", ++data->counter, format);
vfprintf(data->log_file, new_format, args);
fflush(data->log_file);
va_end(args);
sem_post(&data->action_mutex);
}
void inc_stop(int stop_id)
{
sem_wait(&data->action_mutex);
data->at_stop[stop_id]++;
sem_post(&data->action_mutex);
}
void dec_stop(int stop_id)
{
sem_wait(&data->action_mutex);
data->at_stop[stop_id]--;
sem_post(&data->action_mutex);
}
int get_random_from_range(int min, int max)
{
srand(time(NULL) + getpid()); // set random seed
int t = rand() % (max + 1 - min) + min;
return t;
}
void skier(int id, int Z, int TL)
{
log_action("L %d: started", id);
usleep(get_random_from_range(0, TL)); // chalky
int stop_id = get_random_from_range(0, Z - 1);
int stop_no = stop_id + 1;
log_action("L %d: arrived to %d", id, stop_no);
inc_stop(stop_id); // blud starts waiting for da bus
// wait till bus arrivez
sem_wait(&data->stop_sem[stop_id]);
// bus here
log_action("L %d: boarding", id);
dec_stop(stop_id);
data->in_bus++;
sem_post(&data->bus); // boarded
// wait till bus reach final
sem_wait(&data->bus_finished);
log_action("L %d: going to ski", id);
// he is done
data->in_bus--;
data->skiing++;
sem_post(&data->bus_can_leave);
exit(0);
}
void bus(int Z, int K, int TB, int L)
{
log_action("BUS: started");
for (int i = 0; i < Z; i++)
{
int stop_no = i + 1;
// slow aaaaah bus
usleep(get_random_from_range(0, TB));
// 🚌 🔥
log_action("BUS: arrived to %d", stop_no);
sem_wait(&data->action_mutex);
int on_stop = data->at_stop[i];
sem_post(&data->action_mutex);
int ppl_to_board = (on_stop > K) ? K : on_stop;
// lets not overfill the bus 💀
if (data->in_bus + ppl_to_board > K)
{
ppl_to_board = K - data->in_bus;
}
// BUS DRIVER: 🗣️ get in
for (int bro = 0; bro < ppl_to_board; bro++)
{
sem_post(&data->stop_sem[i]); // tell bro to board
// BUS DRIVER is the 🐐, he waits for all till they board
sem_wait(&data->bus); // wait
}
// BUS: bye bye 🤫🧏♂️
log_action("BUS: leaving %d", stop_no);
// another stop or nah?
if (stop_no < Z)
continue;
// slow bus frfr
usleep(get_random_from_range(0, TB));
log_action("BUS: arrived to final");
// BUS DRIVER: 🗣️ f off 😡💢
int ppl_to_leave = data->in_bus;
for (int xd = 0; xd < ppl_to_leave; xd++)
{
sem_post(&data->bus_finished);
sem_wait(&data->bus_can_leave);
}
log_action("BUS: leaving final");
if (data->skiing != L)
{
// ah 💩, here we go again
i = -1;
continue;
}
}
log_action("BUS: finish");
exit(0);
}
int main(int argc, char *argv[])
{
// ඞ sussy baka
if (argc != POCET_ARG + 1) // +1 name
{
fprintf(stderr, "Error: Invalid arguments\n");
fprintf(stderr, "Run like this: $ %s L Z K TL TB\n", argv[0]);
return ERR_CODE;
}
int L = strtol(argv[1], NULL, 10);
int Z = strtol(argv[2], NULL, 10);
int K = strtol(argv[3], NULL, 10);
int TL = strtol(argv[4], NULL, 10);
int TB = strtol(argv[5], NULL, 10);
if (
L < 0 || L > MAX_SKIERS || // L<20000
Z <= 0 || Z > MAX_BUS_STOPS || // 0<Z<=10
K < 10 || K > MAX_BUS_CAPACITY || // 10<=K<=100
TL < 0 || TL > MAX_SKIER_WAIT || // 0<=TL<=10000
TB < 0 || TB > MAX_BUS_DELAY // 0<=TB<=1000
)
{
fprintf(stderr, "Error: Argument values out of range\n");
return ERR_CODE;
}
if (init_resources(Z) != OK_CODE)
return ERR_CODE;
pid_t pid = fork(); // skibidibus
if (pid < 0)
{
fprintf(stderr, "Error: skibus fork failed\n");
free_all();
return ERR_CODE;
}
if (pid == 0)
{
bus(Z, K, TB, L);
}
// skiers
for (int i = 0; i < L; i++)
{
pid = fork();
if (pid < 0)
{
fprintf(stderr, "Error: %d skier fork failed\n", i + 1);
free_all();
return ERR_CODE;
}
if (pid == 0)
{
skier(i + 1, Z, TL);
}
}
// wait till all skiers and bus are done
for (int i = 0; i < L + 1; i++)
{
wait(NULL);
}
// 💀 kill all sems, shared mem etc.
free_all();
return OK_CODE;
}
// skibidi 🚽