-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.cpp
More file actions
120 lines (66 loc) · 2.67 KB
/
read.cpp
File metadata and controls
120 lines (66 loc) · 2.67 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
/* Angshuman Ghosh 2017CS01
Write a program to simulate an orchestra conductor and a group of musicians. The musicians play independently but
are coordinated by the conductor. For each measure of music indicated by the conductor’s baton,
the musicians play a set of notes. The musicians do not move onto the next measure until indicated
by the conductor’s baton. The Orchestra is called the Narcolepsy Symphony Orchestra because
occasionally a musician falls asleep. When this happens the rest of the musicians continue playing.
When the sleeping musician wakes up, the musician begins playing again with the orchestra.
The solution is done similar to that of a classical readers writers problem
*/
#include <iostream>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define MUSICIANS 6
using namespace std;
sem_t m; // semaphore to allow a certain number of readers in the system at once
sem_t lock; // semaphore to allow atomic reading and writing of a particular variable
sem_t mlock; // semaphore to atomically modify the number of readers currently in system
int m_count = 0;
int measure; // the current measure
void * musician(void * v){
int * id = (int *)v;
int last = -1;
while(measure != 20){
sleep(rand() % 5); // sleep a randoma mount of time
sem_wait(&m); // wait on the number of max allowed readers
sem_wait(&mlock); // wait on the lock to register the arrival of a reader
m_count++;
if(m_count == 1)
sem_wait(&lock); // lock the measure variable when first reader arrives
sem_post(&mlock); // unlock the reader registering lock
if(measure != last){
cout << "Thread " << *id << " playing measure " << measure << endl; // read the measure value
last = measure;
}
sleep(1);
sem_wait(&mlock); // reacquire the register lock to decrement the number of readers currently in system
m_count--;
if(m_count == 0)
sem_post(&lock); // release the lock for accessing file so that a writer waiting can start writing
sem_post(&mlock);
sem_post(&m); // signal any other waiting readers
}
}
int main(){
pthread_t musicians[MUSICIANS];
sem_init(&m, 0, (MUSICIANS * 3) / 4); // allow only 75% of readers at a time
sem_init(&lock, 0 , 1);
sem_init(&mlock, 0 , 1);
for(int i = 0; i < MUSICIANS ; i++){
pthread_create(&musicians[i], NULL, musician, (void *) new int(i));
}
for(int i = 0 ; i <= 20 ; i++){
sem_wait(&lock); // acquire lock to change the shared measure variable
measure = i;
sem_post(&lock); // release the lock
sleep(1);
}
for(int i = 0; i < MUSICIANS ; i++){
pthread_join(musicians[i], NULL);
}
sem_destroy(&lock);
sem_destroy(&mlock);
sem_destroy(&m);
}