-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
122 lines (89 loc) · 3.05 KB
/
test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <math.h>
// int nums[] = {37,11,7,5};
// int numbers = 4,
int active_threads = 3;
// int threads_test[] = {5,4,3,2,1};
// int lcm(int index, int mcm){
// if(index == numbers-1) return mcm;
// int b = nums[++index];
// int max = (mcm > b) ? mcm : b;
// while((max % mcm != 0) || (max % b != 0))
// max++;
// printf("found %d\n", max);
// return lcm(index, max);
// }
// Structs
struct th_info {
unsigned long id;
int period;
int comptime;
int priority;
// float utilization;
};
struct thread {
struct th_info info; // Thread info
int rem_time; // Remaining time
};
void order_ths(struct thread* threads_test){
struct thread tmp;
for(int i=0; i<active_threads-1; i++)
if(threads_test[i].info.priority > threads_test[i+1].info.priority){
for(int j=i; j<active_threads-1; j++){
tmp = threads_test[j];
threads_test[j] = threads_test[j+1];
threads_test[j+1] = tmp;
}
i--;
}
}
// struct thread threads[] = {{{3,50,10,3},3},{{2,30,6,2},2},{{1,20,10,1},1}};
struct thread threads[] = {
{{3, 5, 1, 3}, 3},
{{2, 3, 1, 2},2},
{{1, 2, 1, 1},1},
};
int is_RM(){return 0;}
int is_schedulable(){
if(is_RM()) return 1; // The task set respects the U_lub
order_ths(threads); // Order priorities in descending order
int expired = 0; // Flag to check if a deadline is not respected
int prev_respt, curr_respt;
// Check all threads
for(int i=0; !expired && i<active_threads; i++){
printf("--- Task %d ---\n", i+1);
curr_respt = threads[i].info.comptime; // Just to pass the next while
prev_respt = curr_respt-1;
// Continue for (undefined) iterations
int c = 0;
while(prev_respt < curr_respt && !expired){
prev_respt = curr_respt;
// printf("[iter: %d] New approx: ", i+1);
// Consider all interferences from higher-priority tasks
curr_respt = threads[i].info.comptime;
printf("w(%d) = %d", c, curr_respt);
for(int j=i-1; j>-1; j--){
printf(" + ceil(%d / %d) * %d", prev_respt, threads[j].info.period, threads[j].info.comptime);
curr_respt += ((int)ceil((double)prev_respt/(double)threads[j].info.period)) * threads[j].info.comptime;
}
printf(" = %d\n", curr_respt);
if(curr_respt > threads[i].info.period){
expired = 1;
break;
}
c++;
}
if(expired) break;
}
return !expired;
}
int main(){
printf("Schedulable: %d\n", is_schedulable());
// printf("lcm: %d\n" , lcm(0, nums[0]));
// for(int i=0; i<active_threads; i++) printf("%d ", threads_test[i]);
// printf("\n");
// order_ths();
// for(int i=0; i<active_threads; i++) printf("%d ", threads_test[i]);
// printf("\n");
return 0;
}