Shortest Remaining Time First ( SRTF ) or preemptive sjf cpu scheduling in c
On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
code in C:#include <stdio.h> int main() { int a[10],b[10],x[10],i,j,smallest,count=0,time,n; double avg=0,tt=0,end; printf("enter the number of Processes:\n"); scanf("%d",&n); printf("enter arrival time\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter burst time\n"); for(i=0;i<n;i++) scanf("%d",&b[i]); for(i=0;i<n;i++) x[i]=b[i]; b[9]=9999; 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]--; if(b[smallest]==0) { count++; end=time+1; avg=avg+end-a[smallest]-x[smallest]; tt= tt+end-a[smallest]; } } printf("\n\nAverage waiting time = %lf\n",avg/n); printf("Average Turnaround time = %lf",tt/n); return 0; }output:-
enter the number of Processes: 4 enter arrival time 0 1 2 3 enter burst time 8 4 9 5 Average waiting time = 6.500000 Average Turnaround time = 13.000000 --------------------------------
0 comments: