Professional Documents
Culture Documents
| Lab 5
Scheduling Algorithms
we have assumed arrival times as 0, so turn around and completion times are same.
Implementation in C#:
using System;
class FCFS {
static void findWaitingTime(int []processes,int n,int []bt,int []wt) {
wt[0]=0;
for (int i=1;i<n;i++) {
wt[i] = wt[i - 1] + bt[i - 1];
}
}
Page | 1
4thLevel | Operating System. | Lab 5
static void findTurnAroundTime(int[] processes, int n, int[] bt, int[] wt,int []tat) {
for (int i=0;i<n;i++) {
tat[i] = bt[i] + wt[i];
}
}
static void AvgTime(int [] processes,int n,int[]bt) {
int[] wt = new int[n];
int [] tat = new int[n];
int total_wt=0, total_tat=0;
//Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt);
//Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
//Display processes along with all details
Console.Write("Processes Burst time Waiting"
+ " time Turn around time\n");
for (int i=0;i<n;i++) {
total_wt += wt[i];
total_tat+=tat[i];
Console.Write(" {0} ", (i + 1));
Console.Write(" {0} ", bt[i]);
Console.Write(" {0}", wt[i]);
Console.Write(" {0}\n", tat[i]);
}
double avgWaitTime = (double)total_wt / n;
double avgTime = (double)total_tat / n;
Console.WriteLine("AvgWaitTime= {0}", avgWaitTime);
Console.WriteLine("AvgTurnAroundTime= {0}",avgTime);
}
public static void Main(String[] args) {
int[] processes = {1,2,3 };
int n=processes.Length;
int[] bt = { 24,3,4};
AvgTime(processes,n,bt);
}
}
Output:
Page | 2
4thLevel | Operating System. | Lab 5
Round Robin:
Round Robin is a CPU scheduling algorithm where each process is assigned a fixed time slot
in a cyclic way.
• It is simple, easy to implement, and starvation-free as all processes get fair share of
CPU.
• One of the most commonly used technique in CPU scheduling as a core.
• It is preemptive as processes are assigned CPU only for a fixed slice of time at
most.
• The disadvantage of it is more overhead of context switching.
Implementation in C#:
// C# program for implementation of RR
// scheduling
using System;
Page | 3
4thLevel | Operating System. | Lab 5
Page | 4
4thLevel | Operating System. | Lab 5
// Driver Method
public static void Main()
{// process id's
int[] processes = { 1,2,3};
int n = processes.Length;
// Burst time of all processes
int[] burst_time = { 3,4,3 };
// Time quantum
int quantum = 4;
findavgTime(processes, n, burst_time, quantum);
}
}
Output:
Page | 5