-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcfs.cpp
175 lines (149 loc) · 4.93 KB
/
fcfs.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
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <math.h>
using namespace std;
// Define a structure to represent the data
struct DataPoint {
int index;
string name;
double arrTime;
double execTime;
double burstTime;
double ioBlockTime;
int priority;
};
class compare{
public:
bool operator()(DataPoint a, DataPoint b) {
if(a.arrTime == b.arrTime){
return a.index > b.index;
}
else{
return a.arrTime > b.arrTime;
}
}
};
// Function to compare two DataPoint objects for sorting
bool compareByarrTime(const DataPoint& a, const DataPoint& b) {
return a.arrTime < b.arrTime;
}
DataPoint* getMinArrTimeProcess(vector<DataPoint>& arr){
double mini = 1e9;
DataPoint* res;
for(int i=0; i<arr.size(); i++){
if(arr[i].arrTime == -1) continue; //its execution is already completed
if(mini > arr[i].arrTime){
mini = arr[i].arrTime;
res = &(arr[i]);
}
}
if(mini == 1e9) res = NULL;
return res;
}
int main() {
// Open the input file
string fileName;
cout<<"Enter Filename:- "<<endl;
cin>>fileName;
ifstream inputFile(fileName);
// Check if the file is open
if (!inputFile.is_open()) {
cerr << "Failed to open the file." << endl;
return 1;
}
cout<<"The name of the file to be read :- "<<fileName<<endl;
vector<DataPoint> data; // Vector to store the data
priority_queue<DataPoint,vector<DataPoint>, compare> Queue;
// Read data from the file into the vector
int dataPtr = 0;
while (true) {
DataPoint point;
if (inputFile >> point.name >> point.arrTime >> point.execTime >> point.burstTime >> point.ioBlockTime >> point.priority) {
point.index = dataPtr;
data.push_back(point);
Queue.push(point);
dataPtr++;
} else {
break;
}
}
// Close the file
inputFile.close();
// Sort the data based on the first arrTime
sort(data.begin(), data.end(), compareByarrTime);
// Print the sorted data
for (const auto& point : data) {
cout << point.name << " " << point.arrTime << " " << point.execTime << " " << point.burstTime << " " << point.ioBlockTime << " " << point.priority << endl;
cout<<endl;
}
map<string,double> initialArrTime;
for(const auto& point : data){
initialArrTime[point.name] = point.arrTime;
}
map<string,double> tat;
map<string,double> wt;
double timer = data[0].arrTime;
while(true){
DataPoint process;
if(!Queue.empty()){
process = Queue.top();
Queue.pop();
}
else break;
DataPoint* execProcess = &process;
if(execProcess == NULL) break;
cout<<"Process which starts to execute " <<execProcess->name<<endl;
cout<<"Time of exec start: "<<timer<<endl; //current process starts executing at this time
wt[execProcess->name] += max(0.0,timer-execProcess->arrTime);
double prevTime = timer; //before updating
// updated timer
if(execProcess->execTime >= execProcess->burstTime){
execProcess->execTime -= execProcess->burstTime;
timer = max(prevTime,execProcess->arrTime) + execProcess->burstTime;
}
else{
//time left is less than burst time
timer = max(prevTime,execProcess->arrTime) + execProcess->execTime;
execProcess->execTime = 0.0;
}
//timer has been updated
if(execProcess->execTime == 0.0){
tat[execProcess->name] = timer - initialArrTime[execProcess->name];
execProcess->arrTime = -1;
}
else{
execProcess->arrTime = max(prevTime,execProcess->arrTime) + execProcess->burstTime + execProcess->ioBlockTime;
}
if(execProcess->arrTime != -1){
DataPoint updated;
updated.name = execProcess->name;
updated.arrTime = execProcess->arrTime;
updated.burstTime = execProcess->burstTime;
updated.execTime = execProcess->execTime;
updated.ioBlockTime = execProcess->ioBlockTime;
updated.priority = execProcess->priority;
Queue.push(updated);
}
for (const auto& point : data) {
cout << point.name << " " << point.arrTime << " " << point.execTime << " " << point.burstTime << " " << point.ioBlockTime << " " << point.priority <<endl;
cout<<endl;
}
}
cout<<"DONE"<<endl;
for(auto &it:tat){
cout<<"Turnaround time of "<<it.first << " " << it.second <<endl;
}
for(auto &it:data){
if(wt.find(it.name) == wt.end()){
wt[it.name] = 0;
}
}
for(auto &it:wt){
cout<<"Waiting Time of "<<it.first << " " << it.second <<endl;
}
return 0;
}