RR
// RR
#include <stdio.h>
struct
Process {
int
pid, arrival, burst, remaining, completion, turnaround, waiting, response; };
void
roundRobinScheduling(struct Process p[ ], int n, int quantum) {
int
currentTime=0, completed=0, visited[n];
for
(int i = 0; i < n; i++) visited[i] = 0;
while
(completed <n) ( int idle 1;
for
(int i=0; i<n; 1++) {
if
(p[ i ].remaining>0 && p[ i ].arrival <= currentTime) {
idle=0; if (!visited[i]) (
p[i].response
= currentTime - p[i].arrival;
visited[i]
=1;}
if
(p[i].remaining >quantum) { currentTime
+= quantum;
p[i].remaining
-= quantum; } else {
currentTime
+= p[i].remaining;
p[i].completion
= currentTime;
p[i].remaining
=0; completed++; } } }
if
(idle) currentTime++; }
for
(int i=0; i < n; i++) {
p[
i ].turnaround = p[ i ].completion - p[ i ].arrival;
p[
i ].waiting = p[ i ].turnaround - p[ i ].burst;
} }
void
printTable(struct Process p[ ], int n) {
float
totalTAT = 0, totalWT = 0, totalRT = 0;
printf("\nPID\tArrival\tBurst\tCompletion\tTAT\tWT\tRT\n");
for
(int i = 0; i < n; i++) {
totalTAT
+= p[i].turnaround; totalWT +=
p[i].waiting;
totalRT
+= p[i].response;
printf("%d\t%d\t%d\t%d\t\t%d\t%d\t%d\n",
p[i].pid,
p[i].arrival, p[i].burst, p[i].completion,
p[i].turnaround,
p[i].waiting, p[i].response); }
printf("\nAverage
Turnaround Time: %.2f", totalTAT / n);
printf("\nAverage
Waiting Time: %.2f", totalWT / n);
printf("\nAverage
Response Time: %.2f\n", totalRT / n);
}
int
main() { int n, quantum;
printf("Enter
the number of processes: "); scanf("%d",
&n);
struct
Process p[n]; for (int i = 0; i <
n; i++) {
p[i].pid
=i + 1;
printf("\nProcess
%d Arrival Time: ", i + 1); scanf("%d",
&p[i].arrival);
printf("Process
%d Burst Time: ", i + 1); scanf("%d",
&p[i].burst);
p[i].remaining = p[i].burst; }
printf("\nEnter
Time Quantum: "); scanf("%d",
&quantum);
roundRobinScheduling
(p, n, quantum);
printTable
(p, n); return 0; }
Comments
Post a Comment