Program to simulate SJF CPU Scheduling

Share

A scheduling method known as “shortest job first” (SJF) or “shortest job next” chooses the waiting process with the shortest execution time to run first. Shortest Job Next (SJN), often known as SJN, can be either preemptive or non-preemptive.

C Program to simulate SJF CPU Scheduling

#include <stdio.h>
  
int main() 
{
      int arrival_time[10], burst_time[10], temp[10];
      int i, smallest, count = 0, time, limit;
      double wait_time = 0, turnaround_time = 0, end;
      float average_waiting_time, average_turnaround_time;
      printf("Enter the Total Number of Processes:");
      scanf("%d", &limit); 
      printf("Enter Details of %d Processes \n", limit);
      for(i = 0; i < limit; i++)
      {
            printf("\nEnter Arrival Time of p[%d]:",i);
            scanf("%d", &arrival_time[i]);
            printf("Enter Burst Time of p[%d]:",i);
            scanf("%d", &burst_time[i]); 
            temp[i] = burst_time[i];
      }
      burst_time[9] = 9999;  
	printf("Gantt Chart: ");
      for(time = 0; count != limit; time++)
      {
            smallest = 9;
            for(i = 0; i < limit; i++)
            {
                  if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)
                  {
                        smallest = i;
                  }
                  
            }
            printf("| P(%d) ",smallest);
            burst_time[smallest]--;
            if(burst_time[smallest] == 0)
            {
                  count++;
                  end = time + 1;
                  wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
                  turnaround_time = turnaround_time + end - arrival_time[smallest];
            }
      }
      average_waiting_time = wait_time / limit; 
      average_turnaround_time = turnaround_time / limit;
      printf("| \nAverage Waiting Time: %.3f\n", average_waiting_time);
      printf("Average Turnaround Time: %.3f\n", average_turnaround_time);
      return 0;
}

OUTPUT

Source code repository: https://github.com/CodewithSudeep/operating-system-c-program

Other C Programs: https://codewithsudeep.com/category/c-program/

Ai https://docs.google.com/document/d/15n3X7tYndwI9v7dCJ8Yv9hGydWBxpLMdv4_Cwsr6Cd4/edit?usp=drivesdk

All: https://docs.google.com/document/d/1DBANUK08Q8IRZfywk5CavBg2Y5U9N-RF6Jm52u6XLbo/edit?usp=sharing

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: