In: Computer Science
Process | Burst Time |
P1 | 6ms |
P2 | 2ms |
P3 | 7ms |
P4 | 3ms |
P5 | 8ms |
P6 | 10ms |
Using the table above calculate the average wait time using First
Come First Served (FCFS) and Shortest Job First (SJF) CPU
scheduling.
1. Calculate average time using FCFS First come First served
Consider the processes P1, P2, P3, P4, P5, P6 given table and
given Burst Time, arrives for execution in the
same order, with Arrival Time 0
,
let's find the average waiting time using the FCFS scheduling
algorithm.
Average time is = (0 + 6 +8 + 15 +18 +26 )/6
= 73/6
=12.1666667 ms
Diagram:
ALGORITHM:
1- Input the processes along with their burst time (bt). 2- Find waiting time (wt) for all processes. 3- As first process that comes need not to wait so waiting time for process 1 will be 0 i.e. wt[0] = 0. 4- Find waiting time for all other processes i.e. for process i -> wt[i] = bt[i-1] + wt[i-1] . 5- Find turnaround time = waiting_time + burst_time for all processes. 6- Find average waiting time = total_waiting_time / no_of_processes. 7- Similarly, find average turnaround time = total_turn_around_time / no_of_processes.
Output of the algorithm will come like this :
2. Calculate average time using SJF Shortest Job First
SJF is of 2 types :
Let us consider the arrival time for the following processes are:
Process | Burst Time | Arrival time |
p1 | 6 | 2 |
p2 | 2 | 0 |
p3 | 7 | 3 |
p4 | 3 | 1 |
p5 | 8 | 4 |
p6 | 10 | 5 |
Average time = (0+1+3+8+14+21)/6
= 47/6
= 7.833335 ms
Diagram:
Java Code to calulate the average time is for your reference:
import java.util.*;
public class demo{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println ("enter no of process:");
int n = sc.nextInt();
int pid[] = new int[n];
int at[] = new int[n];
int bt[] = new int[n];
int ct[] = new int[n];
int ta[] = new int[n];
int wt[] = new int[n];
int f[] = new int[n];
int st=0, tot=0;
float avgwt=0, avgta=0;
for(int i=0;i<n;i++)
{
System.out.println ("enter process " + (i+1) + " arrival time:");
at[i] = sc.nextInt();
System.out.println ("enter process " + (i+1) + " brust time:");
bt[i] = sc.nextInt();
pid[i] = i+1;
f[i] = 0;
}
boolean a = true;
while(true)
{
int c=n, min=999;
if (tot == n)
break;
for (int i=0; i<n; i++)
{
if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))
{
min=bt[i];
c=i;
}
}
if (c==n)
st++;
else
{
ct[c]=st+bt[c];
st+=bt[c];
ta[c]=ct[c]-at[c];
wt[c]=ta[c]-bt[c];
f[c]=1;
tot++;
}
}
System.out.println("\npid arrival brust complete turn waiting");
for(int i=0;i<n;i++)
{
avgwt+= wt[i];
avgta+= ta[i];
System.out.println(pid[i]+"\t"+at[i]+"\t"+bt[i]+"\t"+ct[i]+"\t"+ta[i]+"\t"+wt[i]);
}
System.out.println ("\naverage tat is "+ (float)(avgta/n));
System.out.println ("average wt is "+ (float)(avgwt/n));
sc.close();
}
}
Output: