SJF Scheduling Program in C
Here you will learn SJF Scheduling and, the program code of SJF Scheduling Program in C programming language.
sjf scheduling in os
Shortest Job First (SJF) is a scheduling algorithm used in operating systems to manage processes. It is a non–preemptive algorithm, which means that once a process has started running, it cannot be interrupted until it has completed its task. The aim of SJF is to minimize the average waiting time of processes.
The algorithm works by sorting the processes by their burst time and then scheduling them in the order of shortest burst time first. This ensures that the processes with the shortest burst time are executed first and thus the average waiting time is minimized.
SJF scheduling can be used in preemptive and non-preemptive mode. The preemptive mode of Shortest Job First is called as Shortest Remaining Time First (SRTF).
Non-Preemptive SJF
Process Queue | Burst time | Arrival time |
P1 | 6 | 2 |
P2 | 2 | 5 |
P3 | 8 | 1 |
P4 | 3 | 0 |
P5 | 4 | 4 |
Wait time
P4= 0-0=0
P1= 3-2=1
P2= 9-5=4
P5= 11-4=7
P3= 15-1=14
Average Waiting Time = 0+1+4+7+14/5 = 26/5 = 5.2
Preemptive SJF
Process Queue | Burst time | Arrival time |
P1 | 6 | 2 |
P2 | 2 | 5 |
P3 | 8 | 1 |
P4 | 3 | 0 |
P5 | 4 | 4 |
P4= 0-0=0
P1= (3-2) + 6 =7
P2= 5-5 = 0
P5= 4-4+2 =2
P3= 15-1 = 14
SJF scheduling example
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 | //SJF Scheduling Program in C #include<stdio.h> #include<conio.h> void main() { int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp; float avg_wt,avg_tat; printf("\nEnter number of process:"); scanf("%d",&n); printf("\nEnter Burst Time:n"); for(i=0;i<n;i++) { printf("p%d:",i+1); scanf("%d",&bt[i]); p[i]=i+1; } for(i=0;i<n;i++) { pos=i; for(j=i+1;j<n;j++) { if(bt[j]<bt[pos]) pos=j; } temp=bt[i]; bt[i]=bt[pos]; bt[pos]=temp; temp=p[i]; p[i]=p[pos]; p[pos]=temp; } wt[0]=0; for(i=1;i<n;i++) { wt[i]=0; for(j=0;j<i;j++) wt[i]+=bt[j]; total+=wt[i]; } avg_wt=(float)total/n; total=0; printf("\nProcesst Burst Time \tWaiting Time\tTurnaround Time"); for(i=0;i<n;i++) { tat[i]=bt[i]+wt[i]; total+=tat[i]; printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]); } avg_tat=(float)total/n; //average turnaround time printf("\n\nAverage Waiting Time=%f",avg_wt); printf("\nAverage Turnaround Time=%f\n",avg_tat); } |
Output
Read Also
I got life saving code here, Thanks for help
Thanks, share this website link with your needy friends.