Professional Documents
Culture Documents
Ui22Cs57 Lab 5 Tejkumar
Ui22Cs57 Lab 5 Tejkumar
AIM: Implement SJF and SRTF CPU Scheduling Algorithm and find the average
waiting and turnaround time. Validate the program outcomes with different test
cases.
Shortest Job First(SJF): The shortest job first (SJF) or shortest job next, is a
scheduling policy that selects the waiting process with the smallest execution
time to execute next. SJN, also known as Shortest Job Next (SJN), can be
preemptive or non-preemptive.
ALGORITHM:
1. Prompt the user to enter the number of processes,process ID(s), Arrival Time
and Burst Time of processes respectively.
2. Use a function ‘sjf_scheduling’ which works according to the SJF CPU
scheduling algorithm to iterate through burst time and sort out the burst time of all
the processes according to the process available.
3. Completion time of processes are obtained by the total time respectively. 4.
Use a loop to iterate through Turn Around Time array to find TAT values for
give data by utilizing the formula: Turn_Around_Time=Completion Time-Arrival
Time.
5. Use a loop to iterate through Waiting Time array to find WT values for give
data by utilizing the formula: Waiting_Time=Turn Around Time-Burst Time.
6.Find the average waiting time and average turnaround time. 7.Print the
above resulting values generated from the average as the required answer.
CODE:-
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int executed[n];
for (int i = 0; i < n; i++) {
executed[i] = 0;
}
int total_time = 0; // Total time elapsed
UI22CS57 LAB 5 TEJKUMAR
int completed = 0; // Number of completed processes
while (completed < n) {
int shortest_job = -1;
int shortest_time = INT_MAX;
for (int i = 0; i < n; i++) {
if (!executed[i] && AT[i] <= total_time && BT[i] < shortest_time) {
shortest_job = i;
shortest_time = BT[i];
}
}
if (shortest_job == -1) {
total_time++;
}
total_time += BT[shortest_job];
CT[shortest_job]=total_time;
TAT[shortest_job] = CT[shortest_job] - AT[shortest_job];
WT[shortest_job] = TAT[shortest_job] - BT[shortest_job];
executed[shortest_job] = 1;
completed++;
}
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int P_ID[n];
float BT[n],AT[n],WT[n],TAT[n],CT[n];
printf("Enter burst times and arrival times for each process:\n");
for (int i = 0; i < n; i++) {
P_ID[i] = i + 1;
scanf("%f", &AT[i]);
printf("Burst time for Process %d: ", i + 1);
scanf("%f", &BT[i]);
}
UI22CS57 LAB 5 TEJKUMAR
Case2-:
UI22CS57 LAB 5 TEJKUMAR
CODE-:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
UI22CS57 LAB 5 TEJKUMAR
void srtf_scheduling(int P_ID[], float BT[], float AT[], float CT[], int n, float WT[],
float TAT[]) {
int executed[n];
float Temp[n];
for (int i = 0; i < n; i++) {
executed[i] = 0;
Temp[i]=BT[i];
}
int completed = 0; // Number of completed processes
while (completed < n) {
int shortest_job = -1;
int shortest_time = INT_MAX;
for (int i = 0; i < n; i++) {
if (!executed[i] && AT[i] <= total_time && Temp[i] < shortest_time) {
shortest_job = i;
shortest_time = Temp[i];
}
}
if (shortest_job == -1) {
total_time++;
}
else {
++total_time;
--Temp[shortest_job];
if(Temp[shortest_job]==0){
CT[shortest_job]=total_time
WT[shortest_job] = TAT[shortest_job] - BT[shortest_job];
executed[shortest_job] = 1;
completed++;
}
}
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
UI22CS57 LAB 5 TEJKUMAR
int P_ID[n];
float BT[n],AT[n],WT[n],TAT[n],CT[n];
printf("Enter burst times and arrival times for each process:\n");
for (int i = 0; i < n; i++) {
P_ID[i] = i + 1;
printf("Arrival time for Process %d: ", i + 1);
scanf("%f", &AT[i]);
scanf("%f", &BT[i]);
}
srtf_scheduling(P_ID, BT, AT, CT, n, WT, TAT);
float avg_WT = 0;
float avg_TAT = 0;
for (int i = 0; i < n; i++) {
avg_WT += WT[i];
avg_TAT += TAT[i];
}
avg_WT /= n;
avg_TAT /= n;
printf("\nProcess\tArrival Time\tBurst Time\tCompletion time\tTurnAround
Time\tWaiting
Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n", P_ID[i], AT[i], BT[i], CT[i],
TAT[i], WT[i]);
}
printf("\nAverage Waiting Time: %.2f\n", avg_WT);
printf("Average Turnaround Time: %.2f\n", avg_TAT);
return 0;
OUTPUT:-
UI22CS57 LAB 5 TEJKUMAR
Case1-:
Case2:-