-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsjf.cpp
More file actions
131 lines (105 loc) · 3.79 KB
/
Copy pathsjf.cpp
File metadata and controls
131 lines (105 loc) · 3.79 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
//Shortest Job First Scheduling
//Input File name :- "input.dat" (see reference input file)
//Output File name :- "sjf_out.dat"
//Compile:- g++ sjf.cpp -o sjf
//Run:- ./sjf
//Assuming processes in file are in sorted arrival time manner
#include<bits/stdc++.h>
using namespace std;
int main()
{
string line;
int n,pid,arrival=0,burst=0,prev=-1,start=0,count=0,tsched=0, turn_around_time, response_time, waiting_time,tat_sum=0,rt_sum=0;
ifstream infile("input.dat");
ofstream outfile("sjf_out.dat");
if(!infile)
{
cerr << "Datafile does not exist" << endl;
exit(-1);
}
getline(infile,line);
stringstream str(line);
str >> n; //no. of processes
vector<tuple<int,int,int>> process(n); //tuple = <burst_time,arrival_time,pid>
map<int,pair<int,int>> proc_count; //(arrival,(index,count))
for(int i=0; i < n; i++)
{
getline(infile,line);
stringstream str(line);
str >> arrival;
str >> burst;
if(arrival == prev)
count++;
else
{
if(i!=0)
{
sort(process.begin()+start,process.begin()+start+count); //sorting based on burst time for same arrival time
proc_count.insert({prev,{start,count}});
}
count = 1; prev = arrival;
start = i;
}
process[i] = make_tuple(burst,arrival,i+1);
}
sort(process.begin()+start,process.begin()+start+count);
proc_count.insert({prev,{start,count}});
infile.close(); //read complete
//SJF scheduling calculations
tuple<int,int,int> curr_process;
pair<int,int> st_ct;
vector<tuple<int,int,int>> proc_queue;
int time = -1;
outfile << "PID\tTurnaround\tResponse\tWaiting" << endl;
cout << "PID\tTurnaround\tResponse\tWaiting" << endl;
count = 0; //no. of completed processes
while(count != n)
{
if(!proc_queue.empty())
{
curr_process = proc_queue[0];
burst = get<0>(curr_process);
arrival = get<1>(curr_process);
pid = get<2>(curr_process);
proc_queue.erase(proc_queue.begin());
count++;
tsched = time;
time += burst;
turn_around_time = tsched + burst - arrival; //calculations
tat_sum += turn_around_time;
response_time = tsched - arrival;
rt_sum += response_time;
waiting_time = response_time;
outfile << pid << "\t\t" << turn_around_time << "\t\t " << response_time << "\t\t\t" << waiting_time << endl;
cout << pid << "\t" << turn_around_time << "\t\t" << response_time << "\t\t" << waiting_time << endl;
}
else
{
tsched = time;
time++;
}
for(int i=tsched+1; i <= time; i++)
{
auto itr = proc_count.find(i);
if(itr != proc_count.end())
{
st_ct = itr->second;
start = st_ct.first;
for(int j=start; j < start+st_ct.second; j++)
proc_queue.push_back(process[j]);
}
}
if(!proc_queue.empty())
sort(proc_queue.begin(),proc_queue.end());
}
outfile << endl;
cout << endl;
outfile << "Average Turnaround Time = " << (float)tat_sum/n << endl;
outfile << "Average Response Time = " << (float)rt_sum/n << endl;
outfile << "Average Waiting Time = " << (float)rt_sum/n << endl;
cout << "Average Turnaround Time = " << (float)tat_sum/n << endl;
cout << "Average Response Time = " << (float)rt_sum/n << endl;
cout << "Average Waiting Time = " << (float)rt_sum/n << endl;
outfile.close();
return 0;
}