diff --git a/assignment2_handout.c b/assignment2_handout.c index a978c71..d231ad9 100644 --- a/assignment2_handout.c +++ b/assignment2_handout.c @@ -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) } diff --git a/logic.md b/logic.md index 7636f0e..e83306f 100644 --- a/logic.md +++ b/logic.md @@ -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 + * A semaphore is used before the conditional to help ensure the correct value of `in progress` transfers is correct 2. While the lift is not full, and we have people waiting: * Keep track of the number of `in progress` waiting people 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 Press the `call elevator` button 4. Wait for the lift to arrive *on the appropriate spot (up | down); no metaphor for this concept*