forked from brunoanndre/ScheduleSO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule_edf.c
97 lines (84 loc) · 3.19 KB
/
schedule_edf.c
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
#include "schedule_edf.h"
#include "list.c"
#include <locale.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
struct node *head = NULL;
LARGE_INTEGER frequency;
int unitTimeCounter;
// add a task to the list
void add(char *name, int priority, int burst, int deadline) {
Task *task = malloc(sizeof(Task));
task->name = name;
task->priority = priority;
task->burst = burst;
task->deadline = deadline;
insert(&head, task);
}
void sortByDeadline(struct node *head) { // order tasks by deadline
struct node *i, *j;
Task *tempTask;
for (i = head; i != NULL; i = i->next) {
for (j = i->next; j != NULL; j = j->next) {
if (i->task->deadline > j->task->deadline) {
tempTask = i->task;
i->task = j->task;
j->task = tempTask;
}
}
}
}
double elapsedTime(LARGE_INTEGER startTime){
LARGE_INTEGER endTime;
QueryPerformanceCounter(&endTime);
double totalTime = (double)(endTime.QuadPart - startTime.QuadPart) / (double)frequency.QuadPart;
return totalTime;
}
// invoke the scheduler
void scheduleEDF() {
sortByDeadline(head);
struct node *temp = head;
QueryPerformanceFrequency(&frequency);
LARGE_INTEGER startTime;
QueryPerformanceCounter(&startTime);
while (temp != NULL) {
printf("-----------------------------------------\n");
int currentDeadline = temp->task->deadline;
while (temp != NULL && temp->task->deadline == currentDeadline) {
if(temp->task->burst > 0){
int limitToExecute = temp->task->deadline - temp->task->burst;
if(limitToExecute < unitTimeCounter){
printf("The task %s could not be performed, therefore it has been discarted.\n\n", temp->task->name);
delete(&head, temp->task);
temp = temp->next;
}else{
runEDF(temp->task, QUANTUM);
unitTimeCounter += QUANTUM;
temp->task->burst -= QUANTUM;
if (temp->task->burst == 0) {
char *name = temp->task->name;
struct node *next = temp->next;
delete(&head, temp->task); // remove task from the list
printf("It took %.9f seconds to complete the task %s. \n\n", elapsedTime(startTime), name);
}
if (temp->next != NULL && temp->next->task->deadline == currentDeadline) {
temp = temp->next;
} else {
temp = head;
}
}
}
printf("----------------------------------------\n");
printf("%d units of elapsed time\n",unitTimeCounter);
printf("----------------------------------------\n\n");
}
if (temp != NULL) {
currentDeadline = temp->task->deadline;
}
}
printf("----------------------------------------\n");
printf("It took %.9f seconds to complete all tasks.\n", elapsedTime(startTime));
printf("----------------------------------------\n");
}