You are on page 1of 4

LAB 6:

Write a C program to implement the SRTF (shortest remaining time first)


scheduling algorithm.

SOURCE CODE
//SJFS -preemptive
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int at[n], bt[n], ct[n], at_copy[n], bt_copy[n], val, max_time = 0;
printf("Enter the arrival time and burst time for the processes:\n");
for(int i=0; i<n; i++){
printf("\nEnter arrival time for process[%d] ", i+1);
scanf("%d", &val);
at[i] = val;
if(max_time < val)
max_time = val;
at_copy[i] = val;
printf("Enter burst time for process[%d] ", i+1);
scanf("%d", &val);
bt[i] = val;
bt_copy[i] = val;
}
int time = 0, i = 0, process = 0;
while(process != n)
{
if(at[i] <= time && at[i] != -1)

Harshit Chaturvedi Sec-C 27/1018494


{
int min_bt = bt[i],
pos = i;
for(int j=i+1; j<n; j++){
if(at[j] <= time && bt[j] < min_bt && at[j] != -1)
{
min_bt = bt[j];
pos = j;
}
}
if(time < max_time)
{
bt[pos] -= 1;
time += 1;
i = 0;
if(bt[pos] == 0)
{
ct[pos] = time;
at[pos] = -1;
process += 1;
}
}
else
{
time += bt[pos];
ct[pos] = time;
i = 0;
process += 1;
at[pos] = -1;

Harshit Chaturvedi Sec-C 27/1018494


else
i += 1;
if(i == n)
{
time += 1;
i = 0;
}
}
printf("Solution\n");
printf("P#\tAT\tBT\tCT\tTAT\tWT\n"); int total_tat = 0, total_wt = 0;
for(int i=0; i<n; i++)
{
int val = ct[i] - at_copy[i]; total_tat += val;
total_wt += val - bt_copy[i];
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i+1, at_copy[i], bt_copy[i], ct[i], val, val -
bt_copy[i]);
}
printf("Average TAT = %f\n", total_tat / (float)n);
printf("Average WT = %f\n", total_wt / (float)n);
return 0;
}

Harshit Chaturvedi Sec-C 27/1018494


OUTPUT

Harshit Chaturvedi Sec-C 27/1018494

You might also like