-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundRobin.c
More file actions
111 lines (96 loc) · 3.18 KB
/
RoundRobin.c
File metadata and controls
111 lines (96 loc) · 3.18 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Process structure (Node of circular linked list)
struct Process {
int pid; // Process ID
int burst_time; // Remaining burst time
int waiting_time;
int turnaround_time;
int completed;
struct Process *next; // Pointer to next process (circular)
};
// Function to create a new process node
struct Process* createProcess(int pid, int burst_time) {
struct Process* newProcess = (struct Process*)malloc(sizeof(struct Process));
newProcess->pid = pid;
newProcess->burst_time = burst_time;
newProcess->waiting_time = 0;
newProcess->turnaround_time = 0;
newProcess->completed = 0;
newProcess->next = NULL;
return newProcess;
}
// Function to add process to circular linked list
void addProcess(struct Process** head, int pid, int burst_time) {
struct Process* newProcess = createProcess(pid, burst_time);
if (*head == NULL) {
*head = newProcess;
newProcess->next = *head; // circular
} else {
struct Process* temp = *head;
while (temp->next != *head)
temp = temp->next;
temp->next = newProcess;
newProcess->next = *head;
}
}
// Round Robin Scheduling function
void roundRobin(struct Process* head, int time_quantum) {
int time = 0, completed = 0, n = 0;
struct Process* temp = head;
// Count number of processes
do {
n++;
temp = temp->next;
} while (temp != head);
printf("\n--- Round Robin CPU Scheduling ---\n");
printf("Gantt Chart: \n");
temp = head;
while (completed < n) {
if (temp->burst_time > 0) {
printf("| P%d ", temp->pid);
if (temp->burst_time > time_quantum) {
time += time_quantum;
temp->burst_time -= time_quantum;
} else {
time += temp->burst_time;
temp->waiting_time = time - temp->turnaround_time - temp->burst_time;
temp->burst_time = 0;
temp->completed = 1;
temp->turnaround_time = time;
completed++;
}
}
temp = temp->next;
}
printf("|\n");
// Print results
printf("\nProcess\tWaiting Time\tTurnaround Time\n");
float totalWT = 0, totalTAT = 0;
temp = head;
do {
printf("P%d\t%d\t\t%d\n", temp->pid, temp->waiting_time, temp->turnaround_time);
totalWT += temp->waiting_time;
totalTAT += temp->turnaround_time;
temp = temp->next;
} while (temp != head);
printf("\nAverage Waiting Time: %.2f", totalWT / n);
printf("\nAverage Turnaround Time: %.2f\n", totalTAT / n);
}
// Main function
int main() {
struct Process* head = NULL;
int n, i, bt, tq;
printf("Enter number of processes: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Enter burst time for Process %d: ", i);
scanf("%d", &bt);
addProcess(&head, i, bt);
}
printf("Enter Time Quantum: ");
scanf("%d", &tq);
roundRobin(head, tq);
return 0;
}