From 6b6e38daa1d067ed3a0629cf1d50377d69aa3fb7 Mon Sep 17 00:00:00 2001 From: tanvir627 Date: Sun, 12 Jan 2025 11:04:31 +0600 Subject: [PATCH] Improve RoundRobin --- algorithms/roundrobin.c | 126 ++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 68 deletions(-) diff --git a/algorithms/roundrobin.c b/algorithms/roundrobin.c index c3c88c52..36c79b6d 100644 --- a/algorithms/roundrobin.c +++ b/algorithms/roundrobin.c @@ -1,69 +1,59 @@ -#include - -int main() -{ - int i, limit, total = 0, x, counter = 0, time_quantum; - int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10]; - float average_wait_time, average_turnaround_time; - printf("nEnter Total Number of Processes:t"); - scanf("%d", &limit); - x = limit; - for(i = 0; i < limit; i++) - { - printf("nEnter Details of Process[%d]n", i + 1); - - printf("Arrival Time:t"); - - scanf("%d", &arrival_time[i]); - - printf("Burst Time:t"); - - scanf("%d", &burst_time[i]); - - temp[i] = burst_time[i]; - } - - printf("nEnter Time Quantum:t"); - scanf("%d", &time_quantum); - printf("nProcess IDttBurst Timet Turnaround Timet Waiting Timen"); - for(total = 0, i = 0; x != 0;) - { - if(temp[i] <= time_quantum && temp[i] > 0) - { - total = total + temp[i]; - temp[i] = 0; - counter = 1; - } - else if(temp[i] > 0) - { - temp[i] = temp[i] - time_quantum; - total = total + time_quantum; - } - if(temp[i] == 0 && counter == 1) - { - x--; - printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]); - wait_time = wait_time + total - arrival_time[i] - burst_time[i]; - turnaround_time = turnaround_time + total - arrival_time[i]; - counter = 0; - } - if(i == limit - 1) - { - i = 0; - } - else if(arrival_time[i + 1] <= total) - { - i++; - } - else - { - i = 0; - } - } - - average_wait_time = wait_time * 1.0 / limit; - average_turnaround_time = turnaround_time * 1.0 / limit; - printf("nnAverage Waiting Time:t%f", average_wait_time); - printf("nAvg Turnaround Time:t%fn", average_turnaround_time); - return 0; +#include + +int main() { + int i, limit, total = 0, counter = 0, time_quantum; + int wait_time = 0, turnaround_time = 0; + int arrival_time[10], burst_time[10], temp[10]; + float avg_wait_time, avg_turnaround_time; + + // Input total number of processes + printf("Enter Total Number of Processes: "); + scanf("%d", &limit); + + // Input arrival and burst time for each process + for (i = 0; i < limit; i++) { + printf("Enter Arrival Time and Burst Time for Process[%d]: ", i + 1); + scanf("%d %d", &arrival_time[i], &burst_time[i]); + temp[i] = burst_time[i]; // Store burst time for future reference + } + + // Input time quantum + printf("Enter Time Quantum: "); + scanf("%d", &time_quantum); + + printf("\nProcess ID | Burst Time | Turnaround Time | Waiting Time\n"); + + // Round Robin Scheduling + for (total = 0, i = 0; counter < limit;) { + if (temp[i] <= time_quantum && temp[i] > 0) { + total += temp[i]; + temp[i] = 0; + counter++; + printf("Process[%d] | %d | %d | %d\n", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]); + wait_time += total - arrival_time[i] - burst_time[i]; + turnaround_time += total - arrival_time[i]; + } else if (temp[i] > 0) { + temp[i] -= time_quantum; + total += time_quantum; + } + + // Move to next process + if (i == limit - 1) { + i = 0; + } else if (arrival_time[i + 1] <= total) { + i++; + } else { + i = 0; + } + } + + // Calculate average waiting and turnaround times + avg_wait_time = (float) wait_time / limit; + avg_turnaround_time = (float) turnaround_time / limit; + + // Display results + printf("\nAverage Waiting Time: %.2f\n", avg_wait_time); + printf("Average Turnaround Time: %.2f\n", avg_turnaround_time); + + return 0; }