Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Commit

Permalink
Homework 2 add
Browse files Browse the repository at this point in the history
  • Loading branch information
frozsgy committed Nov 20, 2020
1 parent 79d8ebf commit 15958a9
Show file tree
Hide file tree
Showing 15 changed files with 1,242 additions and 0 deletions.
Binary file added Homework 2/Ceng334-Homework2_v1-2.pdf
Binary file not shown.
34 changes: 34 additions & 0 deletions Homework 2/correct_format.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Person (0, hp, 2 -> 5, 50) made a request
Elevator (Moving-up, 0, 0, 0 -> 2)
Person (1, lp, 1 -> 5, 60) made a request
Elevator (Moving-up, 0, 0, 0 -> 1,2)
Person (2, lp, 2 -> 5, 50) made a request
Elevator (Moving-up, 0, 0, 0 -> 1,2)
Elevator (Moving-up, 0, 0, 1 -> 2)
Person (1, lp, 1 -> 5, 60) entered the elevator
Elevator (Moving-up, 60, 1, 1 -> 2,5)
Elevator (Moving-up, 60, 1, 2 -> 5)
Person (0, hp, 2 -> 5, 50) entered the elevator
Elevator (Moving-up, 110, 2, 2 -> 5)
Elevator (Moving-up, 110, 2, 3 -> 5)
Elevator (Moving-up, 110, 2, 4 -> 5)
Elevator (Idle, 110, 2, 5 ->)
Person (0, hp, 2 -> 5, 50) has left the elevator
Elevator (Idle, 60, 1, 5 ->)
Person (2, lp, 2 -> 5, 50) made a request
Elevator (Moving-down, 60, 1, 5 -> 2)
Person (1, lp, 1 -> 5, 60) has left the elevator
Elevator (Moving-down, 0, 0, 5 -> 2)
Elevator (Moving-down, 0, 0, 4 -> 2)
Elevator (Moving-down, 0, 0, 3 -> 2)
Elevator (Idle, 0, 0, 2 ->)
Person (2, lp, 2 -> 5, 50) made a request
Elevator (Idle, 0, 0, 2 ->)
Person (2, lp, 2 -> 5, 50) entered the elevator
Elevator (Moving-up, 50, 1, 2 -> 5)
Elevator (Moving-up, 50, 1, 3 -> 5)
Elevator (Moving-up, 50, 1, 4 -> 5)
Elevator (Idle, 50, 1, 5 ->)
Person (2, lp, 2 -> 5, 50) has left the elevator
Elevator (Idle, 0, 0, 5 ->)

9 changes: 9 additions & 0 deletions Homework 2/inp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
10 8 160 3 10000 5000 15000
90 0 9 2
80 0 8 1
85 2 6 1
70 1 3 2
75 8 0 1
65 7 2 2
60 7 1 2
100 9 3 1
69 changes: 69 additions & 0 deletions Homework 2/monitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef __MONITOR_H
#define __MONITOR_H
#include<pthread.h>

//! A base class to help deriving monitor like classes
class Monitor {
pthread_mutex_t mut; // this will protect the monitor
public:
Monitor() {
pthread_mutex_init(&mut, NULL);
}
class Condition {
Monitor *owner;
pthread_cond_t cond;
public:
Condition(Monitor *o) { // we need monitor ptr to access the mutex
owner = o;
pthread_cond_init(&cond, NULL) ;
}
void wait() { pthread_cond_wait(&cond, &owner->mut);}
void notify() { pthread_cond_signal(&cond);}
void notifyAll() { pthread_cond_broadcast(&cond);}
};
class Lock {
Monitor *owner;
public:
Lock(Monitor *o) { // we need monitor ptr to access the mutex
owner = o;
pthread_mutex_lock(&owner->mut); // lock on creation
}
~Lock() {
pthread_mutex_unlock(&owner->mut); // unlock on destruct
}
void lock() { pthread_mutex_lock(&owner->mut);}
void unlock() { pthread_mutex_unlock(&owner->mut);}
};
};

// when following is used as a local variable the
// method becomes a monitor method. On constructor
// lock is acquired, when function returns, automatically
// called desctructor unlocks it
#define __synchronized__ Lock mutex(this);

/* USAGE:
class A: public Monitor { // inherit from Monitor
Condition cv1, cv2; // condition varibles
...
public:
A() : cv1(this), cv2(this) { // pass "this" to cv constructors
...
}
void method1() {
__synchronized__;
// implement your monitor method. lock is already acquired
// thanks to macro call above.
while ( ...) { // I need to wait for an event
cv1.wait();
}
...
cv2.notify();
} // no need to unlock here, destructor of macro variable does it
void method2() {
__synchronized__;
// method1() // !!!! you should not do that.
}
};
*/
#endif
106 changes: 106 additions & 0 deletions Homework 2/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
Person (0,lp,0,9,90) made a request
Elevator (Moving-up,0,0,0->)
Person (0,lp,0,9,90) entered the elevator
Elevator (Moving-up,90,1,0->9)
Person (1,hp,0,8,80) made a request
Elevator (Moving-up,90,1,0->9)
Person (2,hp,2,6,85) made a request
Elevator (Moving-up,90,1,0->2,9)
Person (3,lp,1,3,70) made a request
Elevator (Moving-up,90,1,0->1,2,9)
Elevator (Moving-up,90,1,1->2,9)
Person (3,lp,1,3,70) entered the elevator
Elevator (Moving-up,160,2,1->2,3,9)
Elevator (Moving-up,160,2,2->3,9)
Elevator (Moving-up,160,2,3->9)
Person (3,lp,1,3,70) has left the elevator
Elevator (Moving-up,90,1,3->9)
Elevator (Moving-up,90,1,4->9)
Elevator (Moving-up,90,1,5->9)
Elevator (Moving-up,90,1,6->9)
Elevator (Moving-up,90,1,7->9)
Elevator (Moving-up,90,1,8->9)
Elevator (Idle,90,1,9->)
Person (5,lp,7,2,65) made a request
Elevator (Moving-down,90,1,9->7)
Person (4,hp,8,0,75) made a request
Elevator (Moving-down,90,1,9->8,7)
Person (0,lp,0,9,90) has left the elevator
Elevator (Moving-down,0,0,9->8,7)
Person (6,lp,7,1,60) made a request
Elevator (Moving-down,0,0,9->8,7)
Person (7,hp,9,3,100) made a request
Elevator (Moving-down,0,0,9->8,7)
Person (7,hp,9,3,100) entered the elevator
Elevator (Moving-down,100,1,9->8,7,3)
Elevator (Moving-down,100,1,8->7,3)
Elevator (Moving-down,100,1,7->3)
Person (6,lp,7,1,60) entered the elevator
Elevator (Moving-down,160,2,7->3,1)
Elevator (Moving-down,160,2,6->3,1)
Elevator (Moving-down,160,2,5->3,1)
Elevator (Moving-down,160,2,4->3,1)
Elevator (Moving-down,160,2,3->1)
Person (7,hp,9,3,100) has left the elevator
Elevator (Moving-down,60,1,3->1)
Elevator (Moving-down,60,1,2->1)
Elevator (Idle,60,1,1->)
Person (1,hp,0,8,80) made a request
Elevator (Moving-down,60,1,1->0)
Person (6,lp,7,1,60) has left the elevator
Elevator (Moving-down,0,0,1->0)
Elevator (Idle,0,0,0->)
Person (4,hp,8,0,75) made a request
Elevator (Moving-up,0,0,0->8)
Person (2,hp,2,6,85) made a request
Elevator (Moving-up,0,0,0->2,8)
Person (1,hp,0,8,80) made a request
Elevator (Moving-up,0,0,0->2,8)
Person (1,hp,0,8,80) entered the elevator
Elevator (Moving-up,80,1,0->2,8)
Elevator (Moving-up,80,1,1->2,8)
Elevator (Moving-up,80,1,2->8)
Elevator (Moving-up,80,1,3->8)
Elevator (Moving-up,80,1,4->8)
Elevator (Moving-up,80,1,5->8)
Elevator (Moving-up,80,1,6->8)
Elevator (Moving-up,80,1,7->8)
Elevator (Idle,80,1,8->)
Person (5,lp,7,2,65) made a request
Elevator (Moving-down,80,1,8->7)
Person (1,hp,0,8,80) has left the elevator
Elevator (Moving-down,0,0,8->7)
Person (4,hp,8,0,75) made a request
Elevator (Moving-down,0,0,8->7)
Person (4,hp,8,0,75) entered the elevator
Elevator (Moving-down,75,1,8->7,0)
Elevator (Moving-down,75,1,7->0)
Person (5,lp,7,2,65) entered the elevator
Elevator (Moving-down,140,2,7->2,0)
Elevator (Moving-down,140,2,6->2,0)
Elevator (Moving-down,140,2,5->2,0)
Elevator (Moving-down,140,2,4->2,0)
Elevator (Moving-down,140,2,3->2,0)
Elevator (Moving-down,140,2,2->0)
Person (5,lp,7,2,65) has left the elevator
Elevator (Moving-down,75,1,2->0)
Elevator (Moving-down,75,1,1->0)
Elevator (Idle,75,1,0->)
Person (2,hp,2,6,85) made a request
Elevator (Moving-up,75,1,0->2)
Person (4,hp,8,0,75) has left the elevator
Elevator (Moving-up,0,0,0->2)
Elevator (Moving-up,0,0,1->2)
Elevator (Idle,0,0,2->)
Person (2,hp,2,6,85) made a request
Elevator (Moving-up,0,0,2->)
Person (2,hp,2,6,85) entered the elevator
Elevator (Moving-up,85,1,2->6)
Elevator (Moving-up,85,1,3->6)
Elevator (Moving-up,85,1,4->6)
Elevator (Moving-up,85,1,5->6)
Elevator (Idle,85,1,6->)
Person (2,hp,2,6,85) has left the elevator
Elevator (Idle,0,0,6->)


84 changes: 84 additions & 0 deletions Homework 2/submission/controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "controller.hpp"

Controller::Controller()
{

}

Controller::Controller(int weight_capacity, int person_capacity, int travel_time, int idle_time, int in_out_time, ElevatorMonitor* emon)
{
this->weight_capacity = weight_capacity;
this->person_capacity = person_capacity;
this->travel_time = travel_time;
this->idle_time = idle_time;
this->in_out_time = in_out_time;
this->emon = emon;
}

Controller::Controller(const Controller& rhs)
{
this->weight_capacity = rhs.weight_capacity;
this->person_capacity = rhs.person_capacity,
this->travel_time = rhs.travel_time;
this->idle_time = rhs.idle_time;
this->in_out_time = rhs.in_out_time;
this->emon = rhs.emon;
}

Controller::~Controller()
{

}

void Controller::Serve()
{
int i = 0;
bool init_served = false;

while (this->emon->GetNextFloor() != -1) {
int remaining_people = 0;
for (auto jj: this->emon->GetFloors()) {
remaining_people += jj.size();
}
if (remaining_people == 0 && this->emon->GetPassengerCount() == 0) {
break;
}
usleep(this->travel_time);

if (this->emon->GetStatus() == IDLE) {
if (this->emon->GetNextFloor() > this->emon->GetCurrentFloor()) {
i++;
this->emon->MoveUp();
}
else if (this->emon->GetNextFloor() < this->emon->GetCurrentFloor()) {
i--;
this->emon->MoveDown();
} else {

}
}
if (this->emon->GetStatus() == MOVING_DOWN) {
i--;
this->emon->MoveDown();
}
else if (this->emon->GetStatus() == MOVING_UP){
i++;
this->emon->MoveUp();
} else {

}

usleep(this->in_out_time);
this->emon->RemovePeople(i);

usleep(this->in_out_time);
this->emon->TakePeople(i);

}

}

int Controller::GetIdleTime() const
{
return this->idle_time;
}
71 changes: 71 additions & 0 deletions Homework 2/submission/controller.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef CENG334HOMEWORK2_CONTROLLER_HPP
#define CENG334HOMEWORK2_CONTROLLER_HPP

#include "elevator.hpp"

#include <iostream>
#include <unistd.h>

class Controller {
private:#ifndef CENG334HOMEWORK2_PERSON_HPP
#define CENG334HOMEWORK2_PERSON_HPP

#include <iostream>

enum Priority { HIGH_PRIORITY = 1, LOW_PRIORITY = 2 };
enum Direction { UP = 1, DOWN = 2 };

class Person {
private:
int weight;
int initial_floor;
int destination_floor;
int id;
Priority priority;
bool served = false;

public:
Person();
Person(int id, int weight_person, int initial_floor, int destination_floor, int priority);
Person(const Person& rhs);
~Person();

int GetWeight() const;
int GetDestination() const;
int GetID() const;
int GetInitialFloor() const;
Priority GetPriority() const;
Direction GetDirection() const;
bool IsServed() const;

void SetServed();

void PrintEnter() const;
void PrintLeave() const;
void PrintRequest() const;


};

#endif //CENG334HOMEWORK2_PERSON_HPP
int weight_capacity;
int person_capacity;
int travel_time;
int idle_time;
int in_out_time;
ElevatorMonitor* emon;


public:
Controller();
Controller(int weight_capacity, int person_capacity, int travel_time, int idle_time, int in_out_time, ElevatorMonitor* emon);
Controller(const Controller& rhs);
~Controller();

int GetIdleTime() const;

void Serve();

};

#endif //CENG334HOMEWORK2_CONTROLLER_HPP
Loading

0 comments on commit 15958a9

Please sign in to comment.