forked from phoenix-aditya/processscheduling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority scheduling code os.cp
More file actions
101 lines (84 loc) · 2.21 KB
/
priority scheduling code os.cp
File metadata and controls
101 lines (84 loc) · 2.21 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
#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 processpriority{
int burst;
int pid;
int arrival;
int priority;
};
bool prioritycomp(processpriority a, processpriority b)
{
if(a.arrival==b.arrival)
{
if(a.priority==b.priority)
return a.pid<b.pid;
return a.priority<b.priority;
}
return a.arrival<b.arrival;
}
bool prioritychecker(processpriority arr[], int n)
{
for(int i=0;i<n;i++)
if(arr[i].burst>0)
return false;
return true;
}
void prioritys(processpriority arr[], int n)
{
int currtime=0;
int waittimeprocess[n];
int prevexe[n];
memset(waittimeprocess, 0, sizeof(waittimeprocess));
memset(prevexe, 0, sizeof(prevexe));
vector<int> gaant;
for(int i=0;i<n;i++)
prevexe[i]=arr[i].arrival;
while(!prioritychecker(arr, n))
{
sort(arr, arr+n, prioritycomp);
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+1<<" ";
cout<<endl;
}
int main()
{
cout<<"enter number of processes: ";
int n;
cin>>n;
processpriority arr[n];
cout<<"enter process id(0 format), arrival time , burst time, priority: \n";
for(int i=0;i<n;i++)
cin>>arr[i].pid>>arr[i].arrival>>arr[i].burst>>arr[i].priority;
prioritys(arr, n);
return 0;
}