Scheduling algorithms play a crucial role in the efficient utilisation of a computer's resources, such as CPU time. One such scheduling algorithm that aims to minimise the turnaround time and waiting time of processes is Shortest Job First (SJF) scheduling algorithm. In this article, we will explore the concept of SJF scheduling, the Shortest Job First Scheduling Example, SJF scheduling Program In C Using Structure, and explore how to implement it in C programming.
Understanding SJF Scheduling is pivotal in enhancing the responsiveness and efficiency of computer systems. But before starting the preparation regarding swapping, consider learning these Online C Courses and Certifications Courses.
Shortest Job First scheduling, also known as Shortest Job Next (SJN) or Shortest Job First (SJF), is a non-preemptive SJF scheduling algorithm that selects the process with the smallest burst time or execution time to execute first. The key idea behind the SJF Scheduling algorithm is to minimise the average waiting time and turnaround time of processes, leading to a more efficient use of CPU resources.
The main challenge with SJF scheduling is that, in a real-world scenario, it is often impossible to predict the exact burst times of processes. This uncertainty makes the SJF Scheduling algorithm theoretical but less practical for general-purpose operating systems. But before diving straight into the blog, take a look at the various Algorithm Certification Courses offered in order to prepare yourself for the SJF Scheduling Algorithm.
Also read
Shortest Job First (SJF) scheduling, as discussed earlier, is a non-preemptive SJF Scheduling algorithm that selects the process with the shortest burst time to execute first. SJF Scheduling algorithm can be categorised into two main types:
Non-Preemptive SJF Scheduling: In non-preemptive SJF scheduling, once a process begins execution, it is not interrupted until it completes its execution. Only when the currently executing process finishes can another process be selected based on its burst time.
Example:
Let us consider a set of processes with their arrival times and burst times. The table below illustrates this:
Process | Arrival Time | Burst Time |
P1 | 0 | 6 |
P2 | 1 | 8 |
P3 | 2 | 7 |
P4 | 3 | 3 |
With non-preemptive SJF scheduling, the order of execution would be:
Execute P1 (burst time: 6)
Execute P4 (burst time: 3)
Execute P3 (burst time: 7)
Execute P2 (burst time: 8)
Preemptive SJF Scheduling: Preemptive SJF scheduling allows a running process to be interrupted if a shorter burst time process arrives. In this case, the shorter job is given priority, which may lead to the preemption of the currently executing process.
Example:
Consider the same set of processes as in the previous example. The order of execution with preemptive SJF scheduling would be as follows:
Execute P1 (burst time: 6)
Preempt P1, execute P4 (burst time: 3)
Preempt P4, execute P3 (burst time: 7)
Preempt P3, execute P2 (burst time: 8)
In preemptive SJF scheduling, the scheduler periodically checks if there is a process with a shorter burst time than the currently running process. If such a process exists, preemption occurs, and the shorter job is executed.
Let us write a C program for preemptive SJF scheduling, considering processes with arrival times and burst times. The program will calculate waiting times for each process and demonstrate the scheduling order. The following showcases an example of SJF preemptive scheduling program in C:
#include <stdio.h>
struct Process {
int processID;
int arrivalTime;
int burstTime;
int waitingTime;
};
void SJFPreemptive(struct Process processes[], int n) {
int currentTime = 0;
int remainingProcesses = n;
while (remainingProcesses > 0) {
int shortestJobIndex = -1;
int shortestBurst = INT_MAX;
for (int i = 0; i < n; i++) {
if (processes[i].arrivalTime <= currentTime && processes[i].burstTime < shortestBurst && processes[i].burstTime > 0) {
shortestJobIndex = i;
shortestBurst = processes[i].burstTime;
}
}
if (shortestJobIndex == -1) {
currentTime++;
} else {
printf("Executing process P%d\n", processes[shortestJobIndex].processID);
processes[shortestJobIndex].burstTime--;
currentTime++;
if (processes[shortestJobIndex].burstTime == 0) {
remainingProcesses--;
processes[shortestJobIndex].waitingTime = currentTime - processes[shortestJobIndex].arrivalTime;
}
}
}
}
int main() {
struct Process processes[] = {
{1, 0, 6, 0},
{2, 1, 8, 0},
{3, 2, 7, 0},
{4, 3, 3, 0}
};
int n = sizeof(processes) / sizeof(processes[0]);
SJFPreemptive(processes, n);
printf("Process Execution Order:\n");
for (int i = 0; i < n; i++) {
printf("P%d -> ", processes[i].processID);
}
printf("\n");
printf("Process Waiting Times:\n");
for (int i = 0; i < n; i++) {
printf("P%d: %d\n", processes[i].processID, processes[i].waitingTime);
}
return 0;
}
In this C program, the SJFPreemptive function implements preemptive SJF scheduling, taking into account arrival times. The program also prints the execution order and waiting times for each process.
Also read
Implementing Shortest Job First scheduling in C programming is a fundamental exploration into the world of process scheduling algorithms. SJF, renowned for its efficiency in minimising process waiting times, is a valuable concept in operating systems and computer science. Through the implementation of the shortest job first scheduling program in c, we can delve into the mechanisms of process management, arriving at a deeper understanding of how scheduling algorithms optimise resource utilisation and enhance system performance. Let us explore the implementation of the SJF scheduling program in C. We will start with the essential data structures and functions needed for this algorithm.
Also read:
In C, you can define a structure to represent a process. This structure typically includes information such as process ID, arrival time, burst time, and waiting time. Here is an example of a Process structure:
struct Process {
int processID;
int arrivalTime;
int burstTime;
int waitingTime;
};
To implement SJF scheduling, you need a function to sort the processes based on their burst times.In the code shown below we compare elements with adjacent elements to achieve the correct order of processes. You can use a sorting algorithm such as Bubble Sort or Quick Sort. Here is an example using Bubble Sort:
void sortProcesses(struct Process processes[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].burstTime > processes[j + 1].burstTime) {
// Swap the processes
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
}
Now, let us create the main SJF scheduling function that selects the shortest job and updates waiting times.
void SJFSchedule(struct Process processes[], int n) {
// Sort the processes by burst time
sortProcesses(processes, n);
int currentTime = 0;
for (int i = 0; i < n; i++) {
// Execute the current process
printf("Executing process P%d\n", processes[i].processID);
processes[i].waitingTime = currentTime - processes[i].arrivalTime;
// Update current time
currentTime += processes[i].burstTime;
}
}
Putting it All Together
In your main function, you can create an array of processes, initialise their attributes, and call the SJFSchedule function.
int main() {
struct Process processes[] = {
{1, 0, 6, 0},
{2, 1, 8, 0},
{3, 2, 7, 0},
{4, 3, 3, 0}
};
int n = sizeof(processes) / sizeof(processes[0]);
SJFSchedule(processes, n);
return 0;
}
This simple example demonstrates the shortest job first scheduling in c programming. However, in real-world operating systems, you would need to incorporate more sophisticated data structures and error handling.
Shortest Job First scheduling algorithm is a theoretical scheduling algorithm aimed at minimising process waiting times and turnaround times. While it may not be practical in real-world scenarios due to the unpredictability of process burst times, understanding its concepts and implementing it in C programming can provide valuable insights into scheduling algorithms and their efficiency.
From understanding shortest job first scheduling, shortest job first scheduling example and shortest job first scheduling program in c with arrival time makes the candidates understand the dynamics involved within SJF scheduling algorithm in order to make students proficient Computer programmers.
It is a process scheduling algorithm used in operating systems. It selects the process with the shortest burst time (execution time) to execute first. It's important because it aims to minimise process waiting times and turnaround times, leading to more efficient CPU resource utilisation.
SJF scheduling can be categorised into two main types: non-preemptive SJF, where a process runs to completion before the next is chosen, and preemptive SJF, where processes can be interrupted and replaced by shorter jobs.
In non-preemptive SJF scheduling, the scheduler selects the process with the shortest burst time to run next. Once a process starts, it runs to completion without interruption.
In the article, a C program for preemptive SJF scheduling with arrival times is demonstrated in the execution order and waiting times of processes.
While SJF scheduling is a theoretical concept, it highlights the importance of optimising process scheduling for efficient resource utilisation. Real-world operating systems often use variations of scheduling algorithms inspired by SJF to enhance system performance.
Application Date:05 September,2024 - 25 November,2024
Application Date:15 October,2024 - 15 January,2025
Application Date:10 November,2024 - 08 April,2025