-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (147 loc) · 7.29 KB
/
main.cpp
File metadata and controls
189 lines (147 loc) · 7.29 KB
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
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <functional>
#include <cstdio>
#include <fstream>
#include "Process.h"
using namespace std;
struct ComparePriorities // a struct that compares priorities, if equal, arrival times of the functions
{
bool operator()(const Process &a, const Process &b) {
if (a.priority !=b.priority)
return a.priority > b.priority; //lower value means higher priority
else return a.arTime > b.arTime; //FIFO manner
}
};
/*A function that writes the current situation of the ready queue(priority queue) to the output file
* outFile: output file
* curTime: current time
* priority queue: ready queue
*/
void OutputWriter(string outFile, int curTime, priority_queue<Process, vector<Process>, ComparePriorities> pqProcesses){
ofstream out(outFile,ios::out | ios::app);
out << curTime << ":HEAD-";
if(pqProcesses.empty())
out<< "-";
while(!pqProcesses.empty()){ //prints all the processes' current situation in the ready queue
Process p1 = pqProcesses.top();
pqProcesses.pop();
int instrCount=p1.instrPtr+1; //counter, denotes next instruction of the related process
out<< p1.procName<<"["<< instrCount<<"]-";
}
out << "TAIL\n";
out.close();
}
/* I've executed the necessary scheduling operations in the main function
*
*/
int main(int args, char* argv[]) {
if(args==1){ //sets definition file and output file in case of args given
argv[1]= (char*)"./definition.txt";
argv[2]= (char*)"./output.txt";
}
ifstream defnInput(argv[1], ios::in); // defines input file as argv[1]
string _procName; //process name
string _codeFile; //name of the code file
int _arrivalTime; //arrival time of the process
int _priority; //priority of the process
vector<Process> procVec; //a vector which holds all of the processes
int counter = 0; //counter that determines the process ID(arrival line# of the process)
while (defnInput.good()){ //reads until the end of the input file
defnInput >> _procName >> _priority >> _codeFile >> _arrivalTime; //each token assigned to related instances
if(!(procVec.size()>0&&_procName==procVec.back().procName)) { //handles the end of line exception
Process temp=Process(_priority, _arrivalTime, _procName, _codeFile, counter); //creates process object
procVec.push_back(temp); //pushes the created process to the processes vector
counter++;
}
}
defnInput.close();
vector <int> burstTimes(procVec.size(),0); //initializing a vector for burst times of processes
/*
* calculates burst times of processes, by adding each instructions'
* execution times, which are defined in the instruction vectors of the processes,
* in the process object beforehand
*/
for (int j = 0; j <procVec.size() ; j++) {
for (int i = 0; i <procVec[j].instrVec.size() ; ++i) {
burstTimes[j]+=(procVec[j].instrVec[i]);
}
}
int procVecSize=procVec.size(); //integer holding the size of the processes vector
/*
*
* Scheduling process takes place from now on
*
*/
int currTime=0; //current time which will be set to a new value after each operation, if necessary
int procPtr=0; //a pointer to keep at which process we are at
/* priority queue which orders the processes in it according to the ComparePriorities function
* which is the "ready queue",
*/
priority_queue<Process, vector<Process>, ComparePriorities> pqProcesses;
OutputWriter(argv[2],currTime,pqProcesses); //prints the initial situation of the ready queue
currTime=procVec[procPtr].arTime; //sets the current time to the arrival time of the first process
//checks the arrival of the new processes <= current time, pushes to the PQ if there are
while(procPtr != procVecSize&&procVec[procPtr].arTime<=currTime){
pqProcesses.push(procVec[procPtr]);
procPtr ++;
}
OutputWriter(argv[2],currTime,pqProcesses); //prints the current situation of the ready queue after adding new processes
while(!pqProcesses.empty()) { //operates until the ready queue is emptied
//the pointed instruction of the process at the top of the priority queue is executed,
// currTime is set accordingly
Process p2 = pqProcesses.top();
pqProcesses.pop();
currTime += p2.instrVec[p2.instrPtr];
p2.instrPtr++; //pointer points the next instruction of the process
if ((p2.instrPtr) != p2.instrVec.size()) { //if current process is yet to complete
pqProcesses.push(p2); //if current process is completed, we do not push it in pq again
bool newCheck=0; //boolean denotes the arrival of a new processchecks whether a new process is arrived <= current time
//checks the arrival of the new processes <= current time, pushes to the PQ if there are
while (procPtr!= procVecSize&&procVec[procPtr].arTime <= currTime) {
pqProcesses.push(procVec[procPtr]);
procPtr++;
newCheck=1;
}
if(newCheck) //prints the newly adopted ready queue's state
OutputWriter(argv[2],currTime,pqProcesses);
} else { //if current process is completed
procVec[p2.processID].compTime=currTime; //updates the completed process' completion time
//checks the arrival of the new processes <= current time, pushes to the PQ if there are
while (procPtr != procVecSize&&procVec[procPtr].arTime <= currTime) {
pqProcesses.push(procVec[procPtr]);
procPtr++;
}
OutputWriter(argv[2],currTime,pqProcesses); //prints the ready queue, since it's changed
}
//if the PQ is empty and there s processes not executed yet, sets the current time
// to the next process' first instruction's completion time
if ((procPtr != procVecSize)&&pqProcesses.empty()) {
currTime = procVec[procPtr].arTime;
//checks the arrival of the new processes <= current time, pushes to the PQ if there are
while ((procPtr != procVecSize)&&(procVec[procPtr].arTime <= currTime)) {
pqProcesses.push(procVec[procPtr]);
procPtr++;
}
OutputWriter(argv[2],currTime,pqProcesses); //prints the ready queue, since it's changed
}
}
/*
* writing to the output file
*/
ofstream out(argv[2],ios::out | ios::app); //creates the writer object
out<<"\n";
for(int i=0; i<procVec.size(); i++){
/*turnaround time= completion time- arrival time of the process
* waiting time= turnaround time- execution time of the process
*/
procVec[i].turnAround=procVec[i].compTime-procVec[i].arTime;
procVec[i].waitingTime=procVec[i].turnAround-burstTimes[i];
out<<"Turnaround time for "<< procVec[i].procName <<" = "<< procVec[i].turnAround<<" ms\n";
out<<"Waiting time for "<< procVec[i].procName <<" = "<< procVec[i].waitingTime<<"\n";
}
out.close();
return 0;
}