This repository was archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutexes.c
556 lines (461 loc) · 13.9 KB
/
mutexes.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
Author: Brendan Sting, Soren Zaiser
Assignment Number: 3
Date of Submission: 8/29/2023
Name of this file: mutexes.c
Short Description of contents:
This file contains the getters/setters for the global variables that require semaphore protection
*/
#include <semaphore.h>
#include <stdbool.h>
#include "mutexes.h"
/*
Function Name: initializeMutexes
Input to Method:
N/A
Output (Return value):
N/A (void)
Brief description of the task:
Initialize all mutexes to 1
*/
void initializeMutexes()
{
// Initialize all mutexes to 1
sem_init(¤tFloorMutex, 0, 1);
sem_init(&peopleCountMutex, 0, 1);
sem_init(&numFloorsMutex, 0, 1);
sem_init(&directionMutex, 0, 1);
sem_init(&openDoorsMutex, 0, 1);
sem_init(&wanderTimeMutex, 0, 1);
sem_init(&elevatorRosterMutex, 0, 1);
sem_init(&requestFloorMutex, 0, 1);
sem_init(&waitingAtFloorMutex, 0, 1);
}
/*
Function Name: setElevatorDirection
Input to Method:
int direction - Direction to set elevatorDirectionGlobal to
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the elevatorDirectionGlobal variable
*/
void setElevatorDirectionGlobal(int direction)
{
// Wait for access to elevatorDirectionGlobal
sem_wait(&directionMutex);
// Update elevatorDirectionGlobal
elevatorDirectionGlobal = direction;
// Release access to elevatorDirectionGlobal
sem_post(&directionMutex);
}
/*
Function Name: getElevatorDirection
Input to Method:
N/A
Output (Return value):
int - The direction the elevator is traveling
Brief description of the task:
Semaphore protection to read the elevatorDirectionGlobal variable
*/
int getElevatorDirection()
{
// Wait for access to elevatorDirectionGlobal
sem_wait(&directionMutex);
// Get elevatorDirectionGlobal
int direction = elevatorDirectionGlobal;
// Release access to elevatorDirectionGlobal
sem_post(&directionMutex);
return direction;
}
/*
Function Name: setNumFloors
Input to Method:
int numFloors - Number of floors to set numFloorsGlobal to
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the numFloorsGlobal variable
*/
void setNumFloors(int numFloors)
{
// Wait for access to numFloorsGlobal
sem_wait(&numFloorsMutex);
// Update numFloorsGlobal
numFloorsGlobal = numFloors;
// Release access to numFloorsGlobal
sem_post(&numFloorsMutex);
}
/*
Function Name: getNumFloors
Input to Method:
N/A
Output (Return value):
int - Total number of floors
Brief description of the task:
Semaphore protection to read the numFloorsGlobal variable
*/
int getNumFloors()
{
// Wait for access to numFloorsGlobal
sem_wait(&numFloorsMutex);
// Get numFloorsGlobal
int numFloors = numFloorsGlobal;
// Release access to numFloorsGlobal
sem_post(&numFloorsMutex);
return numFloors;
}
/*
Function Name: setPeopleCount
Input to Method:
int count - Number of people to set peopleCountGlobal to
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the peopleCountGlobal variable
*/
void setPeopleCount(int count)
{
// Wait for access to peopleCountGlobal
sem_wait(&peopleCountMutex);
// Update peopleCountGlobal
peopleCountGlobal = count;
// Release access to peopleCountGlobal
sem_post(&peopleCountMutex);
}
/*
Function Name: getPeopleCount
Input to Method:
N/A
Output (Return value):
int - Total number of people
Brief description of the task:
Semaphore protection to read the peopleCountGlobal variable
*/
int getPeopleCount()
{
// Wait for access to peopleCountGlobal
sem_wait(&peopleCountMutex);
// Get peopleCountGlobal
int count = peopleCountGlobal;
// Release access to peopleCountGlobal
sem_post(&peopleCountMutex);
return count;
}
/*
Function Name: setCurrentFloor
Input to Method:
int newFloor - The floor to update the currentFloorGlobal variable to
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the currentFloorGlobal variable
*/
void setCurrentFloor(long newFloor)
{
// Wait for access to currentFloor
sem_wait(¤tFloorMutex);
// Update currentFloor
currentFloorGlobal = newFloor;
// Release access to currentFloor
sem_post(¤tFloorMutex);
}
/*
Function Name: getCurrentFloor
Input to Method:
N/A
Output (Return value):
int - The current floor that the elevator is at
Brief description of the task:
Semaphore protection to read the currentFloorGlobal variable
*/
long getCurrentFloor()
{
// Wait for access to currentFloor
sem_wait(¤tFloorMutex);
// Get currentFloor
long currentFloor = currentFloorGlobal;
// Release access to currentFloor
sem_post(¤tFloorMutex);
return currentFloor;
}
/*
Function Name: setDoorsOpen
Input to Method:
bool open - The value to update the openDoorsGlobal variable to
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the openDoorsGlobal variable
*/
void setDoorsOpen(bool open)
{
// Wait for access to openDoorsGlobal
sem_wait(&openDoorsMutex);
// Update openDoorsGlobal
openDoorsGlobal = open;
// Release access to openDoorsGlobal
sem_post(&openDoorsMutex);
}
/*
Function Name: getDoorsOpen
Input to Method:
N/A
Output (Return value):
bool - The state of the elevator doors (open or closed)
Brief description of the task:
Semaphore protection to read the openDoorsGlobal variable
*/
bool getDoorsOpen()
{
// Wait for access to openDoorsGlobal
sem_wait(&openDoorsMutex);
// Get openDoorsGlobal
bool open = openDoorsGlobal;
// Release access to openDoorsGlobal
sem_post(&openDoorsMutex);
return open;
}
/*
Function Name: setWanderingTime
Input to Method:
int time - The value to update the wanderingTimeGlobal variable to
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the wanderingTimeGlobal variable
*/
void setWanderingTime(int time)
{
// Wait for access to wanderingTimeGlobal
sem_wait(&wanderTimeMutex);
// Update wanderingTimeGlobal
wanderingTimeGlobal = time;
// Release access to wanderingTimeGlobal
sem_post(&wanderTimeMutex);
}
/*
Function Name: getWanderingTime
Input to Method:
N/A
Output (Return value):
int - The max wander time
Brief description of the task:
Semaphore protection to read the wanderingTimeGlobal variable
*/
int getWanderingTime()
{
// Wait for access to wanderingTimeGlobal
sem_wait(&wanderTimeMutex);
// Get wanderingTimeGlobal
int time = wanderingTimeGlobal;
// Release access to wanderingTimeGlobal
sem_post(&wanderTimeMutex);
return time;
}
/*
Function Name: getIsOnElevator
Input to Method:
int personId - The person id to lookup on the elevator
Output (Return value):
bool - if the given personId on the elevator
Brief description of the task:
Semaphore protection to read the elevatorRosterGlobal array and check if the given personId is on the elevator
*/
bool getIsOnElevator(int personId)
{
// Wait for access to elevatorRosterGlobal
sem_wait(&elevatorRosterMutex);
int totalPeople = getPeopleCount();
// Iterate through elevatorRosterGlobal to see if personId is on the elevator
for (int i = 0; i < totalPeople; i++)
{
if (elevatorRosterGlobal[i] == personId)
{
// Release access to elevatorRosterGlobal
sem_post(&elevatorRosterMutex);
return true;
}
}
// Release access to elevatorRosterGlobal
sem_post(&elevatorRosterMutex);
return false;
}
/*
Function Name: boardElevator
Input to Method:
int personId - The person id to board on the elevator
Output (Return value):
bool - if the given personId was successfully boarded on the elevator
Brief description of the task:
Semaphore protection to update the elevatorRosterGlobal array and board a person onto the elevator
*/
bool boardElevator(int personId)
{
// Get if personId is on the elevator. If so, return
if (getIsOnElevator(personId) || !getDoorsOpen())
{
return false;
}
// Wait for access to elevatorRosterGlobal
sem_wait(&elevatorRosterMutex);
int totalPeople = getPeopleCount();
// Find first -1 in roster and board the elevator
for (int i = 0; i < totalPeople; i++)
{
if (elevatorRosterGlobal[i] == -1)
{
elevatorRosterGlobal[i] = personId;
// Release access to elevatorRosterGlobal
sem_post(&elevatorRosterMutex);
return true;
}
}
// Release access to elevatorRosterGlobal
sem_post(&elevatorRosterMutex);
return false; // Failed
}
/*
Function Name: leaveElevator
Input to Method:
int personId - The person id to deboard on the elevator
Output (Return value):
bool - if the given personId was successfully deboarded from the elevator
Brief description of the task:
Semaphore protection to update the elevatorRosterGlobal array and deboard a person from the elevator
*/
bool leaveElevator(int personId)
{
// Get if personId is on the elevator. If not, return
if (!getIsOnElevator(personId) || !getDoorsOpen())
{
return false;
}
// Wait for access to elevatorRosterGlobal
sem_wait(&elevatorRosterMutex);
int totalPeople = getPeopleCount();
// Find personId in roster and remove them from the elevator
for (int i = 0; i < totalPeople; i++)
{
if (elevatorRosterGlobal[i] == personId)
{
elevatorRosterGlobal[i] = -1;
// Release access to elevatorRosterGlobal
sem_post(&elevatorRosterMutex);
return true;
}
}
// Release access to elevatorRosterGlobal
sem_post(&elevatorRosterMutex);
return false; // Failed
}
/*
Function Name: isFloorRequested
Input to Method:
long floor - The floor number to check if it is requested
Output (Return value):
bool - if the given floor is requested
Brief description of the task:
Semaphore protection to read the requestFloorGlobal array and check if the given floor is requested
*/
bool isFloorRequested(long floor)
{
// Wait for access to requestFloorGlobal
sem_wait(&requestFloorMutex);
// Get requestFloorGlobal
bool requested = requestFloorGlobal[floor];
// Release access to requestFloorGlobal
sem_post(&requestFloorMutex);
return requested;
}
/*
Function Name: isFloorRequested
Input to Method:
int floor - The floor number to request
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the requestFloorGlobal array and set the given floor to requested (true)
*/
void requestFloor(int floor)
{
// Wait for access to requestFloorGlobal
sem_wait(&requestFloorMutex);
// Update requestFloorGlobal
requestFloorGlobal[floor] = true;
// Release access to requestFloorGlobal
sem_post(&requestFloorMutex);
}
/*
Function Name: clearFloorRequest
Input to Method:
long floor - The floor number to clear
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the requestFloorGlobal array and clear the call request for the given floor
*/
void clearFloorRequest(long floor)
{
// Wait for access to requestFloorGlobal
sem_wait(&requestFloorMutex);
// Update requestFloorGlobal
requestFloorGlobal[floor] = false;
// Release access to requestFloorGlobal
sem_post(&requestFloorMutex);
}
/*
Function Name: getWaitingAtAllFloors
Input to Method:
N/A
Output (Return value):
int* - Array of length n representing how many people are waiting at each floor. (n = number of floors)
Brief description of the task:
Semaphore protection to read the waitingAtFloorGlobal array
*/
int *getWaitingAtAllFloors()
{
// Wait for access to waitingAtFloorGlobal
sem_wait(&waitingAtFloorMutex);
// Get waitingAtFloorGlobal
int *waiting = waitingAtFloorGlobal;
// Release access to waitingAtFloorGlobal
sem_post(&waitingAtFloorMutex);
return waiting;
}
/*
Function Name: incrementWaitingCount
Input to Method:
int floor - The floor number to increment the waiting count
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the waiting count array (waitingAtFloorGlobal) at a given floor
*/
void incrementWaitingCount(int floor)
{
// Wait for access to waitingAtFloorGlobal
sem_wait(&waitingAtFloorMutex);
// Update waitingAtFloorGlobal
waitingAtFloorGlobal[floor]++;
// Release access to waitingAtFloorGlobal
sem_post(&waitingAtFloorMutex);
}
/*
Function Name: decrementWaitingCount
Input to Method:
int floor - The floor number to decrement the waiting count
Output (Return value):
N/A (void)
Brief description of the task:
Semaphore protection to update the waiting count array (waitingAtFloorGlobal) at a given floor
*/
void decrementWaitingCount(int floor)
{
// Wait for access to waitingAtFloorGlobal
sem_wait(&waitingAtFloorMutex);
// Update waitingAtFloorGlobal
waitingAtFloorGlobal[floor]--;
// Release access to waitingAtFloorGlobal
sem_post(&waitingAtFloorMutex);
}