Woirking?

This commit is contained in:
Brychan Dempsey 2021-04-28 17:54:37 +12:00
parent a07c81912c
commit 5bf764f537
2 changed files with 21 additions and 18 deletions

BIN
assignment2 Normal file

Binary file not shown.

View File

@ -11,9 +11,9 @@
// --------------------------------------------------
// Define Problem Size
// --------------------------------------------------
#define NLIFTS 20 // The number of lifts in the building
#define NLIFTS 4 // The number of lifts in the building
#define NFLOORS 20 // The number of floors in the building
#define NPEOPLE 200 // The number of people in the building
#define NPEOPLE 3 // The number of people in the building
#define MAXNOINLIFT 10 // Maximum number of people in a lift
@ -78,9 +78,9 @@ typedef struct {
int stops[NFLOORS]; // How many people are going to each floor
semaphore stopsem[NFLOORS]; // People in the lift wait on one of these
// Semaphore stating if a thread is currently loading/unloading someone from the lift
semaphore loading;
//semaphore loading;
// If the target floor is being selected (only one can press at a time)
semaphore buttonPress;
//semaphore buttonPress;
} lift_info;
// --------------------------------------------------
@ -102,7 +102,7 @@ void print_at_xy(int x, int y, const char *s) {
gotoxy(x,y);
// Slow things down
Sleep(1);
Sleep(0);
// Print the string
printf("%s", s);
@ -176,6 +176,7 @@ void get_into_lift(lift_info *lift, int direction) {
// Wait for person to get into lift
Sleep(GETINSPEED);
// Need a sem here
targetLift[lift->position] = lift;
semaphore_signal(s);
} else {
@ -200,8 +201,8 @@ void* lift_thread(void *p) {
lift.direction = UP; // Lift starts going up
lift.peopleinlift = 0; // Lift starts empty
// lift is ok with loading a single person
semaphore_create(&lift.loading, 1);
semaphore_create(&lift.buttonPress, 1);
//semaphore_create(&lift.loading, 1);
//semaphore_create(&lift.buttonPress, 1);
for(i = 0; i < NFLOORS; i++) {
lift.stops[i]=0; // No passengers waiting
@ -215,7 +216,7 @@ void* lift_thread(void *p) {
Sleep(rnd(1000));
// Loop forever
while(TRUE) {
while(true) {
// Print current position of the lift
print_at_xy(no*4+1, NFLOORS-lift.position, lf);
// Wait for a while
@ -224,14 +225,14 @@ void* lift_thread(void *p) {
// Drop off passengers on this floor
while (lift.stops[lift.position] != 0) {
// One less passenger in lift
semaphore_wait(&lift.loading);
//semaphore_wait(&lift.loading);
lift.peopleinlift--;
semaphore_signal(&lift.loading);
//semaphore_signal(&lift.loading);
// One less waiting to get off at this floor
// Don't need a semaphore as nothing should read this value now
semaphore_wait(&lift.buttonPress);
//semaphore_wait(&lift.buttonPress);
lift.stops[lift.position]--;
semaphore_signal(&lift.buttonPress);
//semaphore_signal(&lift.buttonPress);
// Wait for exit lift delay
Sleep(GETOUTSPEED);
@ -285,7 +286,7 @@ void* person_thread(void *p) {
randomise();
// Stay in the building forever
while(TRUE) {
while(true) {
// Work for a while
Sleep(rnd(PEOPLESPEED));
do {
@ -294,10 +295,11 @@ void* person_thread(void *p) {
} while(to == from);
semaphore* s;
// Wait for our turn to press a button
semaphore_wait(&floors[from].queueInteraction);
// Check which direction the person is going (UP/DOWN)
if(to > from) {
// One more person waiting to go up
semaphore_wait(&floors[from].queueInteraction);
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);
@ -307,6 +309,7 @@ void* person_thread(void *p) {
s = &floors[from].up_arrow;
// Wait for a lift to arrive (going up)
} else {
semaphore_wait(&floors[from].queueInteraction);
// 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;
@ -327,9 +330,9 @@ void* person_thread(void *p) {
// Add one to passengers waiting for floor
// Only one person enters at a time
semaphore_wait(&lift->buttonPress);
//semaphore_wait(&lift->buttonPress);
lift->stops[to]++;
semaphore_signal(&lift->buttonPress);
//semaphore_signal(&lift->buttonPress);
// Press button if we are the first
if(lift->stops[to]==1) {
// Print light for destination
@ -393,7 +396,7 @@ void printbuilding(void) {
// Iterate through the floors and update the queue size
// --------------------------------------------------
void* update_floor_counts(void *p){
while(TRUE){
while(true){
int i;
for (i = 0; i < NFLOORS; i++){
char str[12];
@ -403,7 +406,7 @@ void* update_floor_counts(void *p){
}
}
Sleep(16);
Sleep(0);
}