Skip to content

Commit 8e230fd

Browse files
committed
[OS] Added Scheduling Algorithms
1 parent bb6fd38 commit 8e230fd

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int at[100],bt[100],rt[100],endTime,i,smallest,flag=0,idle_time=0,context_switches=0;
5+
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
6+
printf("Enter no of Processes : ");
7+
scanf("%d",&n);
8+
for(i=0;i<n;i++)
9+
{
10+
printf("Enter arrival time for Process P%d : ",i+1);
11+
scanf("%d",&at[i]);
12+
printf("Enter burst time for Process P%d : ",i+1);
13+
scanf("%d",&bt[i]);
14+
rt[i]=bt[i];
15+
}
16+
printf("\n\nProcess\t|Turnaround Time| Waiting Time\n\n");
17+
rt[99]=10000;
18+
int prev_smallest = 10000;
19+
for(time=0;remain!=n;time++)
20+
{ prev_smallest=smallest;
21+
smallest=101;
22+
flag=0;
23+
for(i=0;i<n;i++)
24+
{
25+
if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)
26+
{
27+
flag++;
28+
smallest=i;
29+
}
30+
}
31+
if(flag==0)
32+
{
33+
idle_time++;
34+
}
35+
if(prev_smallest!=smallest)
36+
{
37+
context_switches++;
38+
}
39+
rt[smallest]--;
40+
if(rt[smallest]==0)
41+
{
42+
remain++;
43+
endTime=time+1;
44+
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-bt[smallest]-at[smallest]);
45+
// sum_wait+=endTime-bt[smallest]-at[smallest];
46+
// sum_turnaround+=endTime-at[smallest];
47+
}
48+
}
49+
// printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);
50+
// printf("Average Turnaround time = %f",sum_turnaround*1.0/5);
51+
printf("Idle Time: %d\n", idle_time);
52+
return 0;
53+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)