-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
420 lines (351 loc) · 10.3 KB
/
main.c
File metadata and controls
420 lines (351 loc) · 10.3 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* @author Matus Hubinsky xhubin04
* @date 26.04.2022
* @brief program for the post office problem inspired by Allen B. Downey: The Little Book of Semaphores (The barbershop problem)
* @file main.c
*/
#include <math.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <signal.h>
#include <semaphore.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "main.h"
int nz; // number of customers
int nu; // number of officials
int tz; // the maximum waiting time that customers waits
int tu; // the maximum length of the official's break time
int f; // the maximum time in miliseconds
FILE* output; // output file
sem_t* xhubin04_semaphore_letter = NULL; // letter queue semaphore
sem_t* xhubin04_semaphore_package = NULL; // package queue semaphore
sem_t* xhubin04_semaphore_money = NULL; // money queue semaphore
sem_t* xhubin04_semaphore_mutex = NULL; // mutex semaphore
sem_t* xhubin04_semaphore_write = NULL; // semaphore for writing to output file
int* queue_letter = NULL; // number of customers in letter queue
int* queue_package = NULL; // number of customers in package queue
int* queue_money = NULL; // number of customers in money queue
int* line_number = NULL; // number of current line
bool* post_office = NULL; // post office status (true - open, false - closed)
/*
* @brief open all semaphores
* @param
* @returns
*/
static void semaphores_open_all() {
if ((xhubin04_semaphore_letter = sem_open("xhubin04_semaphore_letter", O_CREAT | O_EXCL, 0664, 0)) == SEM_FAILED){
fprintf(stderr, "sem_open: xhubin04_semaphore_letter\n");
exit(1);
}
if ((xhubin04_semaphore_package = sem_open("/xhubin04_semaphore_package", O_CREAT | O_EXCL, 0664, 0)) == SEM_FAILED){
fprintf(stderr, "sem_open: xhubin04_semaphore_package\n");
exit(1);
}
if ((xhubin04_semaphore_money = sem_open("/xhubin04_semaphore_money", O_CREAT | O_EXCL, 0664, 0)) == SEM_FAILED){
fprintf(stderr, "sem_open: xhubin04_semaphore_money\n");
exit(1);
}
if ((xhubin04_semaphore_mutex = sem_open("/xhubin04_semaphore_mutex", O_CREAT | O_EXCL, 0664, 1)) == SEM_FAILED){
fprintf(stderr, "sem_open: xhubin04_semaphore_mutex\n");
exit(1);
}
if ((xhubin04_semaphore_write = sem_open("/xhubin04_semaphore_write", O_CREAT | O_EXCL, 0664, 1)) == SEM_FAILED){
fprintf(stderr, "sem_open: xhubin04_semaphore_write\n");
exit(1);
}
}
/*
* @brief close all semaphores
* @param
* @return
*/
static void semaphores_close_all() {
if (sem_close(xhubin04_semaphore_letter) == -1) {
fprintf(stderr, "sem_open: xhubin04_semaphore_letter\n");
exit(1);
}
if (sem_close(xhubin04_semaphore_package) == -1) {
fprintf(stderr, "sem_open: xhubin04_semaphore_package\n");
exit(1);
}
if (sem_close(xhubin04_semaphore_money) == -1) {
fprintf(stderr, "sem_open: xhubin04_semaphore_money\n");
exit(1);
}
if (sem_close(xhubin04_semaphore_mutex) == -1) {
fprintf(stderr, "sem_close: xhubin04_semaphore_mutex\n");
exit(1);
}
if (sem_close(xhubin04_semaphore_write) == -1) {
fprintf(stderr, "sem_close: xhubin04_semaphore_write\n");
exit(1);
}
}
/*
* @brief unlink all semaphores
* @param
* @return
*/
static void semaphores_unlink_all() {
sem_unlink("xhubin04_semaphore_letter");
sem_unlink("xhubin04_semaphore_package");
sem_unlink("xhubin04_semaphore_money");
sem_unlink("/xhubin04_semaphore_mutex");
sem_unlink("/xhubin04_semaphore_write");
}
/*
* @brief initialize shared memory
* @param
* @return
*/
static void mmap_init() {
queue_letter = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
queue_package = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
queue_money = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
line_number = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
post_office = mmap(NULL, sizeof(bool), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
}
/*
* @brief unlink shared memory
* @param
* @return
*/
static void mmap_unlink() {
munmap(queue_letter, sizeof(int));
munmap(queue_package, sizeof(int));
munmap(queue_money, sizeof(int));
munmap(line_number, sizeof(int));
munmap(post_office, sizeof(bool));
}
/*
* @brief close semaphore for writing, write message and open semaphore
* @param format string, same as printf
* @return
*/
static void write_to_file(char *format, ...) {
sem_wait(xhubin04_semaphore_write);
va_list args;
va_start(args, format);
fprintf(output, "%d: ", (*line_number)++);
vfprintf(output, format, args);
fflush(output);
va_end(args);
sem_post(xhubin04_semaphore_write);
}
/*
* @brief check if the is any customer in queues, lock memory while checking state
* @paran
* @return true if the are customers in queues, false if not
*/
static bool customers_in_queue() {
bool result = false;
if ((*queue_letter) || (*queue_package) || (*queue_money)) {
result = true;
} else {
result = false;
}
return result;
}
/*
* @brief process customer
* @param id customer's id
* @return
*/
void customer(int id) {
srand(time(0) ^ getpid());
write_to_file("Z %d: started\n", id);
usleep((rand() % (tz + 1)) * 1000);
sem_wait(xhubin04_semaphore_mutex);
if (*post_office) {
int queue_number = rand() % 3 + 1;
write_to_file("Z %d: entering office for a service %d\n", id, queue_number);
if (queue_number == 1) {
(*queue_letter)++;
sem_post(xhubin04_semaphore_mutex);
sem_wait(xhubin04_semaphore_letter);
} else if (queue_number == 2) {
(*queue_package)++;
sem_post(xhubin04_semaphore_mutex);
sem_wait(xhubin04_semaphore_package);
} else if (queue_number == 3) {
(*queue_money)++;
sem_post(xhubin04_semaphore_mutex);
sem_wait(xhubin04_semaphore_money);
}
write_to_file("Z %d: called by office worker\n", id);
usleep((rand() % 11) * 1000);
} else {
sem_post(xhubin04_semaphore_mutex);
}
write_to_file("Z %d: going home\n", id);
}
/*
* @brief process official
* @param id official's id
* @return
*/
void official(int id) {
srand(time(0) ^ getpid());
write_to_file("U %d: started\n", id);
start:
sem_wait(xhubin04_semaphore_mutex);
if (customers_in_queue()) {
int pick = -1;
int value = 0;
do {
pick = rand() % 3 + 1;
if (pick == 1) {
value = *queue_letter;
} else if (pick == 2) {
value = *queue_package;
} else {
value = *queue_money;
}
} while (value <= 0);
if (pick == 1) {
(*queue_letter)--;
} else if (pick == 2) {
(*queue_package)--;
} else {
(*queue_money)--;
}
if (pick == 1) {
sem_post(xhubin04_semaphore_letter);
} else if (pick == 2) {
sem_post(xhubin04_semaphore_package);
} else {
sem_post(xhubin04_semaphore_money);
}
sem_post(xhubin04_semaphore_mutex);
write_to_file("U %d: serving a service of type %d\n", id, pick);
usleep((rand() % 11) * 1000);
write_to_file("U %d: service finished\n", id);
goto start;
}
else if (*post_office) {
write_to_file("U %d: taking break\n", id);
sem_post(xhubin04_semaphore_mutex);
usleep((rand() % (tu + 1)) * 1000);
write_to_file( "U %d: break finished\n", id);
goto start;
}
else {
sem_post(xhubin04_semaphore_mutex);
write_to_file("U %d: going home\n", id);
exit(0);
}
}
/*
* @brief check if string is number
* @param string pointer to string
* @return true when the string was a number, false otherwise
*/
static bool is_number(char *string) {
int number;
number = strtol(string, &string, 10);
if (*string == '\0') {
number++;
return true;
}
else {
number++;
return false;
}
}
/*
* @brief check all argumetns, assign values to variables, check if arguments are correct
* print error message and exit with code 1 if arguments are not correct
* @param argc nubmer of arguments
* @param argv string array of arguments
* @return
*/
static void process_arguments(int argc, char *argv[]) {
// check number of arguments
if (argc != ARG_NUM) {
fprintf(stderr, "Error: Wrong number of argments!\n");
exit(1);
}
// assing arguments values
nz = atoi(argv[1]);
nu = atoi(argv[2]);
tz = atoi(argv[3]);
tu = atoi(argv[4]);
f = atoi(argv[5]);
// check if arguments are correct, they must be bigger than 0
if (nz < -1 || nu < 1) {
fprintf(stderr, "Error: Wrong number of people!\n");
exit(1);
}
// check if arguments are numbers and not strings
if (!is_number(argv[1]) || !is_number(argv[2]) || !is_number(argv[3]) || !is_number(argv[4]) || !is_number(argv[5])) {
fprintf(stderr, "Error: Arguments are not numbers!\n");
exit(1);
}
// check if waiting times are correct, it must be bigger than 0 and lower than 1000
if ((tz < 0 || tz > 10000) || (tu < 0 || tu > 100) || (f < 0 || f > 1000)) {
fprintf(stderr, "Error: Wrong waiting time!\n");
exit(1);
}
}
int main(int argc, char *argv[]) {
// process all arguments and assign values to variables
process_arguments(argc, argv);
output = fopen("proj2.out", "w");
if (output == NULL) {
fprintf(stderr, "File can't be created\n");
exit(1);
}
// open all semaphores
semaphores_open_all();
// map all shared variables
mmap_init();
// initialize all shared variables
*line_number = 1;
*post_office = true;
// fork all customers
for (int i = 0; i < nz; i++) {
pid_t customer_pid = fork();
if (customer_pid == 0) {
customer(i + 1);
return 0;
} else if (customer_pid == -1) {
fprintf(stderr, "Error: can't fork customer\n");
return 1;
}
}
// fork all officials
for (int i = 0; i < nu; i++) {
pid_t official_pid = fork();
if (official_pid == 0) {
official(i + 1);
return 0;
} else if (official_pid == -1) {
fprintf(stderr, "Error: can't fork official\n");
return 1;
}
}
// go for sleep for random nubmer in range <f/2, f>, then close the post office
usleep(((f/2) + (rand() % (f/2 + 1))) * 1000);
sem_wait(xhubin04_semaphore_mutex);
*post_office = false;
write_to_file("closing\n");
sem_post(xhubin04_semaphore_mutex);
// wait for all children to die
while(wait(NULL) > 0);
// close all semaphores
semaphores_close_all();
// unlink all semaphores
semaphores_unlink_all();
// unmap all memory
mmap_unlink();
fclose(output);
return 0;
}