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

View File

@ -22,6 +22,7 @@
2. Get the number of waiting people on that queue 2. Get the number of waiting people on that queue
3. While there are people waiting: 3. While there are people waiting:
1. If the lift is empty, change our direction to the supplied direction 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: 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> * <span style="color:red">Keep track of the number of `in progress` waiting people</span>
1. Add a person to the lift. 1. Add a person to the lift.
@ -31,7 +32,7 @@
--- ---
* ### Person Thread - *this is modelled around the actual metaphor* * ### 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 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> 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* 4. <span style="color:green">Wait for the lift to arrive</span> *on the appropriate spot (up | down); no metaphor for this concept*