1. Introduction to SJF Scheduling

Definition

Shortest Job First (SJF) scheduling is a scheduling algorithm used in operating systems to minimize the average waiting time for processes. It is a non-preemptive algorithm, which means that once a process is assigned the CPU, it will continue to execute until it completes or is blocked by an I/O operation.

SJF scheduling works by selecting the process with the shortest expected processing time from the queue of waiting processes. This means that the algorithm gives priority to smaller jobs, as they will spend less time waiting in the queue.

Key Concepts

  • Burst time: The time required by a process to complete its execution.
  • Completion time: The time when a process finishes execution.
  • Turnaround time: The time between the submission of a process and its completion.
  • Waiting time: The time spent by a process waiting in the queue before it is assigned the CPU.

SJF Scheduling Program

C++
#include <iostream>
#include <algorithm>
using namespace std;

struct Process {
    int pid; // process id
    int bt; // burst time
    int wt; // waiting time
    int tat; // turnaround time
};

bool compare(Process p1, Process p2) {
    return p1.bt < p2.bt;
}

void sjfScheduling(Process proc[], int n) {
    sort(proc, proc + n, compare);
    int wt[n], tat[n], total_wt = 0, total_tat = 0;
    wt[0] = 0;
    tat[0] = proc[0].bt;
    for(int i = 1; i < n; i++) {
        wt[i] = wt[i-1] + proc[i-1].bt;
        tat[i] = tat[i-1] + proc[i].bt;
    }
    for(int i = 0; i < n; i++) {
        proc[i].wt = wt[i];
        proc[i].tat = tat[i];
        total_wt += wt[i];
        total_tat += tat[i];
    }
    float avg_wt = (float) total_wt / n;
    float avg_tat = (float) total_tat / n;
    cout << "Average waiting time = " << avg_wt << endl;
    cout << "Average turnaround time = " << avg_tat << endl;
}

int main() {
    int n;
    cout << "Enter the number of processes: ";
    cin >> n;
    Process proc[n];
    for(int i = 0; i < n; i++) {
        cout << "Enter the burst time for process " << i << ": ";
        cin >> proc[i].bt;
        proc[i].pid = i;
    }
    sjfScheduling(proc, n);
    return 0;
}
Java
import java.util.*;

public class SJFScheduling {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of processes: ");
        int n = scanner.nextInt();

        int[] bt = new int[n];
        for (int i = 0; i < n; i++) {
            System.out.print("Enter the burst time of process " + (i + 1) + ": ");
            bt[i] = scanner.nextInt();
        }

        PriorityQueue<Process> pq = new PriorityQueue<>(n, new Comparator<Process>() {
            @Override
            public int compare(Process p1, Process p2) {
                return p1.bt - p2.bt;
            }
        });

        for (int i = 0; i < n; i++) {
            pq.add(new Process(i + 1, bt[i]));
        }

        int time = 0;
        System.out.println("Process Execution Order:");
        while (!pq.isEmpty()) {
            Process p = pq.poll();
            System.out.print("P" + p.pid + " ");
            time += p.bt;
        }

        scanner.close();
    }

    static class Process {
        int pid;
        int bt;

        public Process(int pid, int bt) {
            this.pid = pid;
            this.bt = bt;
        }
    }
}
Python
# SJF Scheduling in Python

def sjf_scheduling(processes, burst_time):
    n = len(processes)
    waiting_time = [0] * n
    turnaround_time = [0] * n
    
    # Sort the processes based on their burst time
    for i in range(n):
        for j in range(i+1, n):
            if burst_time[i] > burst_time[j]:
                burst_time[i], burst_time[j] = burst_time[j], burst_time[i]
                processes[i], processes[j] = processes[j], processes[i]
    
    # Calculate the waiting and turnaround time
    for i in range(1, n):
        waiting_time[i] = burst_time[i-1] + waiting_time[i-1]
        turnaround_time[i] = burst_time[i] + waiting_time[i]
    
    # Calculate the average waiting and turnaround time
    avg_waiting_time = sum(waiting_time) / n
    avg_turnaround_time = sum(turnaround_time) / n
    
    # Print the results
    print("Process\tBurst Time\tWaiting Time\tTurnaround Time")
    for i in range(n):
        print(processes[i], "\t\t", burst_time[i], "\t\t", waiting_time[i], "\t\t", turnaround_time[i])
    print("Average Waiting Time:", avg_waiting_time)
    print("Average Turnaround Time:", avg_turnaround_time)

# Sample inputs
processes = ['P1', 'P2', 'P3', 'P4', 'P5']
burst_time = [3, 5, 1, 4, 2]

# Call the function
sjf_scheduling(processes, burst_time)

2. Advantages and Disadvantages of SJF Scheduling

Advantages

  • SJF scheduling minimizes the average waiting time for processes, which leads to better system performance and increased throughput.
  • It ensures that shorter processes are completed faster, which reduces the overall response time of the system.
  • SJF scheduling can also be used to ensure fair allocation of resources, as it prioritizes smaller jobs.

Disadvantages

  • SJF scheduling can cause starvation for longer processes, as they may spend a long time waiting in the queue behind smaller processes.
  • It is difficult to predict the exact burst time for a process, which can lead to inaccurate scheduling decisions.
  • SJF scheduling may not be suitable for real-time systems, as it does not take into account the deadlines of processes.

3. Preemptive vs. Non-Preemptive SJF Scheduling

Preemptive SJF Scheduling

In preemptive SJF scheduling, a running process can be interrupted by a new process with a shorter burst time. This means that the scheduler constantly monitors the waiting processes and switches to the one with the shortest remaining burst time.

Preemptive SJF scheduling is more complex than non-preemptive SJF scheduling, as it requires the scheduler to handle context switching and scheduling decisions in real-time.

Non-Preemptive SJF Scheduling

Non-preemptive SJF scheduling is simpler than preemptive SJF scheduling, as it does not require real-time monitoring of the waiting processes. However, it may not be suitable for systems where new processes arrive frequently, as longer processes may be starved.

4. Variations of SJF Scheduling

Dynamic SJF Scheduling

Dynamic SJF scheduling is a variant of SJF scheduling that takes into account the actual burst time of a process, rather than an estimated burst time. This means that the scheduler adjusts the priorities of processes based on their actual execution time, rather than their expected execution time.

Dynamic SJF scheduling can improve the accuracy of scheduling decisions, as it takes into account the actual behavior of processes. However, it requires more frequent updates of process priorities, which can reduce system performance.

Weighted SJF Scheduling

Weighted SJF scheduling is a variant of SJF scheduling that assigns weights to processes based on their priority. This means that the scheduler selects the process with the shortest expected processing time, taking into account its priority.

Weighted SJF scheduling can be used to prioritize certain processes over others, based on their importance or criticality. However, it requires more complex calculations of process priorities, which can reduce system performance.

Guaranteed SJF Scheduling

Guaranteed SJF scheduling is a variant of SJF scheduling that ensures that every process is executed within a guaranteed time limit. This means that the scheduler assigns priorities to processes based on their deadline, as well as their expected processing time.

Guaranteed SJF scheduling can be used in real-time systems, where processes have strict deadlines that must be met. However, it requires more complex calculations of process priorities, which can reduce system performance.

5. Real-World Applications of SJF Scheduling

Web Server Request Processing

SJF scheduling can be used to process incoming requests to a web server, where each request represents a process. By prioritizing shorter requests, the server can respond faster to user requests, leading to better user experience.

Multi-User Operating Systems

SJF scheduling can be used in multi-user operating systems, where multiple users are competing for CPU time. By prioritizing smaller processes, the operating system can ensure fair allocation of resources, leading to better system performance.

Database Query Processing

SJF scheduling can be used to process incoming queries to a database, where each query represents a process. By prioritizing shorter queries, the database can respond faster to user requests, leading to better user experience.

6. Conclusion

SJF scheduling is a widely used scheduling algorithm that aims to minimize the average waiting time for processes. It prioritizes smaller processes over longer processes, which leads to better system performance and increased throughput. SJF scheduling has several variations, including preemptive and non-preemptive SJF scheduling, dynamic SJF scheduling, weighted SJF scheduling, and guaranteed SJF scheduling. SJF scheduling has real-world applications in web server request processing, multi-user operating systems, and database query processing.

7. FAQs

Q1. What is SJF scheduling?

SJF scheduling is a scheduling algorithm used in operating systems to minimize the average waiting time for processes. It selects the process with the shortest expected processing time from the queue of waiting processes.

Q2. What are the advantages of SJF scheduling?

SJF scheduling minimizes the average waiting time for processes, ensures fair allocation of resources, and reduces the response time of the system.

Q4. What are the variations of SJF scheduling?

There are several variations of SJF scheduling, including preemptive and non-preemptive SJF scheduling, dynamic SJF scheduling, weighted SJF scheduling, and guaranteed SJF scheduling.

Q5. What are the real-world applications of SJF scheduling?

SJF scheduling has real-world applications in web server request processing, multi-user operating systems, and database query processing

# SJF Scheduling in Python

def sjf_scheduling(processes, burst_time):
    n = len(processes)
    waiting_time = [0] * n
    turnaround_time = [0] * n
    
    # Sort the processes based on their burst time
    for i in range(n):
        for j in range(i+1, n):
            if burst_time[i] > burst_time[j]:
                burst_time[i], burst_time[j] = burst_time[j], burst_time[i]
                processes[i], processes[j] = processes[j], processes[i]
    
    # Calculate the waiting and turnaround time
    for i in range(1, n):
        waiting_time[i] = burst_time[i-1] + waiting_time[i-1]
        turnaround_time[i] = burst_time[i] + waiting_time[i]
    
    # Calculate the average waiting and turnaround time
    avg_waiting_time = sum(waiting_time) / n
    avg_turnaround_time = sum(turnaround_time) / n
    
    # Print the results
    print("Process\tBurst Time\tWaiting Time\tTurnaround Time")
    for i in range(n):
        print(processes[i], "\t\t", burst_time[i], "\t\t", waiting_time[i], "\t\t", turnaround_time[i])
    print("Average Waiting Time:", avg_waiting_time)
    print("Average Turnaround Time:", avg_turnaround_time)

# Sample inputs
processes = ['P1', 'P2', 'P3', 'P4', 'P5']
burst_time = [3, 5, 1, 4, 2]

# Call the function
sjf_scheduling(processes, burst_time)
JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.