-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrb
More file actions
67 lines (60 loc) · 1.73 KB
/
brb
File metadata and controls
67 lines (60 loc) · 1.73 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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <unistd.h>
#include <time.h>
#define CHAIRS 3
int waiting_customers = 0;
omp_lock_t barber_lock, customer_lock;
void sleeping_barber_sequential() {
for (int i = 0; i < 10; i++) {
printf("Customer %d is waiting.\n", i);
sleep(1);
printf("Barber is cutting hair.\n");
sleep(2);
}
}
void sleeping_barber_parallel() {
#pragma omp parallel sections
{
#pragma omp section
{
while (1) {
omp_set_lock(&barber_lock);
if (waiting_customers > 0) {
waiting_customers--;
printf("Barber is cutting hair.\n");
sleep(2);
} else {
printf("Barber is sleeping.\n");
}
omp_unset_lock(&barber_lock);
sleep(1);
}
}
#pragma omp section
{
for (int i = 0; i < 10; i++) {
omp_set_lock(&customer_lock);
if (waiting_customers < CHAIRS) {
waiting_customers++;
printf("Customer %d is waiting.\n", i);
} else {
printf("Customer %d left because of no chair.\n", i);
}
omp_unset_lock(&customer_lock);
sleep(1);
}
}
}
}
int main() {
for (int i = 0; i < N; i++) omp_init_lock(&forks[i]);
omp_init_lock(&barber_lock);
clock_t start, end;
printf("\nsleeping barber problem:\n");
start = clock();
sleeping_barber_sequential();
end = clock();
printf("Sequential Time: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
}