-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRound_Robin_Scheduling.cpp
More file actions
154 lines (141 loc) · 2.99 KB
/
Round_Robin_Scheduling.cpp
File metadata and controls
154 lines (141 loc) · 2.99 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<stdio.h>
#include<conio.h>
#define MAX 100
int queue[MAX];
int i,j,time = 0, front = 0, rear = 0, queue_count = 0;
struct rr {
int arrival, fin, burst, tat, wt, pid;
};
void insert(int value) {
queue[rear++] = value;
}
int del() {
int x;
x = queue[front++];
return x;
}
//to check if process has arrived
void check(struct rr p[],int n) {
while (p[j].arrival <= time&&j<n) {
queue_count++;
insert(j++);
}
}
void finish_s(struct rr p[], int quantum,int n) {
//finding total burst time
int total_burst=0;
for (i = 0; i < n; i++) {
total_burst = p[i].burst + total_burst;
}
int temp_st[MAX], flag = 0, count = 0, proc;
j = 0;
//store the burst time in temporary variable
for (i = 0; i<n; i++) {
temp_st[i] = p[i].burst;
}
time = p[0].arrival;
queue_count = 1; //queue_count to check if queue is empty
insert(j++);
while (time <= total_burst) {
if (flag == 1 || queue_count != 0) { //flag to check if process isunder execution
if (flag == 0 && count == 0) {
proc = del();
count = 0;
flag = 1;
}
temp_st[proc]--;
if (temp_st[proc] == 0) {
time++;
count = 0;
p[proc].fin = time;
flag = 0;
queue_count--;
check(p,n);
continue;
}
count++;
if (count == quantum) {
count = 0;
time++;
check(p,n);
insert(proc);
flag = 0;
}
else {
time++;
check(p,n);
}
}
else {
time++;
check(p,n);
}
}
}
void print_table(struct rr p[],int n,float avg_tat,float avg_wt) {
printf("\nprocess\tarrival\tburst\tfinish\ttat\twt\n");
for (j = 0; j < n; j++)
{
printf("\np%d\t%d\t%d\t%d\t%d\t%d\n", p[j].pid, p[j].arrival, p[j].burst, p[j].fin, p[j].tat, p[j].wt);
}
printf("\nthe avg tat= %f and avg wt= %f\n", avg_tat, avg_wt);
}
void avt_tat(struct rr p[],int n) {
for (i = 0; i < n; i++)
{
p[i].tat = p[i].fin - p[i].arrival;
}
for (i = 0; i < n; i++)
{
p[i].wt = p[i].tat - p[i].burst;
}
float avg_tat = 0;
for (i = 0; i < n; i++)
{
avg_tat = p[i].tat + avg_tat;
}
avg_tat = avg_tat / n;
float avg_wt = 0;
for (i = 0; i < n; i++)
{
avg_wt = p[i].wt + avg_wt;
}
avg_wt = avg_wt / n;
print_table(p,n,avg_tat, avg_wt);
}
void main() {
int n;
struct rr p[MAX];
printf("\nentre the number of processes\n");
scanf_s("%d", &n);
printf("\nenter the arrival and burst time\n");
for (i = 0; i < n; i++) {
p[i].pid = i;
scanf_s("%d %d", &p[i].arrival, &p[i].burst);
}
int quantum;
printf("\nenter the quantum time\n");
scanf_s("%d", &quantum);
//sorting
int temp_arr, temp_burst, temp_pid;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (p[j].arrival < p[i].arrival) {
temp_arr = p[i].arrival;
p[i].arrival = p[j].arrival;
p[j].arrival = temp_arr;
temp_burst = p[i].burst;
p[i].burst = p[j].burst;
p[j].burst = temp_burst;
temp_pid = p[i].pid;
p[i].pid = p[j].pid;
p[j].pid = temp_pid;
}
}
}
//to find finish time of each process
finish_s(p, quantum,n);
//to find avg_tat and avg_wt of process
avt_tat(p,n);
_getch();
}