forked from phoenix-aditya/processscheduling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcfs code os assign.cp
82 lines (67 loc) · 1.62 KB
/
fcfs code os assign.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
#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{
// but to burst
int burst;
int pid;
int arrival;
};
bool compfcfs(process a, process b)
{
if(a.arrival==b.arrival)
return a.pid<b.pid;
return a.arrival<b.arrival;
}
void fcfs(process arr[], int n)
{
vector<int> gaant;
sort(arr, arr+n, compfcfs);
float totalwt=0;
int waittimeprocess[n];
int currtime=0;
for(int i=0;i<n;i++)
{
if(currtime<=arr[i].arrival)
{
currtime+=(arr[i].arrival+arr[i].burst);
waittimeprocess[i]=0;
gaant.push_back(arr[i].pid);
}
else
{
waittimeprocess[i]=currtime-arr[i].arrival;
currtime+=arr[i].burst;
gaant.push_back(arr[i].pid);
}
}
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;
fcfs(arr, n);
return 0;
}