Created
May 13, 2020 16:29
-
-
Save projjal1/0d34584794f4043532b9ab6427e3d8de to your computer and use it in GitHub Desktop.
SRTF scheduling algorithm in C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment