-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule_sjf.c
More file actions
48 lines (38 loc) · 1.2 KB
/
Copy pathschedule_sjf.c
File metadata and controls
48 lines (38 loc) · 1.2 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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "task.h"
struct node* head_node;
// 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(&head_node, temp);
}
void schedule() {
int time = 0;
int dispatch_time = -1;
while (head_node != NULL) {
struct node* temp = head_node;
struct node* shortest = head_node;
// Find the task with the shortest burst time
while (temp != NULL) {
if (temp->task->burst <= shortest->task->burst) {
shortest = temp;
}
temp = temp->next;
}
dispatch_time++;
time += shortest->task->burst;
printf("Running task = [%s] [%d] [%d] for %d units.\n",
shortest->task->name, shortest->task->priority,
shortest->task->burst, shortest->task->burst);
printf(" Time is now: %d\n", time);
delete (&head_node, shortest->task);
}
dispatch_time += time;
printf("CPU Utilization: %.2f%%\n", ((float)time / dispatch_time) * 100);
}