# Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++

In this article, we are going to learn about **implementation of shortest job first (SJF) Non-preemptive scheduling algorithm using C++ program**.

Submitted by Aleesha Ali, on January 26, 2018

**Non-preemptive:** We cannot remove a process until it completes it execution.

Scheduling criteria tells us that any algorithm is how much efficient, the main criteria of scheduling are given below:

- CPU Utilization
- Throughput
- Arrival time
- Turn around time
- Waiting time
- Completion time
- Burst time

*Ready Queue is a queue where all the processes wait to get CPU for its execution.

**CPU Utilization:** The amount of time CPU is busy.

**Throughput:** The number of process computed per unit time.

**Arrival time:** The time at which the process enters into ready queue.

**Turn around time:** The interval between the time of submission of a process to the time of completion.

**Waiting time:** The total amount of the time a process spends in ready queue.

**Completion time:** The time at which process completes its execution.

**Burst time:** The time needed by CPU to completes its execution.

## Shortest Job First Algorithm

In this scheduling algorithm the process having minimum burst time will execute first.

### C++ Program for SJF scheduling

//Implementation fo SHORTEST JOB FIRST Using C++ #include <iostream> #include <algorithm> using namespace std; int ab; typedef struct schedule { string pro_id; int at,bt,ct,ta,wt; /* artime = Arrival time, bt = Burst time, ct = Completion time, ta = Turn around time, wt = Waiting time */ }schedule; bool compare(schedule a,schedule b) { return a.at < b.at; /* This process will always return TRUE if above condition comes*/ } bool compare2(schedule a,schedule b) { return a.bt < b.bt && a.at <= ab; /* This process will always return TRUE if above condition comes*/ } int main() { schedule pro[10]; //An array of Processes int n,i,j; //n = number of processes, i= iteration variable cout<<"Enter the number of schedule::"; cin>>n; cout<<"Enter the schedule id arrival time burst time :::"; for(i=0;i<n;i++) { cin>>pro[i].pro_id; cin>>pro[i].at; cin>>pro[i].bt; } /*sort is a predefined funcion defined in algorithm.h header file, it will sort the processes according to their arrival time*/ sort(pro,pro+n,compare); // initial values pro[0].ct=pro[0].bt+pro[0].at; pro[0].ta=pro[0].ct-pro[0].at; pro[0].wt=pro[0].ta-pro[0].bt; for(i=1;i<n;i++) { ab=pro[i-1].ct; sort(pro+i,pro+n,compare2); if(pro[i-1].ct<pro[i].at) { pro[i].ct=pro[i-1].ct+pro[i].bt+(pro[i].at-pro[i-1].ct); } else { pro[i].ct=pro[i-1].ct+pro[i].bt; } pro[i].ta=pro[i].ct-pro[i].at; pro[i].wt=pro[i].ta-pro[i].bt; } for(i=0;i<n;i++) { //before executing make it in one statement cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].bt <<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<pro[i].wt; cout<<endl; } return 0; }

**Output**

Related Tutorials

- Introduction to Algorithms
- Introduction to Greedy Strategy in Algorithms
- Stability in sorting
- External Merge Sorting Algorithm
- Radix Sort and its Algorithm
- Bucket Sort Algorithm
- Bubble sort Algorithm, Flow Chart and C++ Code
- Insertion sort Algorithm, flowchart and C, C++ Code
- Merge Sort | One of the best sorting algorithms used for large inputs
- Binary Search in C, C++
- Randomized Binary Search
- Meta Binary Search | One-sided Binary Search
- Difference between Linear Search and Binary Search
- Binary Search in String
- Variants of Binary Search
- Sieve of Eratosthenes to find prime numbers
- Optimal Merge Pattern (Algorithm and Example)
- Given an array of n numbers, Check whether there is any duplicate or not
- Finding the missing number
- Find the number occurring an odd number of times
- Find the pair whose sum is closest to zero in minimum time complexity
- Find three elements in an array such that their sum is equal to given element K
- Bitonic Search Algorithm
- Check whether a number is Fibonacci or not
- Segregate even and odd numbers in minimum time complexity
- Find trailing zeros in factorial of a number
- Find Nearest Greatest Neighbours of each element in an array
- Interpolation search algorithm
- Floor and ceil of an element in an array using C++
- Two Elements whose sum is closest to zero
- Find a pair with a given difference
- Count number of occurrences (or frequency) in a sorted array
- Find a Fixed Point (Value equal to index) in a given array
- Find the maximum element in an array which is first increasing and then decreasing
- Dynamic Programming (Components, Applications and Elements)
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Find the Nth Fibonacci number | C++
- Longest Common Subsequence using Dynamic programming (DP)
- Longest Increasing Subsequence using Dynamic programming (DP)
- Find the maximum sub-array sum using KADANE'S ALGORITHM
- Non-intersecting chords using Dynamic Programming (DP)
- Edit Distance using Dynamic Programming (DP)
- Finding Ugly Number using Dynamic Programming (DP)
- Egg dropping problem using Dynamic Programming (DP)
- Wild card matching problem using Dynamic programming (DP)
- Compute sum of digits in all numbers from 1 to N for a given N
- Minimum jumps required using Dynamic programming (DP)
- Graph coloring problem's solution using backtracking algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- Travelling Salesman Problem
- Kruskal's (P) and Prim's (K) Algorithms
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code

- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Find the GCD (Greatest Common Divisor) of two numbers using EUCLID'S ALGORITHM
- Compute the value of A raise to the power B using Fast Exponentiation
- Implement First Come First Served (FCFS) CPU Scheduling Algorithm using C program
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Analysis of LRU page replacement algorithm and Belady's anomaly
- Branch and Bound
- Find the roots of a complex polynomial equation using Regula Falsi Method in C
- Sieve of Eratosthenes to find prime numbers
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Divide and Conquer Paradigm (What it is, Its Applications, Pros and Cons)
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Jump Search Implementation using C++
- Optimal Merge Pattern (Algorithm and Example)
- Introduction to Greedy Strategy in Algorithms
- Strassen's Matrix Multiplication in algorithms
- Huffman Coding (Algorithm, Example and Time complexity)
- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Graph coloring problem's solution using backtracking algorithm
- Tournament Tree and their properties
- Deterministic and Non Deterministic Algorithms
- Lower Bound Theory
- Non Recursive Tree Traversal Algorithm
- Line Drawing Algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- P and NP problems and solutions | Algorithms
- Travelling Salesman Problem
- 2 – 3 Trees Algorithm
- Kruskal's (P) and Prim's (K) Algorithms
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Midpoint Circle Algorithm
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code
- Reliability design problem
- Removing consecutive duplicates from a string
- Fast Exponentiation using Bitmasking

Comments and Discussions!