-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadyQueue.cpp
165 lines (149 loc) · 3.5 KB
/
ReadyQueue.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Author: Shania Dhani
* Date Modified May 12, 2020
*
* The Ready Queue in this simulation has two levels, A Real Time process level
* and a Common process level. Real Time processes can preempt Common processes
* from using the CPU. Both levels of the Ready Queue practice Round-Robin CPU
* Scheduling, and therefore responds to the 'Q' command as an end of time slice/quantum
* action.
*/
#include "ReadyQueue.hpp"
#include <iomanip>
/* Default Constructor */
ReadyQueue::ReadyQueue() {}
/* New process arrives/is created */
// @return success
bool ReadyQueue::addProcess(Process &new_process)
{
if (new_process.getProcessType() == "RT")
{
ready_queue_rt_.push_back(new_process);
size_++;
return true;
}
else if (new_process.getProcessType() == "C")
{
ready_queue_common_.push_back(new_process);
size_++;
return true;
}
else
{
return false;
}
}
// @return current process on the CPU
Process ReadyQueue::getProcessOnCPU() const
{
// get process at top of deque
if (!ready_queue_rt_.empty())
{
return ready_queue_rt_.front();
}
else if (!ready_queue_common_.empty())
{
return ready_queue_common_.front();
}
else
{
Process err;
err.setPID(-1);
return err;
}
}
/* Terminate current process */
// @return success
bool ReadyQueue::terminateCurrentProcess()
{
// pop process on top of deque
if (!ready_queue_rt_.empty())
{
ready_queue_rt_.pop_front();
size_--;
}
else if (!ready_queue_common_.empty())
{
ready_queue_common_.pop_front();
size_--;
}
else
{
std::cout << "No process to terminate. " << std::endl;
return false;
}
return true;
}
/* End Current Process's use of CPU; Round Robin logic */
void ReadyQueue::endTimeSlice()
{
// end time slice, give cpu to next process
if (!ready_queue_rt_.empty())
{
/* Send current process to end of queue */
Process current_process = ready_queue_rt_.front();
ready_queue_rt_.pop_front();
ready_queue_rt_.push_back(current_process);
}
else if (!ready_queue_common_.empty())
{
/* Send current process to end of queue */
Process current_process = ready_queue_common_.front();
ready_queue_common_.pop_front();
ready_queue_common_.push_back(current_process);
}
}
// @return current size of Ready Queue
long long int ReadyQueue::getSize() const
{
return size_;
}
// @return RT Ready Queue
std::deque<Process> ReadyQueue::getRTReadyQueue() const
{
return ready_queue_rt_;
}
// @return Common Ready Queue
std::deque<Process> ReadyQueue::getCommonReadyQueue() const
{
return ready_queue_common_;
}
// helper print PID function
std::string printPID(const long long int &pid)
{
return "P" + std::to_string(pid) + " ";
}
// print ready queue snapshot
void ReadyQueue::printReadyQueue()
{
long long int cpu_pid = getProcessOnCPU().getPID();
std::cout << " CPU: "
<< " "
<< (cpu_pid == -1 ? "IDLE" : printPID(cpu_pid)) << std::endl;
std::cout << " RT-queue: ";
if (ready_queue_rt_.size() <= 1)
{
std::cout << "EMPTY";
}
for (auto p : ready_queue_rt_)
{
if (p.getPID() != cpu_pid)
{
std::cout << "<- " << printPID(p.getPID());
}
}
std::cout << std::endl;
std::cout << " C-queue: ";
if (ready_queue_common_.size() <= 1 && ready_queue_common_[0].getPID() == cpu_pid)
{
std::cout << "EMPTY";
}
for (auto p : ready_queue_common_)
{
if (p.getPID() != cpu_pid)
{
std::cout << "<- " << printPID(p.getPID());
}
}
std::cout << std::endl;
}