|
| 1 | +#include<bits/stdc++.h> |
| 2 | +#define pb push_back |
| 3 | +#define ll long long |
| 4 | +#define double long double |
| 5 | +#define pii pair<int,int> |
| 6 | +#define pll pair<ll,ll> |
| 7 | +#define f first |
| 8 | +#define s second |
| 9 | +#define int long long |
| 10 | +#define sz(x) (ll)(x.size()) |
| 11 | +#define MAX 200005 |
| 12 | +using namespace std; |
| 13 | + |
| 14 | +signed main(){ |
| 15 | + cout<<"enter number of process and quant\n"; |
| 16 | + int n,quant; |
| 17 | + cin>>n>>quant; |
| 18 | + pair<int,pair<int,int> > arr[n]; |
| 19 | + cout<<"Enter Arrival time and burst time\n"; |
| 20 | + for(int i=0;i<n;i++){ |
| 21 | + cin>>arr[i].f>>arr[i].s.f; |
| 22 | + arr[i].s.s=i; |
| 23 | + } |
| 24 | + int burst_sort[n],arr_sort[n]; |
| 25 | + sort(arr,arr+n); |
| 26 | + vector<pii> cur; |
| 27 | + for(int i=0;i<n;i++){ |
| 28 | + burst_sort[arr[i].s.s]=arr[i].s.f; |
| 29 | + arr_sort[arr[i].s.s]=arr[i].f; |
| 30 | + } |
| 31 | + int time_exec=0; |
| 32 | + int done[n]={0}; |
| 33 | + int cnt=0; |
| 34 | + int ans[n]; |
| 35 | + int tmp=0; |
| 36 | + int cs=0; |
| 37 | + int prev=-1; |
| 38 | + vector<pii> cpu_idle; |
| 39 | + for(int i=0;i<n;i++){ |
| 40 | + cur.pb(make_pair(arr[i].s.f,arr[i].s.s)); |
| 41 | + if(time_exec<arr[i].f){ |
| 42 | + cpu_idle.pb(make_pair(time_exec,arr[i].f)); |
| 43 | + time_exec=arr[i].f; |
| 44 | + } |
| 45 | + // if(i<n-1) cout<<time_exec<<" "<<arr[i+1].f<<" "<<"here\n"; |
| 46 | + while(i<n-1 and cnt!=cur.size() and arr[i+1].f>time_exec){ |
| 47 | + if(cur[tmp].f<=quant and cur[tmp].f!=0){ |
| 48 | + if(prev!=tmp) cs++,prev=tmp; |
| 49 | + ans[cur[tmp].s]=time_exec+cur[tmp].f; |
| 50 | + cur[tmp].f=0; |
| 51 | + time_exec+=cur[tmp].f; |
| 52 | + cnt++; |
| 53 | + // cout<<"gandu\n"; |
| 54 | + tmp++; |
| 55 | + if(tmp==cur.size() and time_exec<arr[i+1].f) tmp=0; |
| 56 | + continue; |
| 57 | + } |
| 58 | + if(cur[tmp].f>0){ |
| 59 | + // cout<<"hesa\n"; |
| 60 | + if(prev!=tmp) cs++,prev=tmp; |
| 61 | + time_exec+=quant; |
| 62 | + cur[tmp].f-=quant; |
| 63 | + tmp++; |
| 64 | + if(tmp==cur.size() and time_exec<arr[i+1].f) tmp=0; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + while(cnt!=n){ |
| 69 | + for(int i=tmp;i<n;i++){ |
| 70 | + if(cur[i].f>0 and cur[i].f<=quant){ |
| 71 | + if(prev!=i) cs++,prev=i; |
| 72 | + time_exec+=cur[i].f; |
| 73 | + ans[cur[i].s]=time_exec; |
| 74 | + cur[i].f=0; |
| 75 | + cnt++; |
| 76 | + } |
| 77 | + else if(cur[i].f>0){ |
| 78 | + if(prev!=i) cs++,prev=i; |
| 79 | + time_exec+=quant; |
| 80 | + cur[i].f-=quant; |
| 81 | + } |
| 82 | + } |
| 83 | + tmp=0; |
| 84 | + } |
| 85 | + int wait_time[n],turn_around[n]; |
| 86 | + for(int i=0;i<n;i++){ |
| 87 | + turn_around[i]=ans[i]-arr_sort[i]; |
| 88 | + wait_time[i]=turn_around[i]-burst_sort[i]; |
| 89 | + } |
| 90 | + for(int i=0;i<n;i++){ |
| 91 | + cout<<turn_around[i]<<" "<<wait_time[i]<<"\n"; |
| 92 | + } |
| 93 | + cout<<"context_switches are "<<cs-1<<"\n"; |
| 94 | + cout<<"cpu idle time is\n"; |
| 95 | + for(int i=0;i<cpu_idle.size();i++){ |
| 96 | + cout<<cpu_idle[i].f<<" "<<cpu_idle[i].s<<"\n"; |
| 97 | + } |
| 98 | +} |
0 commit comments