Skip to content Skip to sidebar Skip to footer

Sjf Preemptive Scheduling Program In C With Gantt Chart

SJF or Shortest Job First is a scheduling algorithm that selects the process with the shortest burst time. Preemptive SJF is a variant of this algorithm that allows the running process to be interrupted if a new process with a shorter burst time arrives. In this article, we will discuss how to implement a preemptive SJF scheduling program in C and visualize its output using a Gantt Chart.

Understanding SJF Preemptive Scheduling

The SJF preemptive scheduling algorithm is based on the concept that the process with the shortest burst time should be executed first. This algorithm is useful in a situation where there are many processes with different burst times, and the priority is to minimize the average waiting time. In preemptive SJF, the running process is interrupted if a new process with a shorter burst time arrives, and the new process is executed first. This ensures that the process with the shortest burst time is always given priority.

Understanding Sjf Preemptive Scheduling

Implementing SJF Preemptive Scheduling in C

To implement the SJF preemptive scheduling algorithm in C, we need to follow these steps:

  1. Define a structure to hold the process details (process ID, burst time, arrival time, completion time, and waiting time).
  2. Take input from the user for the number of processes and their details (burst time and arrival time).
  3. Sort the processes based on their arrival time.
  4. Loop through the processes and select the process with the shortest burst time that has arrived.
  5. If a new process with a shorter burst time arrives, interrupt the running process and execute the new process first.
  6. Calculate the completion time and waiting time for each process.
  7. Calculate the average waiting time and display the process details and Gantt Chart.

Coding the SJF Preemptive Scheduling Program in C

Here is the code for implementing the SJF preemptive scheduling algorithm in C:

// Define the process structurestruct process {int pid;int burst_time;int arrival_time;int completion_time;int waiting_time;};int main() {// Take input for the number of processesprintf("Enter the number of processes: ");scanf("%d", &n);// Take input for the process detailsfor (i = 0; i < n; i++) {printf("Enter the burst time for process %d: ", i);scanf("%d", &p[i].burst_time);printf("Enter the arrival time for process %d: ", i);scanf("%d", &p[i].arrival_time);p[i].pid = i + 1;}// Sort the processes based on arrival timeqsort(p, n, sizeof(struct process), compare_arrival_time);// Loop through the processes and execute themwhile (finished < n) {int shortest_index = -1;int shortest_burst_time = INT_MAX;for (i = 0; i < n; i++) {if (p[i].burst_time > 0 && p[i].arrival_time <= current_time) {if (p[i].burst_time < shortest_burst_time) {shortest_index = i;shortest_burst_time = p[i].burst_time;}}}if (shortest_index == -1) {current_time++;} else {p[shortest_index].burst_time--;current_time++;if (p[shortest_index].burst_time == 0) {p[shortest_index].completion_time = current_time;p[shortest_index].waiting_time = p[shortest_index].completion_time - p[shortest_index].burst_time - p[shortest_index].arrival_time;finished++;}}}// Calculate the average waiting timefor (i = 0; i < n; i++) {total_waiting_time += p[i].waiting_time;}average_waiting_time = (float)total_waiting_time / (float)n;// Display the process details and Gantt Chartdisplay_process_details(p, n);display_gantt_chart(p, n);return 0;}

Visualizing the Output with a Gantt Chart

A Gantt Chart is a type of bar chart that illustrates a project schedule. It is widely used in project management to visualize the start and finish dates of different tasks. In the context of preemptive SJF scheduling, a Gantt Chart can be used to visualize the timeline of execution of different processes.

Visualizing The Output With A Gantt Chart

Conclusion

In this article, we discussed how to implement the SJF preemptive scheduling algorithm in C and visualize its output using a Gantt Chart. SJF preemptive scheduling is a useful algorithm in situations where the priority is to minimize the average waiting time. By following the steps outlined in this article, you can create a program that implements the SJF preemptive scheduling algorithm and visualizes its output using a Gantt Chart.

Related video of SJF Preemptive Scheduling Program in C with Gantt Chart