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

Round Robin explaintion with code

Uploaded by

hgull8490
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Round Robin explaintion with code

Uploaded by

hgull8490
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Round Robin Implementation

#include<stdio.h> // Including standard input-output library


#include<conio.h> // Including console input-output library (Not required in modern C)

struct process // Structure to store process details


{
int pid, bt, tt, wt; // pid = Process ID, bt = Burst Time, tt = Turnaround Time, wt = Waiting Time
};

int main()
{
struct process x[10], p[30]; // x stores original process details, p stores the execution details
int i, j, k, tot = 0, m, n; // tot = total burst time, m = time slice (quantum), n = number of processes
float wttime = 0.0, tottime = 0.0, a1, a2; // wttime = total waiting time, tottime = total turnaround
time, a1 = avg waiting time, a2 = avg turnaround time

// Prompt the user to enter the number of processes


printf("\nEnter the number of process:\t");
scanf("%d", &n); // Read number of processes

// Input burst time for each process and calculate total burst time
for(i = 1; i <= n; i++)
{
x[i].pid = i; // Assign process ID
printf("\nEnter the Burst Time:\t");
scanf("%d", &x[i].bt); // Read burst time for process i
tot = tot + x[i].bt; // Accumulate total burst time
}
// Display total burst time for all processes
printf("\nTotal Burst Time:\t%d", tot);

p[0].tt = 0; // The turnaround time for the first process is 0 (it starts execution at time 0)
k = 1; // Initialize process counter for execution tracking
// Prompt the user to enter the time slice (quantum)
printf("\nEnter the Time Slice:\t");
scanf("%d", &m); // Read time slice (quantum)

// Loop to simulate the Round Robin scheduling


for(j = 1; j <= tot; j++) // The loop runs for the total burst time
{
// Loop through all processes to allocate CPU time
for(i = 1; i <= n; i++)
{
if(x[i].bt != 0) // If the process still has burst time left
{
p[k].pid = i; // Store process ID in the execution order

// Check if the remaining burst time of the process is less than the time slice
if(x[i].bt - m < 0)
{
p[k].wt = p[k - 1].tt; // Waiting time is the turnaround time of the previous process
p[k].bt = x[i].bt; // Set the burst time for this process to the remaining burst time
p[k].tt = p[k].wt + x[i].bt; // Turnaround time is waiting time plus burst time
x[i].bt = 0; // The process has finished its execution
k++; // Increment the process counter
}
else // If the remaining burst time is greater than the time slice
{
p[k].wt = p[k - 1].tt; // Waiting time is the turnaround time of the previous process
p[k].tt = p[k].wt + m; // Turnaround time is waiting time plus the time slice
x[i].bt = x[i].bt - m; // Decrease the burst time of the process by the time slice
k++; // Increment the process counter
}
}
}
}

// Output the process execution details (ID, waiting time, turnaround time)
printf("\nProcess id \twt \ttt");
for(i = 1; i < k; i++) // Iterate through the executed processes
{
printf("\n\t%d \t%d \t%d", p[i].pid, p[i].wt, p[i].tt); // Print Process ID, Waiting Time, and
Turnaround Time
wttime = wttime + p[i].wt; // Accumulate total waiting time
tottime = tottime + p[i].tt; // Accumulate total turnaround time
a1 = wttime / n; // Average waiting time = total waiting time / number of processes
a2 = tottime / n; // Average turnaround time = total turnaround time / number of processes
}

// Output the average waiting time and turnaround time


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

return 0; // End of the program


}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy