-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
91 lines (73 loc) · 1.93 KB
/
main.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
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "multivisit.h"
// width of the output box
const int width = 9;
// output control
pthread_mutex_t mut;
// enter to rooms
pthread_mutex_t mut_a;
pthread_mutex_t mut_b;
pthread_cond_t cond_a;
pthread_cond_t cond_b;
int freeA, freeB;
// control over total amount of visitors
pthread_mutex_t mut_out;
pthread_cond_t cond_out;
int total_allowed;
// tracking the visitors
int Na, Nb;
int *a, *b;
int to_enter;
int main(int argc, char **argv) {
srand(time(NULL));
to_enter = 1000;
// CLI arguments
if (argc > 2) {
Na = atoi(argv[1]);
Nb = atoi(argv[2]);
if (argc > 3)
to_enter = atoi(argv[3]);
}
else {
Na = 63, Nb = 18;
}
// check if arguments are okay
if (Na <= Nb) {
printf("Capacity A <= capacity B\n");
return 1;
}
else if (to_enter > 1000) {
printf("Maximum supported number of threads: 1000");
return 1;
}
// reserve one place for B visitors to leave in shortst time
total_allowed = Na + Nb - 1, freeA = Na, freeB = Nb;
// initialize arrays with 0 - nobody in
a = calloc(Na, sizeof(int));
b = calloc(Nb, sizeof(int));
pthread_t thread[1000];
// initialization of mutexes
pthread_mutex_init(&mut, NULL);
pthread_mutex_init(&mut_out, NULL);
pthread_mutex_init(&mut_a, NULL);
pthread_mutex_init(&mut_b, NULL);
int t = to_enter;
for (int i = 0; i < t; i++)
pthread_create(&thread[i], NULL, worker, NULL);
for (int i = 0; i < t; i++)
pthread_join(thread[i], NULL);
pthread_mutex_destroy(&mut);
pthread_mutex_destroy(&mut_a);
pthread_mutex_destroy(&mut_b);
pthread_mutex_destroy(&mut_out);
pthread_cond_destroy(&cond_a);
pthread_cond_destroy(&cond_b);
pthread_cond_destroy(&cond_out);
free(b);
free(a);
return 0;
}