-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.cpp
156 lines (130 loc) · 3.73 KB
/
simulation.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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <queue>
#include "Job.h"
#include "Stats.h"
#include "AdmissionScheduler.h"
#include "CPUScheduler.h"
#include "MemoryScheduler.h"
#include "JobList.h"
using namespace std;
///
//Main program source file.
///
int main(int argc, char **argv)
{
int memoryAlgorithm = 0;
char* inputFile;
AdmissionScheduler adminScheduler;
CPUScheduler *cpu = new CPUScheduler();
MemoryScheduler *memScheduler = NULL;
Stats stats;
if (argc < 3)
{
cout << "Usage: simulation <Memory Algorithm> <Data File>" << endl;
cout << " e.g.: ./simulation 1 datafile.txt" << endl;
exit(0);
}
memoryAlgorithm = atoi(argv[1]);
//select the proper memory scheduler based on input.
switch (memoryAlgorithm)
{
case 0:
memScheduler = new FirstFitMemoryScheduler();
break;
case 1:
memScheduler = new BestFitMemoryScheduler();
break;
case 2:
memScheduler = new WorstFitMemoryScheduler();
break;
//default case is FirstFit
default:
memScheduler = new FirstFitMemoryScheduler();
}
inputFile = argv[2];
ifstream infile(inputFile);
int pId;
int arrivalTime;
int serviceTime;
int dataSize;
int jobIndex = 0;
queue<int> readyForWaiting;
queue<int> readyTemp;
float percentTemp=0;
//Load the contents of the data file.
while (infile >> pId >> arrivalTime >> serviceTime >> dataSize)
{
Job job(pId, arrivalTime, serviceTime, dataSize);
JobList::addJob(job);
adminScheduler.addJob(jobIndex);
jobIndex++;
}
//While there are jobs to be processed.
while (adminScheduler.queueSize() > 0 || cpu->queueSize() > 0)
{
int clockTime = cpu->getCurrentClock();
//Gets jobs ready for admission.
readyTemp = adminScheduler.checkJobsForAdmission(
JobList::getJobs(),
clockTime
);
while (readyTemp.size() > 0)
{
readyForWaiting.push(readyTemp.front());
readyTemp.pop();
}
//There are jobs waiting for memory, try and schedule them.
if (readyForWaiting.size() > 0)
{
Job* job = JobList::getJobs()[readyForWaiting.front()];
//if job can be loaded into memory, add to CPUScheduler ready queue.
if (memScheduler->scheduleJob(job->getMappedProcessId(), job->getDataSize()))
{
//Add jobs to CPU ready queue
cpu->addToReadyQueue(readyForWaiting.front());
readyForWaiting.pop();
//output memory map.
stats.OutputMem(memScheduler->getMemory());
}
{
//otherwise, sets jobs state to waiting.
job->setCurrentState("Waiting");
}
//increment items waiting for memory
adminScheduler.incrementWaiting(readyForWaiting);
}
//schedule the next job.
cpu->scheduleJob();
//If a job was completed this time around, release the memory it was using
int lastCompleted = cpu->lastCompleted();
if (lastCompleted >= 0)
{
memScheduler->releaseMemory(JobList::getJobs()[lastCompleted]->getMappedProcessId());
stats.OutputMem(memScheduler->getMemory());
cout << endl << endl;
}
//Output Process tables every 10 ticks.
if (clockTime % 10 == 0)
{
cout << "The Process States at Time " << clockTime << endl << endl;
stats.ProcessStates(JobList::getJobs(), clockTime);
cout << endl;
}
//increment the clock.
cpu->incrementClock();
}
cout << "The Process States at Time " << cpu->getCurrentClock() << endl << endl;
stats.ProcessStates(JobList::getJobs(), cpu->getCurrentClock());
cout << endl;
cout << "Total simulated time units: " << cpu->getCurrentClock() << endl;
cout << "Total number of jobs: " << JobList::getJobs().size() << endl;
cout << "Average hole percent: " ;
stats.getAvgHolePercent();
cout << "Average waiting time: " << setprecision(4) << stats.getAvgWaitTime(JobList::getJobs()) << endl << endl;
cout << "Press enter to continue..." << endl;
cin.get();
}