forked from phoenix-aditya/processscheduling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsjf non premptive code os.cp
88 lines (73 loc) · 1.79 KB
/
sjf non premptive code os.cp
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
#include<bits/stdc++.h>
#define pi 3.14159265359
#define ll long long
#define wh(t) int t;cin>>t; while(t--)
#define speed ios::sync_with_stdio(0); cin.tie(0);
#define endl "\n"
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
#include<string.h>
struct process{
int burst;
int pid;
int arrival;
};
bool sjfcomp(process a, process b)
{
return a.burst<b.burst;
}
bool sjfchecker(process arr[], int n)
{
for(int i=0;i<n;i++)
if(arr[i].burst>0)
return false;
return true;
}
void sjfnon(process arr[], int n)
{
int currtime=0;
int waittimeprocess[n];
memset(waittimeprocess, 0, sizeof(waittimeprocess));
vector<int> gaant;
while(!sjfchecker(arr, n))
{
sort(arr, arr+n, sjfcomp);
for(int i=0;i<n;i++)
{
if(arr[i].burst==0 || arr[i].arrival>currtime)
continue;
else
{
waittimeprocess[arr[i].pid]=currtime-arr[i].arrival;
currtime+=arr[i].burst;
arr[i].burst=0;
gaant.push_back(arr[i].pid);
break;
}
}
}
float totalwt=0;
for(int i=0;i<n;i++)
totalwt+=waittimeprocess[i];
totalwt=float(1.0*totalwt/n);
cout<<"avg waiting time: "<<totalwt<<endl;
cout<<"wait time per process: \n";
for(int i=0;i<n;i++)
cout<<waittimeprocess[i]<<" ";
cout<<endl;
for(auto x: gaant)
cout<<x<<" ";
cout<<endl;
}
int main()
{
cout<<"enter number of processes: ";
int n;
cin>>n;
process arr[n];
cout<<"enter process id, arrival time , burst time: \n";
for(int i=0;i<n;i++)
cin>>arr[i].pid>>arr[i].arrival>>arr[i].burst;
sjfnon(arr, n);
return 0;
}