forked from Sinh9x0-zz/multithreading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleeping_barber_problem.cpp
More file actions
92 lines (82 loc) · 2.54 KB
/
sleeping_barber_problem.cpp
File metadata and controls
92 lines (82 loc) · 2.54 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
//This program solves the 'sleeping barber' problem using semaphores
//TODO: replace sem_t mutex with pthread mutex
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
using namespace std;
int customers;
int chairs;
//only a specified number of people in the waiting room at a time
sem_t waitroom;
//only a single person can have their hair cut at a time
sem_t mutex;
//no more than one activity should be printed to console at a time
sem_t activity;
void * custThread(void *args){
int *p = (int *) args;
int custNum = *p;
sem_wait(&activity);
cout << "Customer " << custNum << " leaving for barber shop" << endl;
sem_post(&activity);
sem_wait(&activity);
cout << "Customer " << custNum << " arrived at barber shop." << endl;
sem_post(&activity);
sem_wait(&waitroom);
sem_wait(&activity);
cout << "Customer " << custNum << " entering waiting room." << endl;
sem_post(&activity);
sem_wait(&mutex);
sem_post(&waitroom);
sem_wait(&activity);
cout << "Customer " << custNum << " waking the barber." << endl;
sem_post(&activity);
sem_wait(&activity);
cout << "The barber is cutting hair." << endl;
sem_post(&activity);
sem_wait(&activity);
cout << "The barber has finished cutting hair." << endl;
sem_post(&activity);
sem_wait(&activity);
cout << "The barber is sleeping." << endl;
sem_post(&activity);
sem_post(&mutex);
sem_wait(&activity);
cout << "Customer " << custNum << " leaving the barber shop." << endl;
sem_post(&activity);
return 0;
}
int main(){
cout << "Maximum number of customers can only be 25. ";
cout << "Enter number of customers and chairs." << endl;
cin >> customers >> chairs;
while (cin.fail()){
cout << "Input must be two integers separated by a space" << endl;
cin.clear();
cin.ignore(256, '\n');
cin >> customers >> chairs;
}
cout << "A solution to the sleeping barber ";
cout << "problem using semaphores." << endl;
pthread_t thread[customers];
sem_init(&waitroom, 0, chairs);
sem_init(&mutex, 0, 1);
sem_init(&activity, 0, 1);
int *custNum;
cout << "The barber is sleeping" << endl;
//creates a thread for each customer
for (int n = 0; n < customers; n++){
custNum = new int(n);
if(pthread_create(&thread[n], NULL, custThread, (void *) custNum)){
cout << "Error with thread creation" << endl;
return -1;
}
}
for (int n = 0; n < customers; n++){
if(pthread_join(thread[n], NULL)){
cout << "Error joining thread" << endl;
return -1;
}
}
cout << "The barber is going home for the day." << endl;
return 0;
}