Lab 6
Lab 6
class Program {
static void Main(string[] args)
{
int n;
int[] burst_time = new int[20];
int[] waiting_time = new int[20];
int[] turnaround_time = new int[20];
float avg_waiting_time = 0;
float avg_turnaround_time = 0;
Console.Write(
"Enter total number of processes(maximum 20): ");
n = Convert.ToInt32(Console.ReadLine());
waiting_time[0]
= 0; // Waiting time for first process is 0
avg_turnaround_time /= n;
Console.WriteLine("\nAverage Turnaround Time: "
+ avg_turnaround_time + "ms\n");
avg_waiting_time /= n;
Console.WriteLine("\nAverage Waiting Time: "
+ avg_waiting_time + "ms\n\n");
Console.WriteLine(
"Process\tBurst Time\tWaiting Time\tTurnaround Time");
for (int i = 0; i < n; i++) {
Console.WriteLine("P[" + (i + 1) + "]\t"
+ burst_time[i] + "\t\t"
+ waiting_time[i] + "\t\t"
+ turnaround_time[i]);
}}}
B. Shortest Job First Scheduling Algorithm (SJF)
using System;
using System.Collections.Generic;
int currentTime = 0;
int completedProcesses = 0;
int n = Processes.Count;
while (completedProcesses < n)
{
// Find the next shortest job that has arrived
Process shortestProcess = null;
for (int i = 0; i < n; i++)
{
Process proc = Processes[i];
if (proc.ArrivalTime <= currentTime && proc.BurstTime > 0)
{
if (shortestProcess == null || proc.BurstTime <
shortestProcess.BurstTime)
{
shortestProcess = proc;
}
}
}
if (shortestProcess != null)
{
// Process the shortest process found
currentTime += shortestProcess.BurstTime;
shortestProcess.TurnaroundTime = currentTime -
shortestProcess.ArrivalTime;
shortestProcess.WaitingTime = shortestProcess.TurnaroundTime -
shortestProcess.BurstTime;
Console.WriteLine($"{proc.Id}\t{proc.ArrivalTime}\t{proc.BurstTime}\t{proc.Wai
tingTime}\t{proc.TurnaroundTime}");
}
}
}
Output
Lab Tasks
1. For below processes, write FCFS and SJF processes to calculate waiting time for each
process and average waiting time.
2. For below processes, write FCFS and SJF processes to calculate waiting time for each
process and average waiting time.