-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.cpp
84 lines (75 loc) · 2.38 KB
/
Scheduler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Created by Charlie McDermitt on 10/10/2017.
#include <vector>
#include "Scheduler.h"
#include "Log.h"
#include <algorithm>
#include "Hex_Util.h"
#include <tuple>
// Initalizes the Scheduler and gives it the job list, disk, ram, and dispatcher.
Scheduler::Scheduler(M_priority_queue<PCB *> &pcb_list, M_priority_queue<PCB *> &ready_queue, M_queue<PCB*> &readyish_queue,
M_queue<PCB *> &done_queue, MMU &mmu) {
pcbs = &pcb_list;
this->ready_queue = &ready_queue;
this->readyish_queue = &readyish_queue;
this->done_queue = &done_queue;
this->mmu = &mmu;
ram_space = std::list<free_ram>();
ram_space.push_front(free_ram(0, true));
for (free_ram i : ram_space)
std::cout << Hex_Util::bool_to_string(i.is_free) << std::endl;
done = 0;
}
// Long Term Scheduler
void Scheduler::schedule() {
PCB *temp;
// Continues until no more jobs can be loaded or there are no more jobs
// clean_ram_space();
while (readyish_queue->getSize() > 0) {
ready_queue->push(readyish_queue->pop());
}
if(done_queue->getSize() < 30){
temp = lt_get_next_pcb(*pcbs);
if (temp != nullptr) {
load_pcb(temp );
temp->state = PCB::READY;
ready_queue->push(temp);
jobsAllocated++;
}
} else {
// add sleep here
}
}
//returns pointer to next PCB, returns null pointer if no next PCB
PCB *Scheduler::lt_get_next_pcb(M_priority_queue<PCB *> &pcbs) {
PCB *temp;
if (pcbs.getSize() == 0)
return nullptr;
else {
temp = pcbs.pop();
return temp;
}
}
void Scheduler::load_pcb(PCB *p) { //puts PCB in RAM and ready_queue deal with sorting laternt ram_start = p->job_ram_address;
int *a;
for (int i = 0; i < INITIAL_NUM_OF_FRAMES; i++) {
a = mmu->add_page_to_ram(mmu->read_page_from_disk(std::get<0>(p->page_table[i])));
if(a != nullptr) {
std::get<1>(p->page_table[i]) = *a;
std::get<2>(p->page_table[i]) = true;
}
else
break;
}
}
void Scheduler::clean_ram_space() {
/*Dispatcher::lock_talk.lock();
//std::cout << "Cleaning Ram \n";
Dispatcher::lock_talk.unlock();*/
PCB *temp;
free_ram *f;
while (done_queue->getSize() != 0) {
temp = done_queue->pop();
done++;
}
}