-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRR.cpp
More file actions
59 lines (51 loc) · 2.38 KB
/
RR.cpp
File metadata and controls
59 lines (51 loc) · 2.38 KB
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
#include "RR.h"
#include "algorithms.h"
#include "cmath"
#include "Process.h"
#include <iostream>
using namespace std;
RR::RR(std::string name, std::vector<Process> processes, int t_cs, int t_slice)
: Algo(name, processes, t_cs), t_slice(t_slice){}
void RR::newProcessRunCheck(){
if (this->runningProcess == nullptr && !this->isLoadingProcess && !this->isRemovingProcess && !this->readyQueue.empty()) {
this->isLoadingProcess = true;
Process* p = this->readyQueue.front();
Command c(this->currentTime + this->t_cs / 2, 2, p);
addCommand(c, this->currentTime + this->t_cs / 2);
} else if (this->currentTime == this->next_expire_time && this->runningProcess != nullptr && this->runningProcess->burst_time_left > 0) {
if (timeCheck())cout << "time " << this->currentTime << "ms: Time slice expired; ";
if(this->readyQueue.empty()){
if (timeCheck())cout << "no preemption because ready queue is empty [Q <empty>]" << endl;
} else {
// Perform a preemption.
if (timeCheck())cout << "preempting process "<< this->runningProcessName(*this->runningProcess) << " with "<< this->runningProcess->burst_time_left <<
"ms remaining [Q" << this->GetQueueString() << "]" << endl;
// Schedule to remove the current running progress
Process* p = this->readyQueue.front();
Command c(this->currentTime + this->t_cs/2, 5, p);
addCommand(c, this->currentTime + this->t_cs/2);
// Run the front process in the ready queue
if (runningProcess->isCpuBound) {
this->cpuPreemption++;
}
else {
this->ioPreemption++;
}
//
Command c0(this->currentTime + this->t_cs / 2, -1, this->runningProcess);
this->addCommand(c0, this->currentTime + this->t_cs / 2);
this->runningProcess = nullptr;
this->isRemovingProcess = true;
this->isLoadingProcess = true;
}
this->next_expire_time = this->runningProcess == nullptr ? 999999 : this->currentTime + this->t_slice;
}
}
void RR::StartCpu(Process & process){
this->next_expire_time = this->currentTime + this->t_slice;
Algo::StartCpu(process);
}
void RR::FinishCpu(Process& process){
this->next_expire_time = 999999;
Algo::FinishCpu(process);
}