Skip to content

Commit 59d1ad5

Browse files
committed
first commit
0 parents  commit 59d1ad5

8 files changed

+605
-0
lines changed

.vscode/c_cpp_properties.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [
9+
"_DEBUG",
10+
"UNICODE",
11+
"_UNICODE"
12+
],
13+
"compilerPath": "C:/MinGW/bin/gcc.exe",
14+
"cStandard": "c17",
15+
"cppStandard": "c++17",
16+
"intelliSenseMode": "windows-msvc-x64"
17+
}
18+
],
19+
"version": 4
20+
}

.vscode/settings.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files.associations": {
3+
".env": "bat",
4+
"iostream": "cpp",
5+
"iosfwd": "cpp",
6+
"iomanip": "cpp"
7+
}
8+
}

Problem Statement.pdf

84.3 KB
Binary file not shown.

fcfs.cpp

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <vector>
4+
#include <queue>
5+
#include <algorithm>
6+
#include <map>
7+
#include <math.h>
8+
9+
using namespace std;
10+
11+
// Define a structure to represent the data
12+
struct DataPoint {
13+
int index;
14+
string name;
15+
double arrTime;
16+
double execTime;
17+
double burstTime;
18+
double ioBlockTime;
19+
int priority;
20+
};
21+
22+
class compare{
23+
public:
24+
bool operator()(DataPoint a, DataPoint b) {
25+
if(a.arrTime == b.arrTime){
26+
return a.index > b.index;
27+
}
28+
else{
29+
return a.arrTime > b.arrTime;
30+
}
31+
}
32+
};
33+
34+
35+
// Function to compare two DataPoint objects for sorting
36+
bool compareByarrTime(const DataPoint& a, const DataPoint& b) {
37+
return a.arrTime < b.arrTime;
38+
}
39+
40+
DataPoint* getMinArrTimeProcess(vector<DataPoint>& arr){
41+
double mini = 1e9;
42+
DataPoint* res;
43+
for(int i=0; i<arr.size(); i++){
44+
if(arr[i].arrTime == -1) continue; //its execution is already completed
45+
if(mini > arr[i].arrTime){
46+
mini = arr[i].arrTime;
47+
res = &(arr[i]);
48+
}
49+
}
50+
if(mini == 1e9) res = NULL;
51+
return res;
52+
}
53+
54+
int main() {
55+
// Open the input file
56+
string fileName;
57+
cout<<"Enter Filename:- "<<endl;
58+
cin>>fileName;
59+
ifstream inputFile(fileName);
60+
// Check if the file is open
61+
if (!inputFile.is_open()) {
62+
cerr << "Failed to open the file." << endl;
63+
return 1;
64+
}
65+
66+
cout<<"The name of the file to be read :- "<<fileName<<endl;
67+
68+
vector<DataPoint> data; // Vector to store the data
69+
priority_queue<DataPoint,vector<DataPoint>, compare> Queue;
70+
71+
// Read data from the file into the vector
72+
int dataPtr = 0;
73+
while (true) {
74+
DataPoint point;
75+
if (inputFile >> point.name >> point.arrTime >> point.execTime >> point.burstTime >> point.ioBlockTime >> point.priority) {
76+
point.index = dataPtr;
77+
data.push_back(point);
78+
Queue.push(point);
79+
dataPtr++;
80+
} else {
81+
break;
82+
}
83+
}
84+
85+
// Close the file
86+
inputFile.close();
87+
88+
// Sort the data based on the first arrTime
89+
sort(data.begin(), data.end(), compareByarrTime);
90+
91+
// Print the sorted data
92+
for (const auto& point : data) {
93+
cout << point.name << " " << point.arrTime << " " << point.execTime << " " << point.burstTime << " " << point.ioBlockTime << " " << point.priority << endl;
94+
cout<<endl;
95+
}
96+
97+
map<string,double> initialArrTime;
98+
for(const auto& point : data){
99+
initialArrTime[point.name] = point.arrTime;
100+
}
101+
102+
map<string,double> tat;
103+
map<string,double> wt;
104+
double timer = data[0].arrTime;
105+
106+
while(true){
107+
DataPoint process;
108+
if(!Queue.empty()){
109+
process = Queue.top();
110+
Queue.pop();
111+
}
112+
else break;
113+
DataPoint* execProcess = &process;
114+
if(execProcess == NULL) break;
115+
cout<<"Process which starts to execute " <<execProcess->name<<endl;
116+
cout<<"Time of exec start: "<<timer<<endl; //current process starts executing at this time
117+
wt[execProcess->name] += max(0.0,timer-execProcess->arrTime);
118+
double prevTime = timer; //before updating
119+
120+
// updated timer
121+
if(execProcess->execTime >= execProcess->burstTime){
122+
execProcess->execTime -= execProcess->burstTime;
123+
timer = max(prevTime,execProcess->arrTime) + execProcess->burstTime;
124+
}
125+
else{
126+
//time left is less than burst time
127+
timer = max(prevTime,execProcess->arrTime) + execProcess->execTime;
128+
execProcess->execTime = 0.0;
129+
}
130+
131+
//timer has been updated
132+
if(execProcess->execTime == 0.0){
133+
tat[execProcess->name] = timer - initialArrTime[execProcess->name];
134+
execProcess->arrTime = -1;
135+
}
136+
else{
137+
execProcess->arrTime = max(prevTime,execProcess->arrTime) + execProcess->burstTime + execProcess->ioBlockTime;
138+
}
139+
140+
if(execProcess->arrTime != -1){
141+
DataPoint updated;
142+
updated.name = execProcess->name;
143+
updated.arrTime = execProcess->arrTime;
144+
updated.burstTime = execProcess->burstTime;
145+
updated.execTime = execProcess->execTime;
146+
updated.ioBlockTime = execProcess->ioBlockTime;
147+
updated.priority = execProcess->priority;
148+
149+
Queue.push(updated);
150+
}
151+
152+
for (const auto& point : data) {
153+
cout << point.name << " " << point.arrTime << " " << point.execTime << " " << point.burstTime << " " << point.ioBlockTime << " " << point.priority <<endl;
154+
cout<<endl;
155+
}
156+
}
157+
158+
cout<<"DONE"<<endl;
159+
160+
for(auto &it:tat){
161+
cout<<"Turnaround time of "<<it.first << " " << it.second <<endl;
162+
}
163+
164+
for(auto &it:data){
165+
if(wt.find(it.name) == wt.end()){
166+
wt[it.name] = 0;
167+
}
168+
}
169+
170+
for(auto &it:wt){
171+
cout<<"Waiting Time of "<<it.first << " " << it.second <<endl;
172+
}
173+
174+
return 0;
175+
}

input.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
P1 0 7 2 2 2
2+
P2 1 5 1.5 1 1
3+
P3 2 4 1 1.5 3

input1.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
P1 0 20.0 1.5 5.0 2
2+
P2 2 15.0 2.0 6.0 1
3+
P3 8 27.0 1.8 3.5 4
4+
P4 12 36.0 2.1 2.6 3

0 commit comments

Comments
 (0)