-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule_priority_rr.c
More file actions
99 lines (77 loc) · 2.23 KB
/
Copy pathschedule_priority_rr.c
File metadata and controls
99 lines (77 loc) · 2.23 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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "task.h"
#define MAX_PRIORITY 10
#define MIN_PRIORITY 1
#define QUANTUM 10
struct node* priority_queue[MAX_PRIORITY] = {NULL};
// Add a task to the list
void add(char* name, int priority, int burst) {
Task* temp = malloc(sizeof(Task));
temp->name = name;
temp->priority = priority;
temp->burst = burst;
temp->tid = 0;
insert(&priority_queue[priority], temp);
}
void schedule() {
int time = 0;
int dispatch_time = -1;
int run_time = 0;
int is_empty = 0;
int index = MAX_PRIORITY;
int size = 0;
int pos = 0;
int count = 0;
while (!is_empty) {
struct node* temp = priority_queue[index];
if (temp == NULL) {
index--;
continue;
}
if (index <= MIN_PRIORITY) {
is_empty = 1;
}
size = 0;
while (temp->next != NULL) {
temp = temp->next;
size++;
}
pos = size;
while (priority_queue[index] != NULL) {
temp = priority_queue[index];
count = 0;
if (pos < 0) {
pos = size;
}
//Grab elements from back to ensure that tasks are run in lexicographical order
while (count < pos) {
temp = temp->next;
count++;
if (temp->next == NULL) {
pos = count;
break;
}
}
if (temp->task->burst < QUANTUM) {
run_time = temp->task->burst;
} else {
run_time = QUANTUM;
}
dispatch_time++;
time += run_time;
temp->task->burst -= run_time;
printf("Running task = [%s] [%d] [%d] for %d units.\n",
temp->task->name, temp->task->priority, temp->task->burst,
run_time);
printf(" Time is now: %d\n", time);
if (temp->task->burst <= 0) {
delete (&priority_queue[index], temp->task);
}
pos--;
}
}
dispatch_time += time;
printf("CPU Utilization: %.2f%%\n", ((float)time / dispatch_time) * 100);
}