-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1905095.cpp
300 lines (245 loc) · 8.82 KB
/
1905095.cpp
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
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<queue>
#include<unistd.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <random>
#define ll long long int
#define NOT_ARRIVED 0 // not arrived at printer
#define WAITING 1 // waiting for printer
#define PRINTING 2 // printing
#define DONE 3 // done printing
#define FREE 0 // free printer
#define BUSY 1 // busy printer
#define MAX_PRINTER 4 // number of printers
#define BINDERS 2 // number of binders
#define MAX_STUDENT 10000 // number of maximum students
#define MAX_GROUP 10000 // number of maximum groups
#define STAFF 2 // number of staff
using namespace std;
pthread_mutex_t lock; // print lock
// pthread_mutex_t printerlock; // printer lock
pthread_t student_thread[MAX_STUDENT]; // student threads
pthread_t staff_thread[STAFF]; // staff threads
sem_t binders; // binders semaphore
sem_t rc_lock; // reader count lock
sem_t db_lock; // database lock to variable submission_count
sem_t students[MAX_STUDENT]; // student semaphores
sem_t printers[MAX_PRINTER]; // printer semaphores
time_t start_time; // start time
time_t end_time; // end time
ll N; // number of students
ll M; // size of each group
ll w; // Printing time
ll x; // binding time
ll y; // reading writing time
ll student_delay[MAX_STUDENT]; // student delays for arrival
ll staff_delay[STAFF]; // staff random delays for reading period
ll student_state[MAX_STUDENT]; // student state
ll printer_state[MAX_PRINTER]; // printer state
ll group_print_count[MAX_GROUP]; // number of students printed in a group
ll rc = 0; // reader count
ll submission_count = 0; // number of submissions
// get current time
ll getcurrenttime(){
time(&end_time);
double time = double(end_time - start_time);
return (ll) time;
}
// generate random delay for students' arrival
ll generateRandomDelay(double lambda) {
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
std::poisson_distribution<ll> distribution(lambda);
return distribution(generator);
}
// check if printer is free
void test(ll i){
if(student_state[i] == WAITING && printer_state[(i+1)%MAX_PRINTER] == FREE){
printer_state[(i+1)%MAX_PRINTER] = BUSY;
student_state[i] = PRINTING;
sem_post(&students[i]);
}
}
// print assignment
void printing(ll id){
sem_wait(&printers[(id+1)%MAX_PRINTER]); // wait for printer
test(id); // check if printer is free
sem_post(&printers[(id+1)%MAX_PRINTER]); // release printer
sem_wait(&students[id]); // wait for printing
sem_wait(&printers[(id+1)%MAX_PRINTER]); // wait for printer
pthread_mutex_lock(&lock);
printf("Student %lld has started printing at printer %lld at time %lld\n", id+1, (id+1)%MAX_PRINTER +1, getcurrenttime());
pthread_mutex_unlock(&lock);
sleep(w);
pthread_mutex_lock(&lock);
printf("Student %lld has finished printing at printer %lld at time %lld\n", id+1, (id+1)%MAX_PRINTER +1, getcurrenttime());
pthread_mutex_unlock(&lock);
student_state[id] = DONE; // student has finished printing
printer_state[(id+1)%MAX_PRINTER] = FREE; // printer is free
ll group_no = id/M; // group number
group_print_count[group_no]++; // increment the number of students printed in the group
bool flag = false;
// check if any student in my group is waiting for printer
for(ll i=group_no*M; i<(group_no+1)*M; i++){
if(student_state[i] == WAITING && (i+1)%MAX_PRINTER == (id+1)%MAX_PRINTER){
flag = true; // found a student in my group waiting for printer
test(i);
break; // only one student can print at a time
}
}
// if no student in my group is waiting for printer, check if any other student is waiting for printer
if(flag == false){
for(ll i=0; i<N; i++){
if(student_state[i] == WAITING && (i+1)%MAX_PRINTER == (id+1)%MAX_PRINTER){
test(i); // found a student waiting for printer
break; // only one student can print at a time
}
}
}
sem_post(&printers[(id+1)%MAX_PRINTER]); // release printer
}
void bind(ll id){
sem_wait(&binders); // wait for binder
pthread_mutex_lock(&lock);
printf("\nGroup %lld has started binding at time %lld\n", id/M+1, getcurrenttime());
pthread_mutex_unlock(&lock);
sleep(x); // binding time
pthread_mutex_lock(&lock);
printf("\nGroup %lld has finished binding at time %lld\n", id/M+1, getcurrenttime());
pthread_mutex_unlock(&lock);
sem_post(&binders); // release binder
}
void writer(ll groupid){
sem_wait(&db_lock); // wait for database lock for submission_count
submission_count++;
pthread_mutex_lock(&lock);
printf("Group %lld has arrived to submit the report at time %lld\n", groupid+1, getcurrenttime());
pthread_mutex_unlock(&lock);
sleep(y); // writing time
pthread_mutex_lock(&lock);
printf("Group %lld has submitted the report at time %lld\n", groupid+1, getcurrenttime());
pthread_mutex_unlock(&lock);
sem_post(&db_lock); // release database lock for submission_count
}
ll terminated[MAX_STUDENT]; // terminated students
void * student(void* arg)
{
ll id = (ll)arg;
sleep(student_delay[id]); // delay before arrival
pthread_mutex_lock(&lock);
printf("Student %lld has arrived at the print station at time %lld \n", id+1, getcurrenttime());
student_state[id] = WAITING;
pthread_mutex_unlock(&lock);
printing(id); // print assignment
// group leader waits for all students in the group to finish printing
if((id+1)%M==0){
for(ll i=(id/M)*M; i<(id/M+1)*M; i++){
if(i==id || terminated[i]==1){
continue;
}
else{
pthread_join(student_thread[i],NULL);
}
}
}
else{
terminated[id] =1;
return NULL; // other students except group leader exit
}
pthread_mutex_lock(&lock);
printf("\nGroup %lld has finished printing at time %lld\n \n", id/M+1, getcurrenttime());
pthread_mutex_unlock(&lock);
// group leader binds the assignment
bind(id);
// group leader submits the assignment
writer(id/M);
pthread_exit(NULL);
}
void* reader(void* arg){
ll staffno = (ll)arg; // staff number
while(true){
sleep(staff_delay[staffno]); // delay period of staff before reading each time
sem_wait(&rc_lock);
rc++;
if(rc == 1){
sem_wait(&db_lock);
}
sem_post(&rc_lock);
// read
pthread_mutex_lock(&lock);
printf("Staff %lld has started reading the entry book at time %lld. No of submission = %lld \n", staffno+1, getcurrenttime(), submission_count);
pthread_mutex_unlock(&lock);
sleep(y); // reading time
pthread_mutex_lock(&lock);
printf("Staff %lld has finished reading the entry book at time %lld. No of submission = %lld \n", staffno+1, getcurrenttime(), submission_count);
pthread_mutex_unlock(&lock);
if(submission_count == N/M){
return NULL; // all submissions are done
}
sem_wait(&rc_lock);
rc--;
if(rc == 0){
sem_post(&db_lock);
}
sem_post(&rc_lock);
}
}
void initialize(){
}
int main(void)
{
printf("Number of Students: ");
scanf("%lld", &N);
printf("Number of Students in a group: ");
scanf("%lld", &M);
printf("Printing time: ");
scanf("%lld", &w);
printf("Binding time: ");
scanf("%lld", &x);
printf("Reading writing time: ");
scanf("%lld", &y);
pthread_mutex_init(&lock,0);
sem_init(&binders,0,BINDERS);
sem_init(&rc_lock,0,1);
sem_init(&db_lock,0,1);
for(ll i=0;i<N;i++){
sem_init(&students[i],0,1);
sem_wait(&students[i]);
terminated[i] = 0;
}
for(ll i=0; i<MAX_PRINTER; i++){
sem_init(&printers[i],0,1);
}
for(ll i=0;i<N/M; i++){
group_print_count[i] = 0;
}
for(ll i=0;i<N;i++){
student_delay[i] = 3+ generateRandomDelay(7.0)%20;
student_state[i] = NOT_ARRIVED;
}
for(ll i=0; i<MAX_PRINTER; i++){
printer_state[i] = FREE;
}
for(ll i=0; i<STAFF; i++){
staff_delay[i] = 1+ generateRandomDelay(10.0)%10;
}
time(&start_time);
for(ll i=0;i<N;i++){
pthread_create(&student_thread[i],NULL,student, (void*)i);
}
for(ll i=0; i<STAFF; i++){
pthread_create(&staff_thread[i],NULL,reader, (void*)i);
}
for(ll i=N/M -1;i<N;i+=M){
pthread_join(student_thread[i],NULL);
}
for(ll i=0; i<STAFF ; i++){
pthread_join(staff_thread[i], NULL);
}
// while(1);
return 0;
}