-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulatedOS.cpp
214 lines (183 loc) · 6.8 KB
/
SimulatedOS.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include"SimulatedOS.h"
SimulatedOS::SimulatedOS(int diskNum, int RAMAmount, int pages){
numOfDisk = diskNum;
amountRAM = RAMAmount;
pageSize = pages;
count = 0;
Process n(0,0);
np = n;
CPU a_core(np);
one_core = a_core;
CPUScheduling sche;
schedule = sche;
std::vector<Disk> d(diskNum);
disks = d;
int frameSize = amountRAM/pageSize;
PageCounter = 0;
RAM ram2(frameSize);
ram = ram2;
frame = 0;
}
SimulatedOS::SimulatedOS(int diskNum, int RAMAmount, int pages, int counting){
numOfDisk = diskNum;
std::vector<Disk> d(diskNum);
frame = 0;
disks = d;
amountRAM = RAMAmount;
pageSize = pages;
count = counting;
Process n(0,0);
np = n;
CPU a_core(np);
one_core = a_core;
CPUScheduling sche;
schedule = sche;
int frameSize = amountRAM/pageSize;
RAM ram2(frameSize);
ram = ram2;
PageCounter = 0;
}
void SimulatedOS::NewProcess(int priority){
++count;
//std::cout<<"Process stage"<<std::endl;
if(frame>=ram.GetFrameSize()){
frame = ram.GetLRU();
}
int page_number = 0;
Process new_process{count, priority,frame,page_number };
// check if CPU IS EMPTY
if(one_core.IsEmpty()){
//std::cout<<"core is empty"<<std::endl;
one_core.Insert(new_process);
//int page_number = 0;
int processid = new_process.GetPID();
//std::cout<<"right before ram"<<std::endl;
ram.Insert(page_number, processid, frame);
++frame;
}else{
if(one_core.GetProcess()<new_process){
Process temp = one_core.GetProcess();
one_core.Insert(new_process);
if(temp.GetPID() != 0 && temp.GetPriority() != 0){ // TO CHECK IF CPU IS IDLE IF pid = 0 and priority == 0 its idle
schedule.Insert(one_core, temp);//
}
int page_number = 0;
int processid = new_process.GetPID();
ram.Insert(page_number, processid,frame);
frame++;
}else{
int page_number = 0;
int processid = new_process.GetPID();
schedule.Insert(one_core, new_process);
ram.Insert(page_number, processid,frame);
frame++;
}
}
}
void SimulatedOS::Exit(){
Process process_to_quit = one_core.GetProcess();
if(!schedule.QueueIsEmpty()){
schedule.InsertNextProcess(one_core);
}else{
//Process process_to_quit = one_core.getProcess();
one_core.Erase();
}
ram.EraseFromMemory(process_to_quit.GetPID());
}
void SimulatedOS::DiskReadRequested(int diskNumber, std::string fileName){
// no file reading only simulation,
// currently running process requests to read the specifed file from the disk
// with a given number,
// we need disk and RAM etc.
disks[diskNumber].SetWord(fileName);
disks[diskNumber].push(one_core.GetProcess());
one_core.Erase();// empty the process,
one_core.Insert(schedule.GetPriorityProcess());
ram.UpdateFramePage(one_core.GetPage(), one_core.GetPID(), one_core.GetFrame());
}
void SimulatedOS::PrintLRU(){
ram.PrintLRU();
}
void SimulatedOS::FetchFrom(unsigned int memoryAddress){
PageCounter = memoryAddress/ pageSize;
if(!ram.FindPagePID(PageCounter, one_core.GetPID())){
ram.Insert(PageCounter,one_core.GetProcess().GetPID(),frame);
one_core.UpdatePage(PageCounter);
one_core.UpdateFrame(ram.GetFront()); // because ram is already inserted, and frame palce is updated
++frame;
}else{ // if found,
//std::cout<<"ONE CORE "<<one_core.getFrame()<<std::endl;
int updateFrame = ram.GetFrameViaPagePID(PageCounter, one_core.GetPID());
ram.UpdateFrame(updateFrame);
}
}
void SimulatedOS::DiskJobCompleted(int diskNumber){
// the process on top of the FIFO list, returns back to the ready queu.
//std::cout<<disks[diskNumber].q.front().PID<<std::endl;
if(!disks[diskNumber].CheckQueueEmpty()){
Process disk_process = disks[diskNumber].front();
schedule.Insert(one_core, disk_process);
disks[diskNumber].pop();
if(!ram.CanFrameViaPagePID(disk_process.GetPageCounter(), disk_process.GetPID())){
//update it in ram.
//std::cout<<"DISK JOB COMPLETED: "<<disk_process.frame<<"PID:"<<disk_process.PID<<"PAGECOUNTER: "<<disk_process.pageCounter<<std::endl;
ram.Insert(disk_process.GetPageCounter(), disk_process.GetPID(), disk_process.GetFrame()); //THIS IS CORRECT
ram.UpdateCPUFramePage(one_core, disk_process.GetPageCounter(), disk_process.GetFrame()); //
}else{
// if we can find it just update the particular frame number to the top.
ram.UpdateFrame(disk_process.GetFrame());
}
}else{
// empty
std::cout<<"Disk is empty "<<std::endl;
}
}
void SimulatedOS::PrintCPU(){
if(one_core.IsEmpty()){
std::cout<<"CPU is currently empty."<<std::endl;
return;
}
std::cout<<"CPU:"<<one_core.PrintID()<<" "<<std::endl;
}
void SimulatedOS::PrintReadyQueue(){
//schedule.printReadyQueue();
if(schedule.QueueIsEmpty()){
std::cout<<"Ready Queue is empty"<<std::endl;
return;
}
schedule.PrintReadyQueue();
}
void SimulatedOS::PrintRAM(){
ram.Print();
}
void SimulatedOS::PrintDisk(int diskNumber){
if(disks[diskNumber].CheckQueueEmpty()){
std::cout<<"Disk "<<diskNumber<<": Idle"<<std::endl;
return;
}
std::cout<<"Disk "<<diskNumber<<": ";
Process p = disks[diskNumber].front();
// total_disks++;
std::cout<<"PID "<< p.GetPID()<<",";
disks[diskNumber].GetWords();
std::cout<<std::endl;
}
void SimulatedOS::PrintDiskQueue(int diskNumber){
// print the qaueue in theD
//std::cout<<"i am at PRINT DISK QUEUE:"<<std::endl;
if(diskNumber > numOfDisk){
std::cout<<"Instruction ignored: no disk with such number exists"<<std::endl;
return;
}
std::queue<Process> q = disks[diskNumber].GetQueue();
q.pop();// because we only print the next one to be served
if(q.empty()){
std::cout<<"Disk "<<diskNumber<<" I/O-queue: Empty"<<std::endl;
//return;
}else{
while(!q.empty()){
std::cout<<"PID: "<<q.front().GetPID()<<std::endl;
q.pop();
}
}
}