-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule_priority.c
More file actions
62 lines (49 loc) · 1.59 KB
/
Copy pathschedule_priority.c
File metadata and controls
62 lines (49 loc) · 1.59 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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "task.h"
#define MAX_PRIORITY 10
#define MIN_PRIORITY 1
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 is_empty = 0;
int index = MAX_PRIORITY;
while (!is_empty) {
// Find the highest non-empty priority queue
while (index >= MIN_PRIORITY && priority_queue[index] == NULL) {
index--;
}
if (index < MIN_PRIORITY) {
is_empty = 1;
continue;
}
// Process all tasks in the current priority queue
while (priority_queue[index] != NULL) {
struct node* temp = priority_queue[index];
// Move to last element in the linked list
while (temp->next != NULL) {
temp = temp->next;
}
dispatch_time++;
time += temp->task->burst;
printf("Running task = [%s] [%d] [%d] for %d units.\n",
temp->task->name, temp->task->priority, temp->task->burst,
temp->task->burst);
printf(" Time is now: %d\n", time);
delete (&priority_queue[index], temp->task);
}
}
dispatch_time += time;
printf("CPU Utilization: %.2f%%\n", ((float)time / dispatch_time) * 100);
}