0% found this document useful (0 votes)
44 views

Assignment 1

The document discusses the First Come First Serve (FCFS) and Shortest Job First (SJF) CPU scheduling algorithms. It provides code samples for implementing FCFS and preemptive SJF in C. For FCFS, it defines terms like completion time, turnaround time, and waiting time. It notes FCFS is non-preemptive and has non-optimal average waiting times. For SJF, it describes preemptive and non-preemptive variants and notes SJF schedules the shortest job first to minimize average waiting time.

Uploaded by

Kshitij Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Assignment 1

The document discusses the First Come First Serve (FCFS) and Shortest Job First (SJF) CPU scheduling algorithms. It provides code samples for implementing FCFS and preemptive SJF in C. For FCFS, it defines terms like completion time, turnaround time, and waiting time. It notes FCFS is non-preemptive and has non-optimal average waiting times. For SJF, it describes preemptive and non-preemptive variants and notes SJF schedules the shortest job first to minimize average waiting time.

Uploaded by

Kshitij Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 1

By Kshitij Sharma – 102103374

1)Aim: To write a C program to implement the CPU


scheduling algorithm for FIRST COME FIRST SERVE.

Solution:-

First Come First Serve is a scheduling algorithm used by the CPU to


schedule jobs. It is a Non-Preemptive Algorithm. Priority is given
according to which they are requested by the processor.

TERMS In First Come First Serve

 Completion time: Time when the execution is completed.

 Turn Around Time: The sum of the burst time and waiting time
gives the turn-around time

 Waiting time: The time the processes need to wait for it to get
the CPU and start execution is called waiting time.

Important Points

 It is non-preemptive

 Average waiting time is not optimal

 Cannot utilize resources in parallel

CODE:-
 #include<stdio.h>
  
  int main()
  
 {
     int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
     printf("Enter total number of processes(maximum 20):");
     scanf("%d",&n);
  
     printf("nEnter Process Burst Timen");
     for(i=0;i<n;i++)
     {
         printf("P[%d]:",i+1);
         scanf("%d",&bt[i]);
     }
  
     wt[0]=0;   
  
     for(i=1;i<n;i++)
     {
         wt[i]=0;
         for(j=0;j<i;j++)
             wt[i]+=bt[j];
     }
  
     printf("nProcessttBurst TimetWaiting TimetTurnaround Time");
  
     for(i=0;i<n;i++)
     {
         tat[i]=bt[i]+wt[i];
         avwt+=wt[i];
         avtat+=tat[i];
         printf("nP[%d]tt%dtt%dtt%d",i+1,bt[i],wt[i],tat[i]);
     }
     avwt/=i;
     avtat/=i;
     printf("nnAverage Waiting Time:%d",avwt);
     printf("nAverage Turnaround Time:%d",avtat);
  
     return 0;
 }

2)Aim: To write a C program to implement the CPU
scheduling algorithm for shortest job first.

Solution:-

 Shortest job first(SJF) is a scheduling algorithm, that is used to


schedule processes in an operating system. It is a very important
topic in Scheduling when compared to round-robin and FCFS
Scheduling.

There are two types of SJF

 Pre-emptive SJF

 Non-Preemptive SJF

These algorithms schedule processes in the order in which the shortest


job is done first. It has a minimum average waiting time.

Code for Pre-emptive SJF 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("nEnter the Total Number of Processes:t");
      scanf("%d", &limit);
      printf("nEnter Details of %d Processesn", 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("nnAverage Waiting Time:t%lfn", average_waiting_time);
      printf("Average Turnaround Time:t%lfn", average_turnaround_time);
      return 0;
}

You might also like