Round Robin RR scheduling algorithm Program Code in c and C++ with gantt chart.
C++ Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include <bits/stdc++.h> using namespace std; const int N=100005; int n,quantum_time; struct process { int id; int burst_time; int arrival_time; int waiting_time; int finishing_time; int turn_around_time; int remaining_time; }; process P[N]; void RoundRobin() { int complete,current_time,change; double total_waiting_time = 0.0; double total_turn_around_time = 0.0; for(int i=0; i<n; i++) P[i].remaining_time = P[i].burst_time; complete = 0; current_time = 0; while(complete < n) { change = 0; for(int i=0; i<n; i++) { if(P[i].arrival_time <= current_time && P[i].remaining_time > 0) { if(P[i].remaining_time <= quantum_time) { complete++; current_time += P[i].remaining_time; P[i].finishing_time = current_time; P[i].turn_around_time = P[i].finishing_time - P[i].arrival_time; P[i].waiting_time = P[i].turn_around_time - P[i].burst_time; total_waiting_time += P[i].waiting_time; total_turn_around_time += P[i].turn_around_time; P[i].remaining_time = 0; } else { current_time += quantum_time; P[i].remaining_time -= quantum_time; } change++; } } if(change == 0) { current_time++; } } cout<<fixed<<setprecision(2); cout<<"Average Waiting Time: "<<(total_waiting_time/n)<<"\n"; cout<<"Average Turn Around Time: "<<(total_turn_around_time/n)<<"\n"; return; } int main() { cout<<"Number of Processes: "; cin>>n; cout<<"Quantum time: "; cin>>quantum_time; cout<<"Process Ids:\n"; for(int i=0; i<n; i++) cin>>P[i].id; cout<<"Process Burst Times:\n"; for(int i=0; i<n; i++) cin>>P[i].burst_time; cout<<"Process Arrival Times:\n"; for(int i=0; i<n; i++) cin>>P[i].arrival_time; RoundRobin(); return 0; } /** 3 4 1 2 3 24 3 3 0 0 0 */ |
C Program Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
#include<stdio.h> struct times { int p,art,but,wtt,tat,rnt; }; void sortart(struct times a[],int pro) { int i,j; struct times temp; for(i=0;i<pro;i++) { for(j=i+1;j<pro;j++) { if(a[i].art > a[j].art) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return; } int main() { int i,j,pro,time,remain,flag=0,ts; struct times a[100]; float avgwt=0,avgtt=0; printf("Round Robin Scheduling Algorithm\n"); printf("Note -\n1. Arrival Time of at least on process should be 0\n2. CPU should never be idle\n"); printf("Enter Number Of Processes : "); scanf("%d",&pro); remain=pro; for(i=0;i<pro;i++) { printf("Enter arrival time and Burst time for Process P%d : ",i); scanf("%d%d",&a[i].art,&a[i].but); a[i].p = i; a[i].rnt = a[i].but; } sortart(a,pro); printf("Enter Time Slice OR Quantum Number : "); scanf("%d",&ts); printf("\n***************************************\n"); printf("Gantt Chart\n"); printf("0"); for(time=0,i=0;remain!=0;) { if(a[i].rnt<=ts && a[i].rnt>0) { time = time + a[i].rnt; printf(" -> [P%d] <- %d",a[i].p,time); a[i].rnt=0; flag=1; } else if(a[i].rnt > 0) { a[i].rnt = a[i].rnt - ts; time = time + ts; printf(" -> [P%d] <- %d",a[i].p,time); } if(a[i].rnt==0 && flag==1) { remain--; a[i].tat = time-a[i].art; a[i].wtt = time-a[i].art-a[i].but; avgwt = avgwt + time-a[i].art-a[i].but; avgtt = avgtt + time-a[i].art; flag=0; } if(i==pro-1) i=0; else if(a[i+1].art <= time) i++; else i=0; } printf("\n\n"); printf("***************************************\n"); printf("Pro\tArTi\tBuTi\tTaTi\tWtTi\n"); printf("***************************************\n"); for(i=0;i<pro;i++) { printf("P%d\t%d\t%d\t%d\t%d\n",a[i].p,a[i].art,a[i].but,a[i].tat,a[i].wtt); } printf("***************************************\n"); avgwt = avgwt/pro; avgtt = avgtt/pro; printf("Average Waiting Time : %.2f\n",avgwt); printf("Average Turnaround Time : %.2f\n",avgtt); return 0; } /* Inputs 4 2 8 1 3 0 5 3 6 3 */ /* Round Robin Scheduling Algorithm Note - 1. Arrival Time of at least on process should be 0 2. CPU should never be idle Enter Number Of Processes : 4 Enter arrival time and Burst time for Process P0 : 2 8 Enter arrival time and Burst time for Process P1 : 1 3 Enter arrival time and Burst time for Process P2 : 0 5 Enter arrival time and Burst time for Process P3 : 3 6 Enter Time Slice OR Quantum Number : 3 *************************************** Gantt Chart 0 -> [P2] <- 3 -> [P1] <- 6 -> [P0] <- 9 -> [P3] <- 12 -> [P2] <- 14 -> [P0] <- 17 -> [P3] <- 20 -> [P0] <- 22 *************************************** Pro ArTi BuTi TaTi WtTi *************************************** P2 0 5 14 9 P1 1 3 5 2 P0 2 8 20 12 P3 3 6 17 11 *************************************** Average Waiting Time : 8.50 Average Turnaround Time : 14.00 */ |