-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.h
139 lines (103 loc) · 3.23 KB
/
algorithms.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef __ALGO_H__
#define __ALGO_H__
#include "Process.h"
#include <string>
#include <vector>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
/// <summary>
/// [0 = context switch done]
/// [1 = CPU completion]
/// [2 = starts using CPU]
/// [3 = I/O burst completion]
/// [4 = new process arrival]
/// </summary>
struct Command {
int time;
int type;
Process *process; // Change to a pointer
Command(int t, int tp, Process* p) : time(t), type(tp), process(p) {}
// Move constructor
Command(Command&& other) noexcept
: time(other.time), type(other.type), process(other.process) {}
// Move assignment operator
Command& operator=(Command&& other) noexcept {
if (this != &other) {
time = other.time;
type = other.type;
process = other.process;
}
return *this;
}
// Copy constructor
Command(const Command& other)
: time(other.time), type(other.type), process(other.process) {}
// Copy assignment operator
Command& operator=(const Command& other) {
if (this != &other) {
time = other.time;
type = other.type;
process = other.process;
}
return *this;
}
};
class Algo {
public:
string name = "";
vector<Process> processes;
vector<Process*> readyQueue;
Process* runningProcess = nullptr;
int t_cs;
int currentTime = 0;
//simout satistics
int endTime = 0;
int cpuPreemption = 0;
int ioPreemption = 0;
int cpuSwitchCount = 0;
int ioSwitchCount = 0;
double cpuTurnAroundTime = 0;
double ioTurnAroundTime = 0;
// For SJF and SRT
int alpha = 0;
// For algo that contain preemption
bool contain_preemption = false;
//is context switching?
bool isRemovingProcess = false;
bool isLoadingProcess = false;
Algo(string name, vector<Process> processes, int t_cs) : name(name), processes(processes), t_cs(t_cs) {}
std::unordered_map<int, vector<Command>> commandBuffer;
bool timeCheck();
bool hasCommand(int time);
void updateWaitTime();
virtual bool checkPreempt(Process & process);
void addCommand(Command command, int time);
void executeCommands(int time);
virtual string runningProcessName(Process & process);
virtual void Start();
bool allProcessCompleted();
void processRunningProcess();
virtual void addProcessToQ(Process& process);
virtual void ProcessArrival(Process& process);
virtual void StartCpu(Process& process);
virtual void FinishCpu(Process& process);
virtual void TauRecalculated(Process& process);
virtual void Preemption(Process& process);
virtual void FinishIO(Process& process);
virtual void LastCpuBurst(Process& process);
virtual void SwitchingDone(Process& process);
string GetQueueString();
static bool compareCommand(Command a, Command b);
virtual void newProcessRunCheck() = 0;
void printInfo(std::ofstream& file);
void RemovingPreemptedProcessDone(Process& process);
void LoadProcessToRunningSlot(Process& process);
private:
void setup();
};
#endif