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