This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
74 lines (72 loc) · 3.21 KB
/
main.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
#include <iostream>
#include <string>
#include "LongTerm.h"
#include "ShortTerm.h"
#include "Memory.h"
#include "Loader.h"
#include "CPU.h"
#include "ShortLoader.h"
#include <time.h>
int main() {
Memory memory;
Loader loader;
int const cpuCount = 4;
CPU* cpus[cpuCount];
for(int i = 0; i < cpuCount; ++i)
cpus[i] = new CPU(&memory);
ShortTerm shortTerm;
ShortLoader shortLoader(&memory, &shortTerm);
//Change this to the absolute location of where you're storing the program file
loader.readFromFile("/Users/zachdillard/School/OperatingSystems/Simulator/Simulator/programfile.txt", &memory);
LongTerm longTerm(&memory, &shortTerm);
while(longTerm.getProcessCount() <= memory.JobCount)
{
longTerm.setNextRamStart(0);
while(longTerm.getProcessCount() <= memory.JobCount && longTerm.getProcessLength() < memory.ramSpaceLeft())
longTerm.addToRam();
/*longTerm.sortList();
for(int i = 0; i < longTerm.sort_queue.size(); ++i)
{
shortTerm.ready_queue.push(longTerm.sort_queue[i]->id);
longTerm.sort_queue[i]->waitingClock = clock();
}*/
while(shortTerm.ready_queue.size() > 0 || cpus[0]->running || cpus[1]->running || cpus[2]->running || cpus[3]->running)
{
for(int i = 0; i < cpuCount; ++i)
{
if(cpus[i]->running == false && shortTerm.ready_queue.size() != 0)
{
cpus[i]->setRamStart(memory.pcbs[shortTerm.ready_queue.front()]->ramStart);
memory.pcbs[shortTerm.ready_queue.front()]->cpuID = i;
shortLoader.setLengths();
shortLoader.toCPU(cpus[i]);
memory.pcbs[shortTerm.ready_queue.front()]->waitingClock = clock() - memory.pcbs[shortTerm.ready_queue.front()]->waitingClock;
memory.pcbs[shortTerm.ready_queue.front()]->waitingTime = ((double) memory.pcbs[shortTerm.ready_queue.front()]->waitingClock) / CLOCKS_PER_SEC;
memory.pcbs[shortTerm.ready_queue.front()]->runningClock = clock();
shortTerm.dispatch(&memory, cpus[i]);
cpus[i]->running = true;
}
else
{
if(cpus[i]->running || shortTerm.ready_queue.size() != 0) //Courtesy of Will
{
cpus[i]->decoder(cpus[i]->fetch());
if(cpus[i]->running == false)
{
//Stop running time for processes
memory.pcbs[cpus[i]->getProcessID()]->runningClock = clock() - memory.pcbs[cpus[i]->getProcessID()]->runningClock;
memory.pcbs[cpus[i]->getProcessID()]->runningTime = ((double) memory.pcbs[cpus[i]->getProcessID()]->runningClock) / CLOCKS_PER_SEC;
shortLoader.toRAM(cpus[i]);
cpus[i]->clearCPU();
}
}
}
}
}
memory.percentRAM.push_back(memory.percentRAMUsed());
memory.coreDump();
memory.clearRam();
//longTerm.sort_queue.clear();
}
return 0;
}