Javatpoint Logo
Javatpoint Logo

SJF Scheduling program in C

SJF (Shortest Job First) is a scheduling strategy that gives the process with the quickest CPU burst time to the CPU first. As this technique is non-preemptive, once a process has begun to run, it cannot be stopped until it has finished. The SJF scheduling method is ideal since it reduces the average waiting time for a set of processes.

In this blog post, we will learn how to implement the SJF scheduling algorithm in C programming language. We will start by discussing the algorithm and its working, followed by the implementation details in C. We will also provide a sample code with syntax, example, and output to help you understand the concept better.

Algorithm:

The SJF scheduling algorithm works as follows:

  1. Find the process with the shortest CPU burst time.
  2. Execute the process until completion.
  3. Repeat steps 1 and 2 until all processes are executed.

In other words, the algorithm selects the process with the smallest burst time, executes it, and then selects the next shortest burst time process and so on. This algorithm is non-preemptive because once a process starts executing, it will continue until completion.

Implementation:

Now, let's see how to implement the SJF scheduling algorithm in C programming language. We will create a simple program that takes input as the number of processes, arrival time, and burst time of each process, and then calculates the average waiting time and turnaround time of each process using the SJF scheduling algorithm.

Step 1: Create a structure for a process that will store its arrival time, burst time, and waiting time.

Step 2: Create a function to compare two processes based on their burst time. This function will be used by the qsort() function to sort the processes based on their burst time.

Step 3: Create a main function that will take input from the user and execute the SJF scheduling algorithm.

Let's go through the above code step by step:

  1. We first take input from the user for the number of processes, arrival time, and burst time of each process.
  2. We create an array of structures to store the information about each process.
  3. We use the qsort() function to sort the processes based on their burst time.
  4. We calculate the waiting time of each process using the SJF scheduling algorithm.
  5. We calculate the average waiting time and average turnaround time of all processes.
  6. Finally, we print the results in tabular form.

Example:

Let's consider an example of four processes with their arrival time and burst time as follows:

Code:

Output:

 Enter the number of processes: 4 
 Enter arrival time and burst time of process 1: 0 8 
 Enter arrival time and burst time of process 2: 1 4 
 Enter arrival time and burst time of process 3: 2 9 
 Enter arrival time and burst time of process 4: 3 5 
 Process Arrival Time Burst Time Waiting Time Turnaround Time 
 1 0 8 0 8 
 2 1 4 8 12 
 4 3 5 12 17 
 3 2 9 17 26 
 Average Waiting Time: 9.250000 
 Average Turnaround Time: 15.750000 

SJF scheduling is an important algorithm used in operating systems to improve system performance by minimizing the average waiting time and turnaround time of processes. It is a non-preemptive scheduling algorithm, meaning that once a process starts execution, it will not be interrupted until it completes its execution. The SJF scheduling algorithm selects the process with the shortest burst time for execution.

One of the main advantages of the SJF scheduling algorithm is that it leads to the minimum average waiting time and turnaround time for all the processes. However, it has a major disadvantage of being unpredictable, especially in cases where the burst time of processes is not known in advance. It is because, the SJF scheduling algorithm requires knowledge of the burst time of all processes before it can decide which process to execute next.

The implementation of the SJF scheduling algorithm in C involves creating an array of structures to store the information about each process, including its arrival time, burst time, and waiting time. After that, the processes are sorted based on their burst time using the qsort() function. Once the processes are sorted, the waiting time of each process is calculated using the SJF scheduling algorithm. Finally, the average waiting time and average turnaround time of all processes are calculated and printed in tabular form.

The implementation of the SJF scheduling algorithm in C can be further improved by adding error handling to handle input validation and edge cases. For example, if the arrival time of a process is greater than the current time, it means that the process has not arrived yet, and it should be skipped until it arrives. Similarly, if two or more processes have the same burst time, the SJF scheduling algorithm should select the process with the earliest arrival time for execution.

Another way to improve the implementation of the SJF scheduling algorithm in C is to make it preemptive. In a preemptive SJF scheduling algorithm, a process with a shorter burst time can interrupt a process with a longer burst time if it arrives while the longer process is already executing. Preemptive SJF scheduling can lead to even lower average waiting time and turnaround time, especially when processes with shorter burst times arrive frequently.

Conclusion:

In conclusion, SJF scheduling is a powerful algorithm for optimizing the performance of operating systems and computer programs. Its implementation in C involves creating an array of structures to store process information, sorting processes based on their burst time, and calculating waiting and turnaround time. Although non-preemptive SJF scheduling has its limitations, it remains a widely used scheduling algorithm due to its ability to minimize average waiting and turnaround time. Improvements to SJF scheduling can further enhance its effectiveness, such as error handling and making the algorithm preemptive. Understanding and implementing the SJF scheduling algorithm in C is a valuable skill for any programmer or computer scientist looking to write efficient and optimized programs.


Next TopicStdlib.h in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA