Round Robin explaintion with code
Round Robin explaintion with code
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
// 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)
// 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
}