Ex. No.: 5(a)
|
SIMULATION
OF CPU SCHEDULING ALGORITHMS - SJF
|
AIM
To simulate CPU scheduling, using Shortest Job First(SJF) Scheduling algorithms.
Program Description:
Shortest Job First is a non-preemptive Scheduling discipline in which the waiting job with the smallest estimated run-time-to completion is run next. It depicts the order in which the processes are executed, calculates the turnaround time and average waiting time of the processes based on burst time for SJF scheduling.
Algorithm:
1. Start of algorithm.
2. Declare the required variables
3. Get the process name, and burst time for each process.
4. Arrange the processor in ascending order according to the lowest burst time
5. Calculate the average waiting time and average turn around time using the following formulas.
Average Waiting Time = (wt1+wt2+…+wtn) / n where
Average turn around time = ((wt1+wt2+…+wtn) + (bt1+bt2+…+btn)) / n
6. Print the Process name, average waiting time and average turnaround time for each process.
7. Terminate the program.
Program : Shortest Job First Scheduling Algorithm
#include<stdio.h>
main()
{
int n,i,j,avgw=0;
float avgt=0,temp;
int burst[10],wait[10],process[10];
printf("\nProcess Scheduling(SJFS) :\n");
printf("\nEnter the number of process : ");
scanf("%d",&n);
printf("Enter the Burst time\n");
for(i=0;i<n;i++)
{
printf("Burst time P %d : ",i+1);
scanf("%d",&burst[i]);
process[i] = i;
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(burst[i]>burst[j])
{
temp = burst[i];
burst[i] = burst[j];
burst[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
printf("\nWaiting time | Turn around time \n");
j=0;
for(i=0;i<n;i++)
{
wait[i] = j;
j = burst[i] + j;
printf("\nP%d : %d\t\t%d",process[i]+1,wait[i],wait[i]+burst[i]);
avgw+=wait[i];
avgt+=wait[i]+burst[i];
}
printf("\n\n");
printf("Average waiting time : %f\nAverage Turn around time : %f\n\n"
,avgw/(float)n,avgt/(float)n);
}
/*Output :
Process Scheduling(SJFS) :
Enter the number of process : 3
Enter the Burst time
Burst time P1 : 24
Burst time P2 : 3
Burst time P3 : 3
Waiting time | Turn around time
P2 : 0 3
P3 : 3 6
P1 : 6 30
Average waiting time : 3.000000
Average Turn around time : 13.000000*/
No comments:
Post a Comment