-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrr.c
More file actions
189 lines (169 loc) · 6.03 KB
/
Copy pathrr.c
File metadata and controls
189 lines (169 loc) · 6.03 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <stdlib.h>
void calculateTimes(int n, int arrival[], int burst[], int waiting[], int turnaround[], int quantum) {
int remaining_time[n];
for (int i = 0; i < n; i++) {
remaining_time[i] = burst[i]; // Initialize remaining time with burst time
}
int time = arrival[0]; // Start time is the arrival time of the first process
int done = 0; // Number of completed processes
while (done < n) {
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0 && arrival[i] <= time) {
if (remaining_time[i] > quantum) {
// Process executes for the quantum time
time += quantum;
remaining_time[i] -= quantum;
} else {
// Process completes execution
time += remaining_time[i];
waiting[i] = time - arrival[i] - burst[i];
turnaround[i] = time - arrival[i];
remaining_time[i] = 0;
done++;
}
}
}
}
}
void displayResults(int n, int pid[], int arrival[], int burst[], int waiting[], int turnaround[]) {
float total_waiting = 0, total_turnaround = 0;
printf("\nPID\tArrival\tBurst\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++) {
total_waiting += waiting[i];
total_turnaround += turnaround[i];
printf("P%d\t%d\t%d\t%d\t%d\n", pid[i], arrival[i], burst[i], waiting[i], turnaround[i]);
}
printf("\nAverage Waiting Time: %.2f", total_waiting / n);
printf("\nAverage Turnaround Time: %.2f\n", total_turnaround / n);
}
void displayGanttChart(int n, int pid[], int arrival[], int burst[], int quantum) {
printf("\nGantt Chart:\n");
int remaining_time[n];
for (int i = 0; i < n; i++) {
remaining_time[i] = burst[i]; // Initialize remaining time with burst time
}
int time = arrival[0]; // Start time is the arrival time of the first process
int done = 0; // Number of completed processes
// Top border of the Gantt chart
printf(" ");
while (done < n) {
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0 && arrival[i] <= time) {
if (remaining_time[i] > quantum) {
for (int j = 0; j < quantum; j++) printf("--");
printf(" ");
time += quantum;
remaining_time[i] -= quantum;
} else {
for (int j = 0; j < remaining_time[i]; j++) printf("--");
printf(" ");
time += remaining_time[i];
remaining_time[i] = 0;
done++;
}
}
}
}
printf("\n|");
// Reset remaining_time and done for the process sequence
for (int i = 0; i < n; i++) {
remaining_time[i] = burst[i];
}
done = 0;
time = arrival[0];
// Process sequence in the Gantt chart
while (done < n) {
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0 && arrival[i] <= time) {
if (remaining_time[i] > quantum) {
printf(" P%d |", pid[i]);
time += quantum;
remaining_time[i] -= quantum;
} else {
printf(" P%d |", pid[i]);
time += remaining_time[i];
remaining_time[i] = 0;
done++;
}
}
}
}
printf("\n");
// Bottom border of the Gantt chart
printf(" ");
for (int i = 0; i < n; i++) {
remaining_time[i] = burst[i];
}
done = 0;
time = arrival[0];
while (done < n) {
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0 && arrival[i] <= time) {
if (remaining_time[i] > quantum) {
for (int j = 0; j < quantum; j++) printf("--");
printf(" ");
time += quantum;
remaining_time[i] -= quantum;
} else {
for (int j = 0; j < remaining_time[i]; j++) printf("--");
printf(" ");
time += remaining_time[i];
remaining_time[i] = 0;
done++;
}
}
}
}
printf("\n");
// Timeline below the Gantt chart
time = arrival[0];
done = 0;
for (int i = 0; i < n; i++) {
remaining_time[i] = burst[i];
}
printf("%d", time);
while (done < n) {
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0 && arrival[i] <= time) {
if (remaining_time[i] > quantum) {
time += quantum;
remaining_time[i] -= quantum;
printf(" %d", time);
} else {
time += remaining_time[i];
remaining_time[i] = 0;
done++;
printf(" %d", time);
}
}
}
}
printf("\n");
}
int main() {
int n, quantum;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter time quantum: ");
scanf("%d", &quantum);
int *arrival = (int *)malloc(n * sizeof(int));
int *burst = (int *)malloc(n * sizeof(int));
int *waiting = (int *)malloc(n * sizeof(int));
int *turnaround = (int *)malloc(n * sizeof(int));
int *pid = (int *)malloc(n * sizeof(int)); // Array to store process IDs
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &arrival[i], &burst[i]);
pid[i] = i + 1; // Assign process IDs (P1, P2, ...)
}
calculateTimes(n, arrival, burst, waiting, turnaround, quantum);
displayResults(n, pid, arrival, burst, waiting, turnaround);
displayGanttChart(n, pid, arrival, burst, quantum);
free(arrival);
free(burst);
free(waiting);
free(turnaround);
free(pid);
return 0;
}