-
Notifications
You must be signed in to change notification settings - Fork 5
/
SRTF.c
60 lines (57 loc) · 1.41 KB
/
SRTF.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
// C Implemantion of Shortest Remaining Time First(SRTF)
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],x[10];
int waiting[10],turnaround[10],completion[10];
int i,j,smallest,count=0,time,n;
double avg=0,tt=0,end;
printf("\nEnter the number of Processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter arrival time of process %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("\nEnter burst time of process %d : ",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
x[i]=b[i];
b[9]=9999;
//printf("time => process number");
for(time=0;count!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(a[i]<=time && b[i]<b[smallest] && b[i]>0 )
smallest=i;
}
b[smallest]--;
//printf("\n%d => p%d",time+1,smallest);
if(b[smallest]==0)
{
count++;
end=time+1;
completion[smallest] = end;
waiting[smallest] = end - a[smallest] - x[smallest];
turnaround[smallest] = end - a[smallest];
// printf("\n %d %d %d",smallest,wt[smallest],ttp[smallest]);
}
}
printf("pid \t burst \t arrival \twaiting \tturnaround \tcompletion");
for(i=0;i<n;i++)
{
printf("\n %d \t %d \t %d\t\t%d \t\t%d\t\t%d",i+1,x[i],a[i],waiting[i],turnaround[i],completion[i]);
avg = avg + waiting[i];
tt = tt + turnaround[i];
}
printf("\n %If %If",avg,tt);
printf("\n\nAverage waiting time = %lf\n",avg/n);
printf("Average Turnaround time = %lf",tt/n);
getch();
}