practical no-4& 5 & 6 (1)
practical no-4& 5 & 6 (1)
AIM: List of processes / jobs along with their arrival times & CPU burst times is given. Write a program to
print the total waiting time, average waiting time, total turnaround time, average turnaround time & Gantt
chart using Round Robin CPU scheduling policy.
CODE:
public class RoundRobin
{
static void findWaitingTime(int[] processes,
int n, int[] bt, int[] wt, int quantum)
{
int[] rem_bt = new int[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
int t = 0;
while (true)
{
bool done = true;
for (int i = 0; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
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 findavgTime(int[] processes, int n,
int[] bt, int quantum)
{
int[] wt = new int[n];
int[] tat = new int[n];
int total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
Console.WriteLine("Processes " + " Burst time " +
" Waiting time " + " Turn around time");
for (int i = 0; i < n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
Console.WriteLine(" " + (i + 1) + "\t\t" + bt[i]
+ "\t " + wt[i] + "\t\t " + tat[i]);
}
Console.WriteLine("Average waiting time = " +
(float)total_wt / (float)n);
Console.Write("Average turn around time = " +
(float)total_tat / (float)n);
}
public static void Main()
{
int[] processes = { 1, 2, 3 };
int n = processes.Length;
int[] burst_time = { 10, 5, 8 };
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
}
}
OUTPUT:
Practical No 6
AIM: List of processes / jobs along with their arrival times, priority value & CPU burst times is given. Write a
program to print the total waiting time, average waiting time, total turnaround time, average turnaround time &
Gantt chart using Priority CPU scheduling policy.
CODE:
class Program
{
static int totalprocess = 5;
static int[][] proc = new int[totalprocess][];
static int[] arrivaltime = new int[] { 1, 2, 3, 4, 5 };
static int[] bursttime = new int[] { 3, 5, 1, 7, 4 };
static int[] priority = new int[] { 3, 4, 1, 7, 8 };
service[0] = 0;
wt[0] = 0;
if (wt[i] < 0)
{
wt[i] = 0;
}
}
}
GetWtTime(wt);
GetTatTime(tat, wt);
int[] stime = new int[totalprocess];
int[] ctime = new int[totalprocess];
stime[0] = 1;
ctime[0] = stime[0] + tat[0];
Console.WriteLine("Process_no\tStart_time\tComplete_time\tTurn_Around_Time\tWaiting_Time");
for (int i = 0; i < totalprocess; i++)
{
wavg += wt[i];
tavg += tat[i];
Console.WriteLine(proc[i][3] + "\t\t" + stime[i] + "\t\t" + ctime[i] + "\t\t" + tat[i] + "\t\t\t" + wt[i]);
if (i != totalprocess - 1)
{
stime[i + 1] = ctime[i];
ctime[i + 1] = stime[i + 1] + tat[i + 1] - wt[i + 1];
}
}
Console.WriteLine("Average waiting time is: " + (double)wavg / totalprocess);
Console.WriteLine("Average turnaround time is: " + (double)tavg / totalprocess);
}
}
OUTPUT:
Practical No 4
Aim - Implementation of Shortest Job First (SJF) CPU Scheduling.
#include <stdio.h>
void main()
{
int bt[20], p[20], wt[20], tat[20], i, j, n, total = 0, pos, temp;
float avg_wt, avg_tat;
printf("Enter number of process:");
scanf("%d", &n);
printf("\nEnter Burst Time:\n");
for (i = 0; i < n; i++)
{
printf("p%d:", i + 1);
scanf("%d", &bt[i]);
p[i] = i + 1; // contains process number
}
// sorting burst time in ascending order using selection sort
for (i = 0; i < n; i++)
{
}
pos = i;
for (j = i + 1; j < n; j++)
{
if (bt[j] < bt[pos])
pos = j;
}
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
temp = p[i];
p[i] = p[pos];
p[pos] = temp;
wt[0] = 0; // waiting time for first process will be zero
// calculate waiting time
for (i = 1; i < n; i++)
{
wt[i] = 0;
for (j = 0; j < i; j++)
wt[i] += bt[j];
total += wt[i];
}
avg_wt = (float)total / n; // average waiting time
total = 0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for (i = 0; i < n; i++)
{
}
tat[i] = bt[i] + wt[i]; // calculate turnaround time
total += tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d", p[i], bt[i], wt[i], tat[i]);
avg_tat = (float)total / n; // average turnaround time
printf("\n\nAverage Waiting Time=%f", avg_wt);
printf("\nAverage Turnaround Time=%f\n", avg_tat);
}
Output :-