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

Scheduling Algorithms (Lab)

The document discusses the First Come First Serve (FCFS) and Shortest Job First (SJF) CPU scheduling algorithms. FCFS schedules processes in the order of arrival, without preemption. It describes calculating waiting time and turnaround time. An example shows the Gantt chart and calculations. SJF schedules the process with the shortest burst time next, preemptively if a new process has shorter burst time than the current. It provides the definitions of waiting time, turnaround time and completion time. An example calculates average waiting time using a Gantt chart. Pseudocode for algorithms to calculate waiting time, turnaround time and average times is also included.

Uploaded by

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

Scheduling Algorithms (Lab)

The document discusses the First Come First Serve (FCFS) and Shortest Job First (SJF) CPU scheduling algorithms. FCFS schedules processes in the order of arrival, without preemption. It describes calculating waiting time and turnaround time. An example shows the Gantt chart and calculations. SJF schedules the process with the shortest burst time next, preemptively if a new process has shorter burst time than the current. It provides the definitions of waiting time, turnaround time and completion time. An example calculates average waiting time using a Gantt chart. Pseudocode for algorithms to calculate waiting time, turnaround time and average times is also included.

Uploaded by

Ahmad bhatti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Scheduling Algorithms

FCFS:
We are given with the n number of processes i.e. P1, P2, P3,.......,Pn and their
corresponding burst times. The task is to find the average waiting time and average
turnaround time using FCFS CPU Scheduling algorithm.
What is Waiting Time and Turnaround Time?
 Turnaround Time is the time interval between the submission of a process and its
completion.
Turnaround Time = completion of a process – submission of a process
 Waiting Time is the difference between turnaround time and burst time
Waiting Time = turnaround time – burst time
What is FCFS Scheduling?
First Come, First Served (FCFS) also known as First In, First Out (FIFO) is the CPU
scheduling algorithm in which the CPU is allocated to the processes in the order they
are queued in the ready queue.
FCFS follows non-preemptive scheduling which mean once the CPU is allocated to a
process it does not leave the CPU until the process will not get terminated or may get
halted due to some I/O interrupt.

Example
Let’s say, there are four processes arriving in the sequence as P2, P3, P1 with their
corresponding execution time as shown in the table below. Also, taking their arrival time
to be 0.

Process Order of arrival Execution time in msec

P1 3 15

P2 1 3

P3 2 3

Gantt chart showing the waiting time of processes P1, P2 and P3 in the system
As shown above,
The waiting time of process P2 is 0
The waiting time of process P3 is 3
The waiting time of process P1 is 6
Average time = (0 + 3 + 6) / 3 = 3 msec.
As we have taken arrival time to be 0 therefore turn around time and completion time
will be same.
Example:

Input-:  processes = P1, P2, P3


        Burst time = 5, 8, 12
Output-:
Processes  Burst    Waiting    Turn around
1          5        0           5
2          8        5           13
3          12       13          25
Average Waiting time = 6.000000
Average turn around time = 14.333333

ALGORITHM:1.
Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process name and the burst time Step

4: Set the waiting of the first process as ‗0‘and its burst time as its turnaround time Step

5: for each process in the Ready Q calculate

a). Waiting time (n) = waiting time (n-1) + Burst time (n-1) b).

Turnaround time (n)= waiting time(n)+Burst time(n)

Step 6: Calculate

a) Average waiting time = Total waiting Time / Number of process

b) Average Turnaround time = Total Turnaround Time / Number of process


Step 7: Stop the process

Example No:1.

#include<stdio.h>
#include<conio.h>
main()
{
intbt[20], wt[20], tat[20], i, n;
floatwtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}

Algorithm: 2.
Start                                                              
Step 1-> In function intwaitingtime(intproc[], int n,
intburst_time[], intwait_time[])
   Set wait_time[0] = 0
   Loop For i = 1 and i< n and i++
      Set wait_time[i] = burst_time[i-1] + wait_time[i-1]
   End For
Step 2-> In function intturnaroundtime(intproc[], int n,
intburst_time[], intwait_time[], int tat[])
   Loop For  i = 0 and i< n and i++
      Set tat[i] = burst_time[i] + wait_time[i]
   End For
Step 3-> In function intavgtime(intproc[], int n, intburst_time[])
   Declare and initialize wait_time[n], tat[n], total_wt = 0,
total_tat = 0;
   Call waitingtime(proc, n, burst_time, wait_time)
   Call turnaroundtime(proc, n, burst_time, wait_time, tat)
   Loop For  i=0 and i<n and i++
      Set total_wt = total_wt + wait_time[i]
      Set total_tat = total_tat + tat[i]
      Print process number, burstime wait time and turnaround time
   End For
   Print "Average waiting time =i.e. total_wt / n
   Print "Average turn around time = i.e. total_tat / n
Step 4-> In intmain()
   Declare the input intproc[] = { 1, 2, 3}
   Declare and initialize n = sizeofproc / sizeofproc[0]
   Declare and initialize burst_time[] = {10, 5, 8}
   Call avgtime(proc, n, burst_time)
Stop

Example No: 2.

#include <stdio.h>

// Function to find the waiting time for all processes

intwaitingtime(intproc[], int n,

intburst_time[], intwait_time[]) {

// waiting time for first process is 0

wait_time[0] = 0;

// calculating waiting time

for (inti = 1; i< n ; i++ )

wait_time[i] = burst_time[i-1] + wait_time[i-1] ;

return 0;

}
// Function to calculate turn around time

intturnaroundtime( intproc[], int n,

intburst_time[], intwait_time[], int tat[]) {

// calculating turnaround time by adding

// burst_time[i] + wait_time[i]

inti;

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

tat[i] = burst_time[i] + wait_time[i];

return 0;

//Function to calculate average time

intavgtime( intproc[], int n, intburst_time[]) {

intwait_time[n], tat[n], total_wt = 0, total_tat = 0;

inti;

//Function to find waiting time of all processes

waitingtime(proc, n, burst_time, wait_time);

//Function to find turn around time for all processes

turnaroundtime(proc, n, burst_time, wait_time, tat);

//Display processes along with all details

printf("Processes Burst Waiting Turn around \n");

// Calculate total waiting time and total turn

// around time

for ( i=0; i<n; i++) {


total_wt = total_wt + wait_time[i];

total_tat = total_tat + tat[i];

printf(" %d\t %d\t\t %d \t%d\n", i+1, burst_time[i], wait_time[i], tat[i]);

printf("Average waiting time = %f\n", (float)total_wt / (float)n);

printf("Average turn around time = %f\n", (float)total_tat / (float)n);

return 0;

// main function

int main() {

//process id's

intproc[] = { 1, 2, 3};

int n = sizeofproc / sizeofproc[0];

//Burst time of all processes

intburst_time[] = {5, 8, 12};

avgtime(proc, n, burst_time);

return 0;
}
Output
Processes  Burst    Waiting    Turn around
1          5        0           5
2          8        5           13
3          12       13          25
Average Waiting time = 6.000000
Average turn around time = 14.333333

((Link for the site:https://www.tutorialspoint.com/c-program-for-fcfs-scheduling))


Shortest Job First:
Shortest job first scheduling is the job or process scheduling algorithm that follows the
non-preemptive scheduling discipline. In this, scheduler selects the process from the
waiting queue with the least completion time and allocate the CPU to that job or
process. Shortest Job First is more desirable than FIFO algorithm because SJF is more
optimal as it reduces average wait time which will increase the throughput.
SJF algorithm can be preemptive as well as non-preemptive. Preemptive scheduling is
also known as shortest-remaining-time-first scheduling. In Preemptive approach, the
new process arises when there is already executing process. If the burst of newly
arriving process is lesser than the burst time of executing process than scheduler will
preempt the execution of the process with lesser burst time.

What is the Turnaround time, waiting time and completion time?

 Completion Time is the time required by the process to complete its execution
 Turnaround Time is the time interval between the submission of a process and
its completion.
Turnaround Time = completion of a process – submission of a process
 Waiting Time is the difference between turnaround time and burst time
Waiting Time = turnaround time – burst time

Example
We are given with the processes P1, P2, P3, P4 and P5 having their corresponding
burst time given below

Process Burst Time Arrival Time

P1 4 0

P2 2 1
Process Burst Time Arrival Time

P3 8 2

P4 1 3

P5 9 4

Since the arrival time of P1 is 0 it will be the first one to get executed till the arrival of
another process. When at 1 the process P2 enters and the burst time of P2 is less than
the burst time of P1 therefore scheduler will dispatch the CPU with the process P2 and
so on.

Average waiting time is calculated on the basis of gantt chart. P1 have to wait for
(0+4)4, P2 have to wait for 1, P3 have to wait for 7, P4 have to wait for 3 and P5 have to
wait for 15. So, their average waiting time will be −

Algorithm:1.
Start
Step 1-> Declare a struct Process
   Declare pid, bt, art
Step 2-> In function findTurnAroundTime(Process proc[], int n,
intwt[], int tat[])
   Loop For i = 0 and i< n and i++
      Set tat[i] = proc[i].bt + wt[i]
Step 3-> In function findWaitingTime(Process proc[], int n,
intwt[])
   Declare rt[n]
   Loop For i = 0 and i< n and i++
      Set rt[i] = proc[i].bt
      Set complete = 0, t = 0, minm = INT_MAX
      Set shortest = 0, finish_time
      Set bool check = false
      Loop While (complete != n)
         Loop For j = 0 and j < n and j++
            If (proc[j].art <= t) && (rt[j] <minm) &&rt[j] > 0
then,
               Set minm = rt[j]
               Set shortest = j
               Set check = true
            If check == false then,
               Increment t by 1
               Continue
               Decrement the value of rt[shortest] by 1
               Set minm = rt[shortest]
            If minm == 0 then,
               Set minm = INT_MAX
               If rt[shortest] == 0 then,
               Increment complete by 1
               Set check = false
               Set finish_time = t + 1
               Set wt[shortest] = finish_time - proc[shortest].bt
-proc[shortest].art
            If wt[shortest] < 0
               Set wt[shortest] = 0
               Increment t by 1
Step 4-> In function findavgTime(Process proc[], int n)
   Declare and set wt[n], tat[n], total_wt = 0, total_tat = 0
   Call findWaitingTime(proc, n, wt)
   Call findTurnAroundTime(proc, n, wt, tat)
   Loop For i = 0 and i< n and i++
      Set total_wt = total_wt + wt[i]
      Set total_tat = total_tat + tat[i]
      Print proc[i].pid, proc[i].bt, wt[i], tat[i]
      Print Average waiting time i.e., total_wt / n
      Print Average turn around time i.e., total_tat / n
Step 5-> In function intmain()
   Declare and set Process proc[] = { { 1, 5, 1 }, { 2, 3, 1 },
{ 3, 6, 2 }, { 4, 5, 3 } }
   Set n = sizeof(proc) / sizeof(proc[0])
   Call findavgTime(proc, n)
Stop
Example No:1.
#include<bits/stdc++.h>

usingnamespacestd;

//structure for every process

structProcess{
   intpid;// Process ID

   intbt;// Burst Time

   int art;// Arrival Time

};

voidfindTurnAroundTime(Processproc[],int n,intwt[],int tat[]){

   for(inti=0;i< n;i++)

   tat[i]=proc[i].bt+wt[i];

//waiting time of all process

voidfindWaitingTime(Processproc[],int n,intwt[]){

   intrt[n];

   for(inti=0;i< n;i++)

   rt[i]=proc[i].bt;

   int complete =0, t =0,minm= INT_MAX;

   int shortest =0,finish_time;

   bool check =false;

   while(complete != n){

      for(int j =0; j < n; j++){

         if((proc[j].art <= t)&&(rt[j]<minm)&&rt[j]>0){

            minm=rt[j];

            shortest= j;

            check=true;

         }

      }

      if(check ==false){

         t++;
         continue;

      }

      // decrementing the remaining time

      rt[shortest]--;

      minm=rt[shortest];

      if(minm==0)

         minm= INT_MAX;

         // If a process gets completely

         // executed

         if(rt[shortest]==0){

            complete++;

            check=false;

            finish_time= t +1;

            // Calculate waiting time

            wt[shortest]=finish_time-

            proc[shortest].bt-

            proc[shortest].art;

            if(wt[shortest]<0)

               wt[shortest]=0;

         }

         // Increment time

         t++;

   }

// Function to calculate average time

voidfindavgTime(Processproc[],int n){
   intwt[n], tat[n],total_wt=0,

   total_tat=0;

   // Function to find waiting time of all

   // processes

   findWaitingTime(proc, n,wt);

   // Function to find turn around time for

   // all processes

   findTurnAroundTime(proc, n,wt, tat);

   cout<<"Processes "<<" Burst time "<<" Waiting time "<<" Turn


around time\n";

   for(inti=0;i< n;i++){

      total_wt=total_wt+wt[i];

      total_tat=total_tat+ tat[i];

      cout<<" "<<proc[i].pid<<"\t\t"<<proc[i].bt<<"\t\t
"<<wt[i]<<"\t\t "<< tat[i]<<endl;

   }

   cout<<"\nAverage waiting time = "<<(float)total_wt/


(float)n;cout<<"\nAverageturn around time = "<<(float)total_tat/
(float)n;

// main function

int main(){

   Processproc[]={{1,5,1},{2,3,1},{3,6,2},{4,5,3}};

   int n =sizeof(proc)/sizeof(proc[0]);

   findavgTime(proc, n);

   return0;

Output
ALGORITHM No:2.
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU
burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to
Lowest to highest burst time.
Step 5: Set the waiting time of the first process as ‗0‘ and its turnaround time as its
burst time.
Step 6: Sort the processes names based on their Burt time
Step 7: For each process in the ready queue,
calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process
Step 9: Stop the process
Program No:2.
#include<stdio.h>
#include<conio.h>
main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float wtavg,
tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n); getch();}

You might also like