-
Notifications
You must be signed in to change notification settings - Fork 0
/
roundRobin.c
139 lines (132 loc) · 3.26 KB
/
roundRobin.c
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
/*
* CS 3104 - Operating Systems
*
* Team 5 - Project
* ROUND ROBIN SCHEDULING
*
* Team Lead: Barcenilla, Jomer Allan G.
* Asst. Team Lead: Monzales, Kathleen Iza M.
* Librarian: Perez, Jameson Adriel S.
* Tejada, Jay P.
* Recorder: Suan, Jose Rico L.
* Yu, Simone Raphael O.
*
* Copyright (C) 2022
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void roundRobin();
void main(){
int exit = 0;
char input;
while(exit == 0){
system("cls");
printf("ROUND ROBIN SCHEDULING\n");
printf("\nPlease choose an option:\n");
printf(" ____________________________\n");
printf("| [1] Start |\n");
printf("|____________________________|\n");
printf("| [2] Exit |\n");
printf("|____________________________|\n");
printf("\nInput: ");
scanf("%c", &input);
fflush(stdin);
switch(input){
case '1':
system("cls");
roundRobin();
break;
case '2':
exit = 1;
break;
default:
break;
}
}
}
void roundRobin(){
int i, j, k, x, process, quantum;
int turnaround = 0, waiting = 0, total = 0, ctr = 0, column = 0;
float avgWait, avgTurn;
// Get number of process
printf("Input number of process: ");
scanf("%d", &process);
x = process;
// Get arrival and burst time
int bTime[process], aTime[process], temp[process];
for(i = 0; i < process; i++){
printf("\nInput Arrival and Burst Time for Process %d", i+1);
printf("\nArrival time: ");
scanf("%d", &aTime[i]);
printf("Burst time: ");
scanf("%d", &bTime[i]);
temp[i] = bTime[i];
}
// Get matrix column
for(i = 0; i < process; i++){
column += bTime[i];
}
// Create matrix
char matrix[process][column];
for(i = 0; i < process; i++){
for(j = 0; j < column; j++){
matrix[i][j] = ' ';
}
}
// Get time quantum
printf("\nInput time quantum: ");
scanf("%d", &quantum);
// Compute the turnaround time and waiting time
system("cls");
printf("Process ID Burst Time Turnaround Time Waiting Time");
for(i = 0, j = 0; x != 0; ){
// Computation
if(temp[i] <= quantum && temp[i] > 0){
for(k = 0; k < temp[i]; k++){
matrix[i][j++] = '+';
}
total = total + temp[i];
temp[i] = 0;
ctr = 1;
}else if(temp[i] > 0){
for(k = 0; k < quantum; k++){
matrix[i][j++] = '+';
}
temp[i] = temp[i] - quantum;
total = total + quantum;
}
// Display Table
if(temp[i]==0 && ctr==1){
x--;
printf("\n %2d %2d %2d %2d", i+1, bTime[i], total-aTime[i], total-aTime[i]-bTime[i]);
waiting = waiting + total - aTime[i] - bTime[i];
turnaround = turnaround + total - aTime[i];
ctr = 0;
}
if(aTime[i+1] <= total && i != process-1){
i++;
}else{
i = 0;
}
}
// Compute the average turn around and waiting time
avgWait = waiting * 1.0 / process;
avgTurn = turnaround * 1.0 / process;
printf("\n\nAverage Turn Around Time: %.4f", avgTurn);
printf("\nAverage Waiting Time: %.4f", avgWait);
// Display gantt chart
printf("\n\nGANTT CHART");
printf("\nProcess ID |");
for(i = 1; i < column+1; i++){
printf("%2d|", i);
}
for(i = 0; i < process; i++){
printf("\n %2d |", i+1);
for(j = 0; j < column; j++){
printf("%2c|", matrix[i][j]);
}
}
printf("\n\nPress any key to continue...\n");
getch();
}