0% found this document useful (0 votes)
11 views3 pages

A-5 - Lab

Uploaded by

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

A-5 - Lab

Uploaded by

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

Program:

Round Robin Scheduling


#include<stdio.h>
#include<conio.h>

Include necessary header files for standard input/output operations and console I/O
functions.
struct process

This declares a structure named process with four members: pid (process ID), bt (burst
time), tt (turnaround time), and wt (waiting time).

{
int pid, bt, tt, wt;
};
int main()

The main function begins.


{
struct process x[10], p[30];
int i, j, k, tot = 0, m, n;

It declares arrays of structures (x and p), along with some variables (i, j, k, tot, m, n,
wttime, tottime, a1, a2).

float wttime = 0.0, tottime = 0.0, a1, a2;


clrscr();

clrscr() is used to clear the console screen, but remember that it's not a standard
function.

printf("\nEnter the number of process:\t");


scanf("%d", &n);

This prompts the user to input the number of processes (n)

for (i = 1; i <= n; i++)


A loop is used to input burst times for each process.
{
x[i].pid = i;
The process ID is set to the loop index.

printf("\nEnter the Burst Time:\t");


scanf("%d", &x[i].bt);
tot = tot + x[i].bt;

The total burst time (tot) is calculated by summing up the burst times of all processes.
}
printf("\nTotal Burst Time:\t%d", tot);

The total burst time is displayed, and then the user is prompted to input the time slice
(m).

p[0].tt = 0;
k = 1;
printf("\nEnter the Time Slice:\t");
scanf("%d", &m);

The turnaround time of the first process in the array p is set to 0.

for (j = 1; j <= tot; j++)


{
for (i = 1; i <= n; i++)

This block implements a round-robin scheduling algorithm using nested loops.

{
if (x[i].bt != 0)
{
p[k].pid = i;
if (x[i].bt - m < 0)
{
p[k].wt = p[k - 1].tt;
p[k].bt = x[i].bt;
p[k].tt = p[k].wt + x[i].bt;
x[i].bt = 0;
k++;
}

It iterates over each time slice and each process, updating the waiting time, burst
time, and turnaround time accordingly.
else
{
p[k].wt = p[k - 1].tt;
p[k].tt = p[k].wt + m;
x[i].bt = x[i].bt - m;
k++;
}
}
}
}

printf("\nProcess id \twt \ttt");


for (i = 1; i < k; i++)
{
The program prints the process ID, waiting time, and turnaround time for each
process.

printf("\n\t%d \t%d \t%d", p[i].pid, p[i].wt, p[i].tt);


wttime = wttime + p[i].wt;
tottime = tottime + p[i].tt;
a1 = wttime / n;
a2 = tottime / n;

}
printf("\n\nAverage Waiting Time:\t%f", a1);
printf("\n\nAverage TurnAround Time:\t%f", a2);

It then calculates and displays the average waiting time and average turnaround time.
getch();

The getch() function is used to wait for a key press before closing the program.

return 0;
}

Note that the calculation of averages (a1 and a2) should be outside the loop that
iterates over the processes to get correct results.

You might also like