-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstcf.cpp
More file actions
166 lines (138 loc) · 4.76 KB
/
Copy pathstcf.cpp
File metadata and controls
166 lines (138 loc) · 4.76 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
//Shortest Time to COmpletion First Scheduling
//Input File name :- "input.dat" (see reference input file)
//Output File name :- "stcf_out.dat"
//Compile:- g++ stcf.cpp -o stcf
//Run:- ./stcf
//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,tat_sum=0,rt_sum=0,wt_sum=0;
ifstream infile("input.dat");
ofstream outfile("stcf_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,pid,arrival_time>
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,i+1,arrival);
}
sort(process.begin()+start,process.begin()+start+count);
proc_count.insert({prev,{start,count}});
infile.close(); //read complete
//Shortest Time to Completion First scheduling calculations
tuple<int,int,int> curr_process;
pair<int,int> st_ct;
vector<tuple<int,int,int>> proc_queue;
vector<int> turn_around_time(n),waiting_time(n),response_time(n),tsched(n,-1);
int time = -1,flag = 0,lastpid = 0;
count = 0; //no. of completed processes
prev = 0;
while(count != n)
{
if(!proc_queue.empty())
{
curr_process = proc_queue[0];
burst = get<0>(curr_process);
arrival = get<2>(curr_process);
pid = get<1>(curr_process);
proc_queue.erase(proc_queue.begin()); //dequeing
burst--; //time left for completion of process
if(tsched[pid-1] == -1)
{
tsched[pid-1] = time;
response_time[pid-1] = time - arrival;
waiting_time[pid-1] = response_time[pid-1];
}
else
{
waiting_time[pid-1] += time - tsched[pid-1] - 1;
tsched[pid-1] = time;
}
if(burst > 0)
{
flag = 1;
time++;
get<0>(curr_process) = burst; //modified burst time
}
else
{
count++;
flag = 0;
time++;
turn_around_time[pid-1] = time - arrival;
}
if(pid != lastpid)
{
lastpid = pid;
cout << "Process " << pid <<" at time " << tsched[pid-1] << endl;
}
prev = tsched[pid-1];
}
else
{
prev = time;
time++;
}
for(int i=prev+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(flag)
proc_queue.push_back(curr_process);
sort(proc_queue.begin(),proc_queue.end());
} //while loop ends
cout << endl;
outfile << "PID\tTurnaround\tResponse\tWaiting" << endl;
cout << "PID\tTurnaround\tResponse\tWaiting" << endl;
for(int i=0; i < n; i++)
{
outfile << i+1 << "\t\t" << turn_around_time[i] << "\t\t " << response_time[i] << "\t\t\t" << waiting_time[i] << endl;
cout << i+1 << "\t" << turn_around_time[i] << "\t\t" << response_time[i] << "\t\t" << waiting_time[i] << endl;
tat_sum += turn_around_time[i];
rt_sum += response_time[i];
wt_sum += waiting_time[i];
}
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)wt_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)wt_sum/n << endl;
outfile.close();
return 0;
}