# C++ Program for Shortest Job First (SJF) scheduling(non-preemptive)

Given process, the burst time of a process respectively and a quantum limit; the task is to find and print the waiting time, turnaround time and their respective average time using Shortest Job First Scheduling non-preemptive method.

What is the shortest job first scheduling?

Shortest job first scheduling is the job or process scheduling algorithm that follows the nonpreemptive scheduling discipline. In this, scheduler selects the process from the waiting queue with the least completion time and allocate the CPU to that job or process. Shortest Job First is more desirable than FIFO algorithm because SJF is more optimal as it reduces average wait time which will increase the throughput.

What is the Turnaround time, waiting time and completion time?

• Completion Time is the time required by the process to complete its execution
• Turnaround Time is the time interval between the submission of a process and its completion.

Turnaround Time = completion of a process – submission of a process

• Waiting Time is the difference between turnaround time and burst time

Waiting Time = turnaround time – burst time

## Example

We are given with the processes P1, P2, P3, P4 and P5 having their corresponding burst time given below

ProcessBurst Time
P14
P22
P38
P41
P59

Since the burst time of process P4 is minimum amongst all the processes it will be allocated CPU first. After that, P2 will be queued then P1, P3 and P5. Average waiting time is calculated on the basis of gantt chart. P1 have to wait for 3, P2 have to wait for 1, P3 have to wait for 7, P4 have to wait for 0 and P5 have to wait for 15. So, their average waiting time will be − ## Algorithm

Start
Step 1-> In function swap(int *a, int *b)
Set temp = *a
Set *a = *b
Set *b = temp
Step 2-> In function arrangeArrival(int num, int mat[])
Loop For i=0 and i<num and i++
Loop For j=0 and j<num-i-1 and j++
If mat[j] > mat[j+1] then,
For k=0 and k<5 and k++
Call function swap(mat[k][j], mat[k][j+1])
Step 3-> In function completionTime(int num, int mat[])
Declare temp, val
Set mat = mat + mat
Set mat = mat - mat
Set mat = mat - mat
Loop For i=1 and i<num and i++
Set temp = mat[i-1]
Set low = mat[i]
Loop For j=i and j<num and j++
If temp >= mat[j] && low >= mat[j] then,
Set low = mat[j]
Set val = j
Set mat[val] = temp + mat[val]
Set mat[val] = mat[val] - mat[val]
Set mat[val] = mat[val] - mat[val]
Loop For  k=0; k<6; k++
Call function swap(mat[k][val], mat[k][i])
Step 4-> In function int main()
Declare and set num = 3, temp
Declare and set mat = {1, 2, 3, 3, 6, 4, 2, 3, 4}
Print Process ID, Arrival Time, Burst Time
Loop For i=0 and i<num and i++
Print the values of mat[i], mat[i], mat[i]
Call function arrangeArrival(num, mat)
Call function completionTime(num, mat)
Print Process ID, Arrival Time, Burst Time, Waiting Time, Turnaround Time
Loop For i=0 and i<num and i++
Print the values of  mat[i], mat[i], mat[i], mat[i], mat[i]
Stop

## Example

Live Demo

// C++ program to implement Shortest Job first with Arrival Time
#include<iostream>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void arrangeArrival(int num, int mat[]) {
for(int i=0; i<num; i++) {
for(int j=0; j<num-i-1; j++) {
if(mat[j] > mat[j+1]) {
for(int k=0; k<5; k++) {
swap(mat[k][j], mat[k][j+1]);
}
}
}
}
}
void completionTime(int num, int mat[]) {
int temp, val;
mat = mat + mat;
mat = mat - mat;
mat = mat - mat;
for(int i=1; i<num; i++) {
temp = mat[i-1];
int low = mat[i];
for(int j=i; j<num; j++) {
if(temp >= mat[j] && low >= mat[j]) {
low = mat[j];
val = j;
}
}
mat[val] = temp + mat[val];
mat[val] = mat[val] - mat[val];
mat[val] = mat[val] - mat[val];
for(int k=0; k<6; k++) {
swap(mat[k][val], mat[k][i]);
}
}
}
int main() {
int num = 3, temp;
int mat = {1, 2, 3, 3, 6, 4, 2, 3, 4};
cout<<"Before Arrange...\n";
cout<<"Process ID\tArrival Time\tBurst Time\n";
for(int i=0; i<num; i++) {
cout<<mat[i]<<"\t\t"<<mat[i]<<"\t\t"<<mat[i]<<"\n";
}
arrangeArrival(num, mat);
completionTime(num, mat);
cout<<"Final Result...\n";
cout<<"Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n";
for(int i=0; i<num; i++) {
cout<<mat[i]<<"\t\t"<<mat[i]<<"\t\t"<<mat[i]<<"\t\t"<<mat[i]<<"\t\t"<<mat[i]<<"\n";
}
}

## Output 