How to calculate WAITING TIME and TURN AROUND TIME in SJF-PRE-EMPTIVE scheduling using C program

 In this post, I will explain how to get the waiting time and turn around time in SJF-PRE-EMPTIVE scheduling in C language. To get the desired output you need to follow three steps I have given. After following the three steps you will get the output


Step-1

C Program:-

#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:\t");

      scanf("%d", &limit); 

      printf("\nEnter Details of %d Processes\n", limit);

      for(i = 0; i < limit; i++)

      {

            printf("\nEnter Arrival Time:\t");

            scanf("%d", &arrival_time[i]);

            printf("Enter Burst Time:\t");

            scanf("%d", &burst_time[i]); 

            temp[i] = burst_time[i];

      }

      burst_time[9] = 9999;  

      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;

                  }

            }

            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:\t%lf\n", average_waiting_time);

      printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);

      return 0;

}


Step-2

Copy the above code and paste in any ONLINE COMPILER and execute it. 

Link:- ONLINE COMPILER


Step-3

After clicking the RUN in the online compiler, it will ask "ENTER NUMBER OF PROCESSES".

After giving the number of processes it will ask ARRIVAL TIME and BURST TIME of each process.

After giving arrival and burst time of each process you will get the output.


PROOF



Comments

Popular posts from this blog

How to read a CSV file from Google Drive using Google Colab?