-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2SJF_NP.cpp
137 lines (112 loc) · 1.91 KB
/
2SJF_NP.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
//Shortest Job First Method
#include<iostream>
using namespace std;
struct process
{
int id;
int bt;
int at;
int wt;
int tat;
bool flag;
};
void showdata(struct process * array[],int n)
{
struct process *p;
cout<<"\nPro\t"<<"Arr\t"<<"Bur\tWait\tTurn\t\n";
for(int i=0;i<n;i++)
{
p=array[i];
cout<<p->id<<"\t"<<p->at<<"\t"<<p->bt<<"\t"<<p->wt<<"\t"<<p->tat<<"\t"<<"\n";
}
}
int execorder[20];
int ct=0;
int sjf(struct process * array[],int n)
{
struct process * p;
struct process * together[20];
int j=0;
for(int i=0;i<n;i++)
{
p=array[i];
if((p->flag==0) && (p->at<=ct))
{
together[j]=p;
j++;
}
}
if(j==0)
return 0;
else
{
int min;
p=together[0];
min=p->bt;
struct process *exec;
exec=p;
for(int i=0;i<=j-1;i++)
{
struct process * q;
q=together[i];
if(q->bt<min)
exec=q;
}
int sum=0;
for(int i=0;i<n;i++)
{
p=array[i];
if(p->flag==1)
sum=sum+p->bt;
}
cout<<exec->id<<"-> ";
exec->tat=sum+exec->bt-exec->at;
exec->wt=exec->tat-exec->bt;
exec->flag=1;
ct=sum+exec->bt;
// cout<<"\nCurrent time now becomes"<<ct;
sjf(array,n);
}
}
void awt_atat(struct process * array[],int n)
{
struct process * p;
float wt=0,tat=0;
float awt,atat;
for(int i=0;i<n;i++)
{
p=array[i];
wt=wt+p->wt;
tat=tat+p->tat;
}
awt=wt/n;
atat=tat/n;
cout<<"\nAverage Waiting Time="<<awt<<" Average Turnaround Time is="<<atat;
}
int main()
{
int n;
cout<<"Enter number of process ";
cin>>n;
struct process * array[20];
for(int i=0;i<n;i++)
{
int b,a;
struct process *p;
p=(struct process *) malloc(sizeof(struct process));
array[i]=p;
p->id=i+1;
cout<<"\nEnter ARRIVAL time and BURST time for process"<<i+1<<" ";
cin>>a;
cin>>b;
p->bt=b;
p->at=a;
p->flag=0;
p->wt=0;
p->tat=0;
}
cout<<"\nOrder of execution of process is ";
sjf(array,n);
showdata(array,n);
awt_atat(array,n);
}