-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJFPreFinal.cpp
More file actions
134 lines (105 loc) · 3.27 KB
/
Copy pathSJFPreFinal.cpp
File metadata and controls
134 lines (105 loc) · 3.27 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
/* Name : Annie Bhalla
Roll No. : 19HCS4009
Course : BSC (H) Computer Science
Semester : 3
Subject : Operating System
Title : Shortest Job First - Preemptive
*/
#include<iostream>
using namespace std;
struct SJF
{
int id;
int arrivalTime;
int burstTime;
int finishTime;
int turnaroundTime;
double waitingTime;
int remaining_bt;
};
// (1) to enter the details of all processes
void input(int len, SJF obj[])
{
for(int i=0;i<len;i++)
{
cout<<"\n Enter Arrival Time and Burst Time "<<i+1<<" : "<<endl;
cin>>obj[i].arrivalTime>>obj[i].burstTime;
obj[i].id=i+1;
obj[i].remaining_bt = obj[i].burstTime;
}
}
// (2) Displaying process no, arrival time , burst time , finishing time , TA , WT
void display(int n, SJF process[])
{
cout<<" Process No. "<<'\t'<<"Arrival Time"<<'\t'<<"Burst Time"<<'\t'<<" Finishing Time"<<'\t'<<" Turn Around Time"<<'\t'<<"Waiting Time"<<endl;
for(int i=0;i<n;i++)
{
cout<<process[i].id<<'\t'<<'\t'<<process[i].arrivalTime<<'\t'<<'\t'<<'\t'<<process[i].burstTime<<'\t'<<'\t'<<process[i].finishTime<<'\t'<<'\t'<<process[i].turnaroundTime<<'\t'<<'\t'<<process[i].waitingTime<<endl;
}
}
// scheduling algo
void schedule(int n, SJF proc[])
{
int completed = 0, timer = 0;
while(completed!=n)
{
int min_bt = 100;
int index = -1;
for(int i=0;i<n;i++)
{
if(proc[i].remaining_bt!=0 && proc[i].arrivalTime <= timer)
{
if(proc[i].remaining_bt<min_bt)
{
min_bt = proc[i].remaining_bt;
index = i;
}
if(proc[i].remaining_bt == min_bt)
if(proc[i].arrivalTime < proc[index].arrivalTime)
index = i;
}
}
if(index != -1)
{
proc[index].remaining_bt--;
timer++;
if(proc[index].remaining_bt == 0)
{
completed++;
proc[index].finishTime = timer;
proc[index].turnaroundTime = proc[index].finishTime - proc[index].arrivalTime;
proc[index].waitingTime = proc[index].turnaroundTime - proc[index].burstTime;
}
}
else
timer++;
}
double wt_avg = 0,tat_avg = 0;
display(n,proc);
for(int i=0;i<n;i++)
{
wt_avg += proc[i].waitingTime;
tat_avg += proc[i].turnaroundTime;
}
cout<<"Average Waiting Time : "<<(wt_avg/n)<<endl;
cout<<"Average Turnaround Time : "<<(tat_avg/n)<<endl;
}
// driver code
int main()
{
int nop; // nop= number of processes
SJF *process;
cout<<"\n Enter the number of Processes : ";
cin>>nop;
process = new SJF[nop];
input(nop,process); // calling input function to enter AT and BT and priority
// Displaying AT and BT of processes
cout<<" Process No. "<<'\t'<<"Arrival Time"<<'\t'<<"Burst Time"<<endl;
for(int i=0;i<nop;i++)
{
cout<<process[i].id<<'\t'<<'\t'<<process[i].arrivalTime<<'\t'<<'\t'<<process[i].burstTime<<endl;
}
schedule(nop, process); // calling schedule function
delete [] process;
return 0;
}