-
Notifications
You must be signed in to change notification settings - Fork 10
/
SRTF Scheduling Algorithm.c
59 lines (59 loc) · 1.5 KB
/
SRTF Scheduling Algorithm.c
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
#include<stdio.h>
#define MAX 9999
struct proc
{
int no,at,bt,rt,ct,tat,wt;
};
struct proc read(int i)
{
struct proc p;
printf("\nProcess No: %d\n",i);
p.no=i;
printf("Enter Arrival Time: ");
scanf("%d",&p.at);
printf("Enter Burst Time: ");
scanf("%d",&p.bt);
p.rt=p.bt;
return p;
}
int main()
{
struct proc p[10],temp;
float avgtat=0,avgwt=0;
int n,s,remain=0,time;
printf("<--SRTF Scheduling Algorithm (Preemptive)-->\n");
printf("Enter Number of Processes: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
p[i]=read(i+1);
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(p[j].at>p[j+1].at)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
printf("\nProcess\t\tAT\tBT\tCT\tTAT\tWT\n");
p[9].rt=MAX;
for(time=0;remain!=n;time++)
{
s=9;
for(int i=0;i<n;i++)
if(p[i].at<=time&&p[i].rt<p[s].rt&&p[i].rt>0)
s=i;
p[s].rt--;
if(p[s].rt==0)
{
remain++;
p[s].ct=time+1;
p[s].tat=p[s].ct-p[s].at;
avgtat+=p[s].tat;
p[s].wt=p[s].tat-p[s].bt;
avgwt+=p[s].wt;
printf("P%d\t\t%d\t%d\t%d\t%d\t%d\n",p[s].no,p[s].at,p[s].bt,p[s].ct,p[s].tat,p[s].wt);
}
}
avgtat/=n,avgwt/=n;
printf("\nAverage TurnAroundTime=%f\nAverage WaitingTime=%f",avgtat,avgwt);
}