0% found this document useful (0 votes)
2 views

practical no-4& 5 & 6 (1)

The document contains three programming examples for CPU scheduling algorithms: Round Robin, Priority, and Shortest Job First (SJF). Each example includes code that calculates total and average waiting times, turnaround times, and outputs a Gantt chart. The programs are implemented in C# and C, demonstrating different scheduling policies with sample input data.

Uploaded by

arpiarpi1663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

practical no-4& 5 & 6 (1)

The document contains three programming examples for CPU scheduling algorithms: Round Robin, Priority, and Shortest Job First (SJF). Each example includes code that calculates total and average waiting times, turnaround times, and outputs a Gantt chart. The programs are implemented in C# and C, demonstrating different scheduling policies with sample input data.

Uploaded by

arpiarpi1663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No 5

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 };

static void Main(string[] args)


{
for (int i = 0; i < totalprocess; i++)
{
proc[i] = new int[4];
proc[i][0] = arrivaltime[i];
proc[i][1] = bursttime[i];
proc[i][2] = priority[i];
proc[i][3] = i + 1;
}

Array.Sort(proc, (x, y) => x[2].CompareTo(y[2]));


Array.Sort(proc, (x, y) => x[0].CompareTo(y[0]));
Findgc();
}

static void GetWtTime(int[] wt)


{
int[] service = new int[totalprocess];

service[0] = 0;
wt[0] = 0;

for (int i = 1; i < totalprocess; i++)


{
service[i] = proc[i - 1][1] + service[i - 1];
wt[i] = service[i] - proc[i][0] + 1;

if (wt[i] < 0)
{
wt[i] = 0;
}
}
}

static void GetTatTime(int[] tat, int[] wt)


{
for (int i = 0; i < totalprocess; i++)
{
tat[i] = proc[i][1] + wt[i];
}
}

static void Findgc()


{

int[] wt = new int[totalprocess];


int[] tat = new int[totalprocess];
int wavg = 0;
int tavg = 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 :-

You might also like