Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Experiment No.

9
Simulation of SJF Scheduling Algorithm

Student Name: Sahil Verma


UID: 20BCS3791
Branch: CSE Big Data
Section/Group: 20BD1/A
Semester: 3rd
Subject Name: Operating System Lab
Subject Code: 20CSP – 232

1. Aim/Overview of the practical:


Write a program and implement SJF Scheduling Algorithm

2. Code:

#include <stdio.h>
using namespace std;
int main()
{
int a[] = { 0, 1, 2},
b[] = {1, 2, 3},
bt[] = {5, 6, 2};

printf("Process\tAT\tBT\t\n");
for(int i=0; i<3; i++)
printf("P%d\t%d\t%d\n", a[i], b[i], bt[i]);
for(int i=0;i<3; i++)
{
for(int j=i+1; j<3; j++)
{
if(bt[i] > bt[j])
{
int temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;

temp = b[i];
b[i] = b[j];
b[j] = temp;

temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

printf("\n\nProcess\tAT\tBT\t\n");
for(int i=0; i<3; i++)
printf("P%d\t%d\t%d\n", a[i], b[i], bt[i]);

int turn[3], complete = 0, waiting[3];

for(int i=0; i<3; i++)


{
complete += bt[i];
if(i != 0)
{
turn[i] = (complete - b[i]);
waiting[i] = turn[i] - bt[i];
}
else
{
turn[i] = 0;
waiting[i] = 0;
}
}

printf("\n\nCompletion time :: %d\n", complete);


printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround
Time\t\n");
for(int i=0; i<3; i++)
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", a[i], b[i], bt[i], waiting[i], turn[i]);

return 0;
}
Output:

Learning outcomes:

1. I learned about scheduling algorithm

2. I learned how to implement SJF algorithm and the applications of SJF

3. I learned about the Turnaround, Finish time and how to find average
Turnaround and Finish time.

4. I learned about the Waiting time and how to find average Waiting time.

5. I learned about the CPU scheduling with the help of this experiment.

You might also like