Solved console printing, at the cost of excessive semaphore holding

This commit is contained in:
Brychan Dempsey 2021-04-27 20:48:52 +12:00
parent 33752ca3a1
commit a07c81912c
2 changed files with 9 additions and 7 deletions

View File

@ -120,8 +120,8 @@ void print_at_xy(int x, int y, const char *s) {
// --------------------------------------------------
void get_into_lift(lift_info *lift, int direction) {
// Local variables
volatile int *waiting;
volatile int *inprogress;
int *waiting;
int *inprogress;
semaphore *s;
// Check lift direction
@ -157,11 +157,10 @@ void get_into_lift(lift_info *lift, int direction) {
if(lift->peopleinlift < MAXNOINLIFT && *waiting - *inprogress) {
//volatile int pos = floors[lift->position].waitingtogodown + floors[lift->position].waitingtogoup - (*inprogress);
(*inprogress)++;
volatile int vPos = lift->position;
//semaphore_signal(&floors[lift->position].queueInteraction);
// Add person to the lift
lift->peopleinlift++;
print_at_xy(NLIFTS*4+floors[lift->position].waitingtogodown + floors[lift->position].waitingtogoup, NFLOORS-vPos, " ");
print_at_xy(NLIFTS*4+floors[lift->position].waitingtogodown + floors[lift->position].waitingtogoup, NFLOORS-lift->position, " ");
// Capture the person's printed position while we have the
//semaphore_signal(&floors[lift->position].queueInteraction);
// Do the print, no need to hold the semaphore while this is going on
@ -301,19 +300,21 @@ void* person_thread(void *p) {
// One more person waiting to go up
floors[from].waitingtogoup++;
//volatile int pos = floors[from].waitingtogoup +floors[from].waitingtogodown - floors[from].inprogress_down - floors[from].inprogress_up;
print_at_xy(NLIFTS*4+ floors[from].waitingtogoup +floors[from].waitingtogodown,NFLOORS-from, pr);
semaphore_signal(&floors[from].queueInteraction);
// Print person waiting
print_at_xy(NLIFTS*4+ floors[from].waitingtogoup +floors[from].waitingtogodown,NFLOORS-from, pr);
s = &floors[from].up_arrow;
// Wait for a lift to arrive (going up)
} else {
// One more person waiting to go down
floors[from].waitingtogodown++;
//volatile int pos = floors[from].waitingtogoup +floors[from].waitingtogodown - floors[from].inprogress_down - floors[from].inprogress_up;
print_at_xy(NLIFTS*4+floors[from].waitingtogoup +floors[from].waitingtogodown,NFLOORS-from, pr);
semaphore_signal(&floors[from].queueInteraction);
// Print person waiting
// We want this to happen after the semaphore is released, so the person is added after any deletions
print_at_xy(NLIFTS*4+floors[from].waitingtogoup +floors[from].waitingtogodown,NFLOORS-from, pr);
s = &floors[from].down_arrow;
// Wait for a lift to arrive (going down)
}

View File

@ -22,6 +22,7 @@
2. Get the number of waiting people on that queue
3. While there are people waiting:
1. If the lift is empty, change our direction to the supplied direction
* <span style="color:red">A semaphore is used before the conditional to help ensure the correct value of `in progress` transfers is correct</span>
2. While the lift is not full, and we have people waiting:
* <span style="color:red">Keep track of the number of `in progress` waiting people</span>
1. Add a person to the lift.
@ -31,7 +32,7 @@
---
* ### Person Thread - *this is modelled around the actual metaphor*
1. Choose a random floor to enter
1. Choose a floor to enter
2. Decide if the floor is above or below the current position
3. Add ourselves to the appropriate move (up | down) group <span style="color:green">Press the `call elevator` button</span>
4. <span style="color:green">Wait for the lift to arrive</span> *on the appropriate spot (up | down); no metaphor for this concept*