Lab # 6
Process Scheduling (i) First Come First Serve (FCFS) &
(ii) Shortest Job First (SJF)
Write shell scripting code for FCFS scheduling and also calculate average waiting time.
A. First Come First Serve Scheduling Algorithm (FCFS) :
using System;
using [Link];
using [Link];
using [Link];
using [Link];
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;
[Link](
"Enter total number of processes(maximum 20): ");
n = Convert.ToInt32([Link]());
[Link]("\nEnter Process Burst Time");
for (int i = 0; i < n; i++) {
[Link]("P[" + (i + 1) + "]: ");
burst_time[i]
= Convert.ToInt32([Link]());
}
waiting_time[0]
= 0; // Waiting time for first process is 0
// Calculating waiting time
for (int i = 1; i < n; i++) {
waiting_time[i] = 0;
for (int j = 0; j < i; j++) {
waiting_time[i] +=
burst_time[j];
}
}
// Calculating turnaround time by adding burst_time
// and waiting_time
for (int i = 0; i < n; i++) {
turnaround_time[i]
= burst_time[i] + waiting_time[i];
avg_turnaround_time += turnaround_time[i];
}
avg_turnaround_time /= n; [Link]("\
nAverage Turnaround Time: "
+ avg_turnaround_time + "ms\n");
// Calculating average waiting time
for (int i = 0; i < n; i++) {
avg_waiting_time += waiting_time[i];
}
avg_waiting_time /= n; [Link]("\
nAverage Waiting Time: "
+ avg_waiting_time + "ms\n\n");
[Link](
"Process\tBurst Time\tWaiting Time\tTurnaround Time");
for (int i = 0; i < n; i++) {
[Link]("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 [Link];
public class Process
{
public int Id { get; set; }
public int ArrivalTime { get; set; }
public int BurstTime { get; set; }
public int WaitingTime { get; set; }
public int TurnaroundTime { get; set; }
public Process(int id, int arrivalTime, int burstTime)
{
Id = id;
ArrivalTime = arrivalTime;
BurstTime = burstTime;
WaitingTime = 0;
TurnaroundTime = 0;
}
}
public class ShortestJobFirst
{
public List<Process> Processes { get; set; }
public ShortestJobFirst(List<Process> processes)
{
Processes = processes;
}
public void Schedule()
{
// Sort processes based on arrival time and burst time
[Link]((a, b) => {
int compareArrival = [Link]([Link]);
if (compareArrival == 0)
{
return [Link]([Link]);
}
return compareArrival;
});
int currentTime = 0;
int completedProcesses = 0;
int n = [Link];
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 ([Link] <= currentTime && [Link] > 0)
{
if (shortestProcess == null || [Link] <
[Link])
{
shortestProcess = proc;
}
}
}
if (shortestProcess != null)
{
// Process the shortest process found
currentTime += [Link];
[Link] = currentTime -
[Link];
[Link] = [Link] -
[Link];
// Mark the process as completed
[Link] = 0;
completedProcesses++;
}
else
{
// If no process found to execute, just increment the time
currentTime++;
}
}
}
public void DisplayResults()
{
[Link]("Process\tArrival\tBurst\tWaiting\tTurnaround");
foreach (var proc in Processes)
{
[Link]($"{[Link]}\t{[Link]}\t{[Link]}\t{[Link]
tingTime}\t{[Link]}");
}
}
}
public class Program
{
public static void Main()
{
// Define the list of processes
List<Process> processes = new List<Process>
{
new Process(1, 0, 6),
new Process(2, 1, 8),
new Process(3, 2, 7),
new Process(4, 3, 3)
};
// Create a SJF scheduler
ShortestJobFirst sjf = new ShortestJobFirst(processes);
// Schedule the processes
[Link]();
// Display the results
[Link]();
}
}
Output
Lab Tasks
1. For below processes, write FCFS and SJF processes to calculate waiting time for
each process and average waiting time.
Process Arrival Time Execute Time
P0 0 5
P1 1 3
P2 2 8
P3 3 6
Solution: For FCFS
Process Service Time - Arrival Time Wait Time
P0 0-0 0
P1 5-1 4
P2 8-2 6
P3 16-3 13
Average Wait Time = (0+4+6+13)/4 = 5.75
For SJF
Process Service Time - Arrival Time Wait Time
P0 0-0 0
P1 5-1 4
P2 14-2 12
P3 8-3 5
Average Wait Time = (0+4+12+5)/4=5.25
2. For below processes, write FCFS and SJF processes to calculate waiting time for
each process and average waiting time.
Process Arrival Time Execute Time
P0 2 6
P1 5 2
P2 1 8
P3 0 3
P4 4 4
Solution: For FCFS
Process Service Time - Arrival Time Wait Time
P0 11-2 9
P1 21-5 16
P2 3-1 2
P3 0-0 0
P4 17-4 13
Average Wait Time = (9+16+2+0+13)/5 = 8
For SJF
Process Service Time - Arrival Time Wait Time
P0 0-0 0
P1 3-2 1
P2 9-5 4
P3 11-4 7
P4 15-1 14
Average Wait Time = (0+1+4+7+14)/5=5.2