PROGRAM NO - 5
Objective: Write a program in C to implement Shortest Remaining Time First (SRTF)
scheduling algorithm.
Description: To calculate the average waiting time in the shortest remaining time first
algorithm the process with the smallest amount of time remaining until completion is
selected to execute. Since the currently executing process is the one with the shortest
amount of time remaining by definition, and since that time should only reduce as
execution progresses, processes will always run until they complete or a new process
is added that requires a smaller amount of time
Algorithm:
Traverse until all process gets completely executed.
•Find process with minimum remaining time at every single time lap.
•Reduce its time by 1.
•Check if its remaining time becomes 0
•Increment the counter of process completion.
•Completion time of current process = current_time + 1;
•Calculate waiting time for each completed process.
•wt[i]= Completion time – arrival_time-burst_time
•Increment time lap by one.
•Find turnaround time (waiting_time + burst_time).
Program:
#include <stdio.h>
int main()
{
int n, ari[10], bur[10], total = 0, i, j, small, temp, procs[100], k, waiting[10],
finish[10];
float tavg = 0.0, wavg = 0.0;
printf("ENTER THE NUMBER OF PROCESSES:");
scanf("%d", & n);
for (i = 0; i < n; i++)
{
printf("ENTER THE ARRIVAL TIME OF PROCESS %d:\t", i);
scanf("%d", & ari[i]);
printf("ENTER THE BURST TIME OF PROCESS %d:\t", i);
scanf("%d", & bur[i]);
waiting[i] = 0;
total += bur[i];
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (ari[i] > ari[j])
{
temp = ari[i];
ari[i] = ari[j];
ari[j] = temp;
temp = bur[i];
bur[i] = bur[j];
bur[j] = temp;
}
}
}
for (i = 0; i < total; i++)
{
small = 3200;
for (j = 0; j < n; j++)
{
if ((bur[j] != 0) && (ari[j] <= i) && (bur[j] < small))
{
small = bur[j];
k = j;
}
}
bur[k]--;
procs[i] = k;
}
k = 0;
for (i = 0; i < total; i++)
{
for (j = 0; j < n; j++)
{
if (procs[i] == j)
{
finish[j] = i;
waiting[j]++;
}
}
}
for (i = 0; i < n; i++)
{
printf("\n PROCESS %d:-FINISH TIME==> %d TURNAROUND TIME==>%d
WAITING TIME==>%d\n", i + 1, finish[i] + 1, (finish[i] - ari[i]) + 1, (((finish[i] + 1)
-
waiting[i]) - ari[i]));
wavg = wavg + (((finish[i] + 1) - waiting[i]) - ari[i]);
tavg = tavg + ((finish[i] - ari[i]) + 1);
}
printf("\n WAvG==>\t%f\n TAVG==>\t%f\n", (wavg / n), (tavg / n));
return 0;
}
Output:
ENTER THE BURST TIME OF PROCESS 0: 2
ENTER THE ARRIVAL TIME OF PROCESS 1: 4
ENTER THE BURST TIME OF PROCESS 1: 8
ENTER THE ARRIVAL TIME OF PROCESS 2: 2
ENTER THE BURST TIME OF PROCESS 2: 6
ENTER THE ARRIVAL TIME OF PROCESS 3: 3
ENTER THE BURST TIME OF PROCESS 3: 4
ENTER THE ARRIVAL TIME OF PROCESS 4: 1
ENTER THE BURST TIME OF PROCESS 4: 10
ENTER THE ARRIVAL TIME OF PROCESS 5: 0
ENTER THE BURST TIME OF PROCESS 5: 15
PROCESS 1:-FINISH TIME==> 45 TURNAROUND TIME==>45 WAITING
TIME==>30
PROCESS 2:-FINISH TIME==> 31 TURNAROUND TIME==>30 WAITING
TIME==>20
PROCESS 3:-FINISH TIME==> 14 TURNAROUND TIME==>12 WAITING
TIME==>6
PROCESS 4:-FINISH TIME==> 7 TURNAROUND TIME==>4 WAITING
TIME==>0
PROCESS 5:-FINISH TIME==> 22 TURNAROUND TIME==>18 WAITING
TIME==>10
PROCESS 6:-FINISH TIME==> 9 TURNAROUND TIME==>4 WAITING
TIME==>2
WAvG==> 11.333333
TAVG==> 18.83333