-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.c
1398 lines (1291 loc) · 49.6 KB
/
simulator.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <limits.h>
#include <errno.h>
#include <stdbool.h>
#include "simulator.h"
/* scheduler type */
static bool flag_dm; /*!< flag_dm activates the deadline monotonic scheduler */
static bool flag_edf; /*!< flag_edf activates the earliest deadline first scheduler */
/* protocol */
static bool flag_pip;
/* scheduler tick */
static unsigned int tick; /*!< scheduler tick is updated in sim_scheduler() */
/* scheduler flags, params,... */
static unsigned int t_start; /*!< start of simulation */
static unsigned int t_stop; /*!< stop of simulation */
static bool flag_nokill;
static bool flag_kill;
#define STATUS_STRING_LENGTH 100
#define STATUS_STRING_AMOUNT 10
/* deadlines, etc. */
static char actions_status[STATUS_STRING_AMOUNT][STATUS_STRING_LENGTH]; /*!< Buffer for action message. Is reset every tick and set if e.g., a task is preempted or aborted because it missed its deadline */
static int actions_status_index;
/* resources */
static char resource_status[STATUS_STRING_AMOUNT][STATUS_STRING_LENGTH];
static int resource_status_index;
/* tasks */
static list_node *tasks; /*!< List with all tasks. New instances of tasks are created from this list. */
static int task_ids;
static list_node *stats; /*!< List with all global_stats. Each task has its own global_stat. Every instance of a task has a reference to the same global_stats */
static list_node *killed_tasks; /*!< List with all tasks that were killed during schedule simulator. */
static list_node *ready_queue; /*!< Queue with all runnable/running tasks */
static list_node *pending_queue; /*!< Queue with all ready tasks. Because tick scheduling is used, these task must be put into ready_queue */
/* resources */
static list_node *blocked_queue; /*!< Queue with all blocked tasks depending on the ressource */
static list_node *resources; /*!< List with all (available) resources */
/* all prototypes in simulation.c */
void print_status(char status[STATUS_STRING_AMOUNT][STATUS_STRING_LENGTH], int index);
void add_status(char (*dst)[STATUS_STRING_LENGTH], const char src[STATUS_STRING_LENGTH], int *dst_index);
void handle_pending_queue(void);
void handle_ready_queue(void);
void prioritize_tasks(list_node *list, void (*prioritize_task)(list_node *, list_node *));
void prioritize_task_dm(list_node *task, list_node *task2);
void prioritize_task_edf(list_node *task, list_node *task2);
task_struct *pick_next_task(list_node *list);
void remove_task_if_finished_execution(task_struct *task);
void handle_deadline(list_node *task_node, list_node *kill_from_queue);
void decrement_deadline(list_node *task);
void handle_deadline_in_queue(list_node *queue);
void lock_resource(task_struct *task, local_task_resource *resource);
void lock_resources(task_struct *task);
void unlock_resource(task_struct *task, list_node *resource);
void unlock_resources(task_struct *task);
void unlock_resources_if_finished(task_struct *task);
void reset_used_by_references(task_struct *task);
void add_tasks_from_blocked_queue_to_ready_queue(void (*prioritize_task)(list_node *, list_node *));
void handle_tasks_to_delete(list_node **list);
void increment_elapsed(list_node *task);
void handle_reaction_time(void);
void handle_preemption(const task_struct *next, task_struct *prev);
void sim_scheduler(void (*prioritize_task)(list_node *, list_node *));
int get_number(const char *token);
bool check_if_resource_exists(const char *resc_name);
void read_and_parse_input(const char *input_fp);
/**
* Prints the content of a buffer.
* @param status is the buffer that is read.
* @param index gives information about how "full" the buffer is.
*/
void print_status(char status[STATUS_STRING_AMOUNT][STATUS_STRING_LENGTH], int index)
{
int print_index;
for (print_index = 0; print_index < index; ++print_index)
{
printf("%s", status[print_index]);
if (print_index < index - 1)
{
printf(" + ");
}
}
}
/**
* Adds a status message to a buffer.
* @param dst is a pointer to the buffer.
* @param src is where the status message is read from.
* @param dst_index points to the next free slot.
*/
void add_status(char (*dst)[STATUS_STRING_LENGTH], const char src[STATUS_STRING_LENGTH], int *dst_index)
{
strcpy(dst[(*dst_index)++], src);
}
/**
* @brief handle_pending_queue() prepares the pending_queue for the ready_queue.
*
* It iterates through all tasks in tasks and checks if they are ready to be executed.
* If so a deep copy of the task is created and added to the pending queue.
*
* This function only adds entries to the pending_queue, but it does not alter in any other way.
* When sim_schedule() is called pending_queue is reset because it expects all tasks to be transferred to ready_queue.
*/
void handle_pending_queue()
{
list_node *tmp_task_list = tasks;
while (tmp_task_list != NULL)
{
if (((tick >= (tmp_task_list->task->t_ph + tmp_task_list->task->t_per)) &&
(0 == ((tick - tmp_task_list->task->t_ph) % tmp_task_list->task->t_per))) ||
((tick < (tmp_task_list->task->t_ph + tmp_task_list->task->t_per)) && (tick == tmp_task_list->task->t_ph)))
{
tmp_task_list->task->task_id = task_ids;
create_and_add_node_to_list(&pending_queue, copy_node(tmp_task_list->task, task_t), task_t);
++task_ids;
++tmp_task_list->task->task_num;
}
tmp_task_list = tmp_task_list->next; /* select next task */
}
}
/**
* @brief handle_ready_queue() prepares the ready queue for pick_next_task().
*
* It adds all "new" tasks from the pending_queue to the ready_queue.
* This function only adds items but does not remove them.
*/
void handle_ready_queue()
{
list_node *tmp_pending_queue = pending_queue;
while (tmp_pending_queue != NULL)
{
create_and_add_node_to_list(&ready_queue, copy_node(tmp_pending_queue->task, task_t), task_t);
tmp_pending_queue = tmp_pending_queue->next;
}
}
/**
* @brief Prioritizes tasks by calling a flag dependant algorithm passed via prioritize_task as reference.
*
* @param list is the reference in which the tasks are prioritized.
* @param prioritize_task is the algorithm that prioritizes the tasks.
*/
void prioritize_tasks(list_node *list, void (*prioritize_task)(list_node *, list_node *))
{
list_node *tmp_queue_outer;
list_node *tmp_queue_inner;
tmp_queue_outer = list;
while (tmp_queue_outer != NULL)
{
tmp_queue_outer->task->task_priority = 0;
tmp_queue_inner = list;
while (tmp_queue_inner != NULL)
{
prioritize_task(tmp_queue_inner, tmp_queue_outer);
tmp_queue_inner = tmp_queue_inner->next;
}
tmp_queue_outer = tmp_queue_outer->next;
}
}
/**
* @brief Prioritizes task according to the Deadline Monotonic scheduling algorithm.
* This algorithm prioritizes the task with the smallest deadline.
* This function can be seen as a comparator between two tasks.
* It increases the priority of only one task und must thus be used in a loop over all tasks.
*
* @param task is the reference to the first task.
* @param task2 is the reference to another task.
*/
void prioritize_task_dm(list_node *task, list_node *task2)
{
list_node *tmp_priorities;
unsigned int min_deadline;
if ((task != NULL) && (task2 != NULL))
{
min_deadline = task->task->t_d;
#ifdef DEBUG
printf("%u prioritize_task_dm: %s%d has deadline %u\n", tick, task->task->name, task->task->task_num, task->task->t_d);
#endif
/* check if task has inherited task_priorities.*/
if (flag_pip && NULL != task->task->task_priorities)
{
tmp_priorities = task->task->task_priorities;
while (tmp_priorities != NULL)
{
/* select smallest deadline from all inherited priorities */
if (min_deadline > tmp_priorities->local_task_stats->t_d)
{
min_deadline = tmp_priorities->local_task_stats->t_d;
}
tmp_priorities = tmp_priorities->next;
}
#ifdef DEBUG
printf("%u prioritize_task_dm: %s%d has (new) minimal deadline %u\n", tick, task->task->name, task->task->task_num, min_deadline);
#endif
}
if (min_deadline > task2->task->t_d)
{
task2->task->task_priority++;
}
}
}
/**
* @brief Prioritizes task according to the Earliest Deadline First scheduling algorithm.
* This algorithm prioritizes the task with the smallest remaining deadline.
* This function can be seen as a comparator between two tasks.
* It increases the priority of only one task und must thus be used in a loop over all tasks.
*
* @param task is the reference to the first task.
* @param task2 is the reference to another task.
*/
void prioritize_task_edf(list_node *task, list_node *task2)
{
list_node *tmp_priorities;
unsigned int min_remaining_deadline;
if ((task != NULL) && (task2 != NULL))
{
min_remaining_deadline = task->task->local_stats->t_remaining_d;
#ifdef DEBUG
printf("%u prioritize_task_edf: %s%d has deadline %u\n", tick, task->task->name, task->task->task_num, task->task->t_remaining_d);
#endif
/* check if task has inherited task_priorities.*/
if (flag_pip && (NULL != task->task->task_priorities))
{
tmp_priorities = task->task->task_priorities;
while (tmp_priorities != NULL)
{
/* select smallest deadline from all inherited priorities */
if (min_remaining_deadline > tmp_priorities->local_task_stats->t_remaining_d && 0 >= tmp_priorities->local_task_stats->t_remaining_d)
{
min_remaining_deadline = tmp_priorities->local_task_stats->t_remaining_d;
}
tmp_priorities = tmp_priorities->next;
}
#ifdef DEBUG
printf("%u prioritize_task_edf: %s%d has (new) minimal deadline %u\n", tick, task->task->name, task->task->task_num, min_remaining_deadline);
#endif
}
if (min_remaining_deadline > task2->task->local_stats->t_remaining_d)
{
task2->task->task_priority++;
}
}
}
/**
* @brief Picks next task with highest priority. Priorities start from 0 (lowest).
*
* @param list is the list where a prioritized task should be picked.
* @return task_struct* is a reference to a task_struct. It is the task that should run next.
*/
task_struct *pick_next_task(list_node *list)
{
list_node *tmp_queue;
task_struct *next_task;
if ((NULL == list) || (NULL == (list->task)))
{
return NULL;
}
tmp_queue = list;
next_task = tmp_queue->task;
while (tmp_queue != NULL)
{
if (tmp_queue->next != NULL)
{
if (next_task->task_priority < tmp_queue->next->task->task_priority)
{
next_task = tmp_queue->next->task;
}
}
tmp_queue = tmp_queue->next;
}
return next_task;
}
/**
* @brief Checks if tasks finished running and sets status `removed`.
* @param task is a reference to task.
*/
void remove_task_if_finished_execution(task_struct *task)
{
/* mark task with REMOVE_TASK flag. It is later removed from ready_queue. */
if ((task != NULL) && ((task->t_remaining_e - 1) <= 0) && (task->task_state != killed))
{
#ifdef DEBUG
printf("%u remove_task_if_finished_execution: %s%d is now marked to be removed.", tick, task->name, task->task_num);
#endif
task->task_state = removed;
}
}
/**
* @brief Checks if any task in a list has passed its deadline.
* If so, depending on the kill flag, it is either killed or only removed from ready_queue.
* @param task_node is a reference to a task in a list.
* @param kill_from_queue is the queue from which the task should be removed.
*/
void handle_deadline(list_node *task_node, list_node *kill_from_queue)
{
list_node *tmp_queue;
task_struct *task;
char buffer[STATUS_STRING_LENGTH];
task = task_node->task;
#ifdef DEBUG
printf("%d handle_deadline: %s%d: deadline %d, exec %d!\n", tick, task->name, task->task_num, task->local_stats->t_remaining_d, task->t_remaining_e);
#endif
if ((0 >= task->local_stats->t_remaining_d) && (0 < task->t_remaining_e))
{
#ifdef DEBUG
printf("%d handle_deadline: %s%d missed deadline!\n", tick, task->name, task->task_num);
#endif
if (flag_kill)
{
snprintf(buffer, STATUS_STRING_LENGTH, "%s%d missed Deadline (aborted)",
task->name,
task->task_num);
add_status(&actions_status[0], buffer, &actions_status_index);
task->task_state = killed;
/* all other instances of task should be killed too. */
tmp_queue = kill_from_queue;
while (tmp_queue != NULL)
{
if (0 == strcmp(task->name, tmp_queue->task->name))
{
tmp_queue->task->task_state = killed;
}
tmp_queue = tmp_queue->next;
}
}
else if (flag_nokill)
{
snprintf(buffer, STATUS_STRING_LENGTH, "%s%d missed Deadline", task->name,
task->task_num);
add_status(&actions_status[0], buffer, &actions_status_index);
task->task_state = removed;
}
}
}
/**
* @brief Decrements all remaining deadlines. That means not only will the task deadline be decremented but also the inherited deadlines
* in order to calculate the priority correctly.
*
* @param task is a reference to the task whose deadline should be decremented.
*/
void decrement_deadline(list_node *task)
{
list_node *tmp_priorities;
if ((NULL != task) && (task->node_type == task_t))
{
task->task->t_remaining_d--;
task->task->local_stats->t_remaining_d--;
tmp_priorities = task->task->task_priorities;
while (tmp_priorities != NULL)
{
#ifdef DEBUG
printf("%u decrement_deadline: %s%d decrementing inherited deadline from %u ", tick, task->task->name, task->task->task_num, tmp_priorities->local_task_stats->t_remaining_d);
#endif
tmp_priorities->local_task_stats->t_remaining_d--;
#ifdef DEBUG
printf("to %u.\n", tmp_priorities->local_task_stats->t_remaining_d);
#endif
tmp_priorities = tmp_priorities->next;
}
}
}
/**
* @brief Checks if tasks in queue missed their deadline.
*
* Depending on the flag they are either removed from the queue or completely aborted.
*
* if `flag_kill` is set, the task is completely removed from the task list and is never added again to pending_queue
* if `flag_nokill` is set, the task is only removed from ready_queue but can still continue its execution in another instance.
*/
void handle_deadline_in_queue(list_node *queue)
{
/* iterate through ready_queue and check every entry */
list_node *tmp_queue;
tmp_queue = queue;
while (tmp_queue != NULL)
{
handle_deadline(tmp_queue, queue);
decrement_deadline(tmp_queue);
tmp_queue = tmp_queue->next;
}
}
/**
* @brief Locks resource.
* It iterates through all available resources and checks if a task needs the resource.
* Before this happens it is checked if t_for and t_from are within range.
* @param task is a reference to the task that allocates the resource.
* @param resource is a reference to the task resources that gets allocated.
*/
void lock_resource(task_struct *task, local_task_resource *resource)
{
list_node *tmp_ready_queue;
list_node *tmp_prev_ready_queue;
list_node *tmp_resource;
char buffer[STATUS_STRING_LENGTH];
#ifdef DEBUG
if (task != NULL)
{
printf("%d lock_resource: %s%d wants to lock resource %s. It can lock it from %d (it has %d) and holds it for %d (it has %d)!\n", tick, task->name, task->task_num, resource->name, resource->t_from, resource->t_from_elapsed, resource->t_for, resource->t_for_elapsed);
}
#endif
if ((NULL == task) || (blocked == task->task_state))
{
#ifdef DEBUG
if (task != NULL)
{
printf("%d lock_resource: %s%d wanted to lock resource %s. But task is already blocked\n", tick, task->name, task->task_num, resource->name);
}
#endif
return;
}
else if ((resource->t_from_elapsed < resource->t_from) ||
(resource->t_for_elapsed >= resource->t_for))
{
#ifdef DEBUG
if (task != NULL)
{
printf("%d lock_resource: %s%d wanted to lock resource %s. But it is too early or too late\n", tick, task->name, task->task_num, resource->name);
}
#endif
resource->t_from_elapsed++;
return;
}
resource->t_from_elapsed++;
tmp_resource = resources;
while (tmp_resource != NULL)
{
if (0 == (strcmp(tmp_resource->global_task_resource->name, resource->name)))
{
if (NULL == tmp_resource->global_task_resource->used_by)
{
/* if resource is free */
task->task_priorities = NULL;
tmp_resource->global_task_resource->used_by = task;
snprintf(buffer, STATUS_STRING_LENGTH, "%s%d locks %s -> ok",
task->name,
task->task_num,
resource->name);
add_status(&resource_status[0], buffer, &resource_status_index);
#ifdef DEBUG
printf("%d lock_resource: %s%d locks resource %s! Resource now used by %s%d\n", tick, task->name, task->task_num, resource->name, tmp_resource->global_task_resource->used_by->name, tmp_resource->global_task_resource->used_by->task_num);
#endif
}
else if (0 != (strcmp(tmp_resource->global_task_resource->used_by->name, task->name)))
{
/* if resource is blocked by another task */
if (task->blocked_by_resc != NULL)
{
free(task->blocked_by_resc);
}
task->blocked_by_resc = strdup(tmp_resource->global_task_resource->name);
task->task_state = blocked;
#ifdef DEBUG
printf("%d lock_resource: %s%d was blocked because resource %s is locked and used by %s%d!\n", tick, task->name, task->task_num, resource->name, tmp_resource->global_task_resource->used_by->name, tmp_resource->global_task_resource->used_by->task_num);
#endif
/* if blocked task has lower priority it gets a new priority */
if (flag_pip && tmp_resource->global_task_resource->used_by->task_priority < task->task_priority)
{
/*
* pass all needed attributes to task in order to calculate new priority.
* Because of EDF, priority can change every tick! Therefore, deadlines of blocked tasks must be checked! */
task->local_stats->resource_name = strdup(resource->name);
create_and_add_node_to_list(&tmp_resource->global_task_resource->used_by->task_priorities, copy_node(task->local_stats, local_task_stats_t), local_task_stats_t);
}
create_and_add_node_to_list(&blocked_queue, copy_node(task, task_t), task_t);
snprintf(buffer, STATUS_STRING_LENGTH, "%s%d locks %s -> blocked",
task->name,
task->task_num,
resource->name);
add_status(&resource_status[0], buffer, &resource_status_index);
/* remove blocked task from ready_queue */
tmp_ready_queue = ready_queue;
tmp_prev_ready_queue = tmp_ready_queue;
while (tmp_ready_queue != NULL)
{
if (tmp_ready_queue->task == task)
{
#ifdef DEBUG
printf("%u lock_resource: %s%d is going to be removed from ready_queue because it was blocked\n", tick, task->name, task->task_num);
#endif
remove_node_from_list(&ready_queue, tmp_ready_queue, &tmp_prev_ready_queue);
}
tmp_prev_ready_queue = tmp_ready_queue;
tmp_ready_queue = tmp_ready_queue->next;
}
}
else
{
#ifdef DEBUG
printf("%d lock_resource: %s%d locks already resource %s!\n", tick, task->name, task->task_num, resource->name);
#endif
resource->t_for_elapsed++;
}
}
tmp_resource = tmp_resource->next;
}
}
/**
* @brief lock_resources() checks the resources that are dedicated to the task.
* This function only iterates over a task's resources list.
*
* @param task is a reference to task.
*/
void lock_resources(task_struct *task)
{
if (task != NULL)
{
list_node *task_resources = task->resources;
while (task_resources != NULL)
{
lock_resource(task, task_resources->local_task_resource);
task_resources = task_resources->next;
}
}
}
/**
* @brief Frees the resource without any further checking.
*
* @param task that should release its resources.
* @param resource which is blocked by the task.
*/
void unlock_resource(task_struct *task, list_node *resource)
{
char buffer[STATUS_STRING_LENGTH];
list_node *tmp_resource;
list_node *tmp_priority;
list_node *tmp_prev_priority;
#ifdef DEBUG
printf("%d unlock_resource: unlocking resource %s from task %s%d!\n", tick, resource->local_task_resource->name, task->name, task->task_num);
#endif
tmp_resource = resources;
while (tmp_resource != NULL)
{
#ifdef DEBUG
printf("%d unlock_resource: iterating through resources. Checking resource %s! ", tick, tmp_resource->global_task_resource->name);
if (tmp_resource->global_task_resource->used_by != NULL)
{
printf("It is used by %s%d!", tmp_resource->global_task_resource->used_by->name, tmp_resource->global_task_resource->used_by->task_num);
}
printf("\n");
#endif
if ((0 == strcmp(tmp_resource->global_task_resource->name, resource->local_task_resource->name)) &&
(tmp_resource->global_task_resource->used_by == task))
{
#ifdef DEBUG
printf("%d unlock_resource: %s%d unlocks resource %s!\n", tick, task->name, task->task_num, resource->local_task_resource->name);
#endif
snprintf(buffer, STATUS_STRING_LENGTH, "%s%d unlocks: %s",
task->name,
task->task_num,
resource->local_task_resource->name);
add_status(&resource_status[0], buffer, &resource_status_index);
/* remove resource from task so that it cannot relock it */
#ifdef DEBUG
printf("%d unlock_resource: removing %s from %s%d!\n", tick, resource->local_task_resource->name, task->name, task->task_num);
#endif
/* remove inherited priority */
tmp_priority = task->task_priorities;
tmp_prev_priority = tmp_priority;
while (tmp_priority != NULL)
{
if (0 == strcmp(tmp_priority->local_task_stats->resource_name, resource->local_task_resource->name))
{
#ifdef DEBUG
printf("%u unlock_resource: %s%d: %s has now been removed from inherited priority!\n", tick, task->name, task->task_num, tmp_priority->local_task_stats->resource_name);
#endif
remove_node_from_list(&task->task_priorities, tmp_priority, &tmp_prev_priority);
}
tmp_prev_priority = tmp_priority;
tmp_priority = tmp_priority->next;
}
#ifdef DEBUG
printf("%u unlock_resource: %s%d: %s has been now removed resource!\n", tick, task->name, task->task_num, resource->local_task_resource->name);
#endif
remove_node_from_list_noprev(&task->resources, resource);
tmp_resource->global_task_resource->used_by = NULL;
break;
}
tmp_resource = tmp_resource->next;
}
}
/**
* @brief Frees all resources of a task.
* @param task is a reference to the task.
*/
void unlock_resources(task_struct *task)
{
list_node *tmp_resource = task->resources;
while (tmp_resource != NULL)
{
#ifdef DEBUG
printf("%d unlock_resources: %s%d should unlock resource %s!\n", tick, task->name, task->task_num, tmp_resource->local_task_resource->name);
#endif
unlock_resource(task, tmp_resource);
tmp_resource = tmp_resource->next;
}
}
/**
* @brief Iterates through task resources and checks if a resource can be unlocked.
* @param task is a reference to the task.
*/
void unlock_resources_if_finished(task_struct *task)
{
list_node *task_resource;
#ifdef DEBUG
printf("\n");
#endif
if (task != NULL)
{
task_resource = task->resources; /* global_task_resource of task */
while (NULL != task_resource)
{
if (task_resource->local_task_resource->t_from_elapsed >= task_resource->local_task_resource->t_from &&
task_resource->local_task_resource->t_for_elapsed >= task_resource->local_task_resource->t_for)
{
#ifdef DEBUG
printf("%d unlock_resources_if_finished: %s%d unlocks resource %s because [t_for_elapsed] %d >= %d [t_for] !\n", tick, task->name, task->task_num, task_resource->local_task_resource->name, task_resource->local_task_resource->t_for_elapsed, task_resource->local_task_resource->t_for);
#endif
unlock_resource(task, task_resource);
}
task_resource = task_resource->next;
}
}
}
void reset_used_by_references(task_struct *task)
{
list_node *tmp_resource;
if (task != NULL)
{
tmp_resource = resources;
while (tmp_resource != NULL)
{
#ifdef DEBUG
if (NULL != tmp_resource->global_task_resource->used_by)
{
printf("%u reset_used_by_references: %s: %s == %s\n", tick, tmp_resource->global_task_resource->name, tmp_resource->global_task_resource->used_by->name, task->name);
}
#endif
if ((tmp_resource->global_task_resource->used_by != NULL) && (0 == strcmp(tmp_resource->global_task_resource->used_by->name, task->name)) && (tmp_resource->global_task_resource->used_by->task_num == task->task_num))
{
#ifdef DEBUG
printf("%u reset_used_by_references: resetting used_by from %p to %p\n", tick, tmp_resource->global_task_resource->used_by, task);
#endif
tmp_resource->global_task_resource->used_by = task;
}
tmp_resource = tmp_resource->next;
}
}
}
/**
* @brief Iterates through the blocked_queue and puts all tasks that wait for a specific resource
* into a separate temporary queue. After that the "right" task is selected depending on the scheduler.
* This selected task is put into ready state and readded to ready_queue.
*
* @param prioritize_task is the task prioritization algorithm. It depends on the scheduling flag.
*/
void add_tasks_from_blocked_queue_to_ready_queue(void (*prioritize_task)(list_node *, list_node *))
{
list_node *tmp_blocked_queue;
list_node *tmp_blocked;
list_node *tmp_resource;
task_struct *copied_task;
task_struct *picked_task;
tmp_resource = resources;
#ifdef DEBUG
printf("\n");
#endif
while (tmp_resource != NULL)
{
#ifdef DEBUG
printf("%d add_tasks_from_blocked_queue_to_ready_queue: checking resource %s.\n", tick, tmp_resource->global_task_resource->name);
#endif
#ifdef DEBUG
if (tmp_resource->global_task_resource->used_by != NULL)
{
printf("%d add_tasks_from_blocked_queue_to_ready_queue: resource %s is used by %s%d.\n", tick,
tmp_resource->global_task_resource->name,
tmp_resource->global_task_resource->used_by->name,
tmp_resource->global_task_resource->used_by->task_num);
}
#endif
if (NULL == tmp_resource->global_task_resource->used_by)
{
tmp_blocked_queue = NULL;
tmp_blocked = blocked_queue;
while (tmp_blocked != NULL)
{
#ifdef DEBUG
printf("%d add_tasks_from_blocked_queue_to_ready_queue: %s%d waits for %s.\n", tick, tmp_blocked->task->name, tmp_blocked->task->task_num, tmp_blocked->task->blocked_by_resc);
#endif
if (0 == strcmp(tmp_blocked->task->blocked_by_resc, tmp_resource->global_task_resource->name))
{
#ifdef DEBUG
printf("%d add_tasks_from_blocked_queue_to_ready_queue: adding %s%d to queue.\n", tick, tmp_blocked->task->name, tmp_blocked->task->task_num);
#endif
create_and_add_node_to_list(&tmp_blocked_queue, copy_node(tmp_blocked->task, task_t), task_t);
}
tmp_blocked = tmp_blocked->next;
}
if (tmp_blocked_queue != NULL)
{
prioritize_tasks(tmp_blocked_queue, prioritize_task);
picked_task = pick_next_task(tmp_blocked_queue);
#ifdef DEBUG
printf("%d add_tasks_from_blocked_queue_to_ready_queue: %s%d was picked.\n", tick, picked_task->name, picked_task->task_num);
#endif
/* add picked task to ready_queue */
picked_task->task_state = ready;
copied_task = copy_node(picked_task, task_t);
create_and_add_node_to_list(&ready_queue, copied_task, task_t);
reset_used_by_references(copied_task);
tmp_blocked = blocked_queue;
while (tmp_blocked != NULL)
{
if (0 == (strcmp(tmp_blocked->task->name, picked_task->name)) && tmp_blocked->task->task_num == picked_task->task_num)
{
tmp_blocked->task->task_state = removed;
}
tmp_blocked = tmp_blocked->next;
}
}
}
tmp_resource = tmp_resource->next;
}
}
/**
* @brief handle_tasks_to_delete() iterates through ready_queue and checks if there are tasks that have a set task state.
*
* if task state is set to `removed` then the task is removed from the ready_queue
* if task state is set to `killed` then the task is completely aborted and removed from the tasks list.
*/
void handle_tasks_to_delete(list_node **list)
{
list_node *tmp_list;
list_node *tmp_prev_list;
list_node *tmp_task_tasks_list;
list_node *tmp_prev_tasks_list;
killed_task *kill_task;
tmp_list = *list;
tmp_prev_list = *list;
#ifdef DEBUG
printf("\n");
#endif
while (tmp_list != NULL)
{
#ifdef DEBUG
printf("%d handle_tasks_to_delete: %s%d has %u state\n", tick, tmp_list->task->name, tmp_list->task->task_num, tmp_list->task->task_state);
#endif
if ((tmp_list->task->task_state == killed) || (tmp_list->task->task_state == removed))
{
if (killed == (tmp_list->task->task_state))
{
/* add killed task to list */
kill_task = create_node(killed_task_t);
kill_task->name = strdup(tmp_list->task->name);
kill_task->task_kill_time = tick;
kill_task->task_num = tmp_list->task->task_num;
create_and_add_node_to_list(&killed_tasks, kill_task, killed_task_t);
/* iterate through tasks and remove task */
tmp_task_tasks_list = tasks;
tmp_prev_tasks_list = tasks;
while (NULL != tmp_task_tasks_list)
{
if (0 == strcmp(tmp_task_tasks_list->task->name, tmp_list->task->name))
{
unlock_resources(tmp_task_tasks_list->task);
if (tmp_list->task->t_elapsed > tmp_list->task->global_stats->t_elapsed)
{
tmp_list->task->global_stats->t_elapsed = tmp_list->task->t_elapsed;
}
#ifdef DEBUG
printf("%d handle_tasks_to_delete: %s%d is going to be removed\n", tick, tmp_task_tasks_list->task->name, tmp_task_tasks_list->task->task_num);
#endif
remove_node_from_list(&tasks, tmp_task_tasks_list, &tmp_prev_tasks_list);
}
tmp_prev_tasks_list = tmp_task_tasks_list;
tmp_task_tasks_list = tmp_task_tasks_list->next;
}
}
unlock_resources(tmp_list->task);
if (tmp_list->task->t_elapsed > tmp_list->task->global_stats->t_elapsed)
{
tmp_list->task->global_stats->t_elapsed = tmp_list->task->t_elapsed;
}
#ifdef DEBUG
printf("%d handle_tasks_to_delete: %s%d is going to be removed\n", tick, tmp_list->task->name, tmp_list->task->task_num);
#endif
remove_node_from_list(list, tmp_list, &tmp_prev_list);
}
tmp_prev_list = tmp_list;
tmp_list = tmp_list->next;
}
}
void increment_elapsed(list_node *task)
{
if (NULL != task)
{
task->task->t_elapsed++;
}
}
/**
* @brief handle_reaction_time() iterates through all tasks in ready- and blocked queue and increments their elapsed time.
*/
void handle_reaction_time()
{
iterator(ready_queue, &increment_elapsed);
iterator(blocked_queue, &increment_elapsed);
}
/**
* @brief handle_preemption() checks whether the next task[] the previous task.
* This happens when the remaining execution time of prev is above 0 and
* is not the same as next.*When prev is preempted it must not be blocked.If it is blocked there is no preemption.
* @param next is the actual task.*@param prev is the previous task in the queue.
*/
void handle_preemption(const task_struct *next, task_struct *prev)
{
char buffer[STATUS_STRING_LENGTH];
if ((NULL != next) && (NULL != prev) && (blocked != prev->task_state) && (removed != prev->task_state) &&
(killed != prev->task_state) && (prev != next) &&
(prev->t_remaining_e > 0))
{
snprintf(buffer, STATUS_STRING_LENGTH, "[%s%d preempted]", prev->name, prev->task_num);
add_status(&actions_status[0], buffer, &actions_status_index);
}
}
/**
* @brief sim_scheduler() is the main scheduler function.
*
* It works with a timer tick and thus is called `tick scheduling`.
* Following things happen here each tick:
* - pending_queue is updated via handle_pending_queue(). This retrieves all tasks that are released.
* - ready_queue is updated via handle_ready_queue(). This gets all tasks from pending_queue.
* - all deadlines are checked. Tasks that miss their deadline are marked with a flag.
* - to be executed task is picked via pick_next_task(). The algorithm for picking the task is passed as a pointer via arguments.
* - after the task has been picked preemption is checked in handle_preemption().
* - then the task is "executed" and printed to the output.
*
* After its execution (which is determined by t_stop), sim_scheduler() calculates each maximum reaction time.
*/
void sim_scheduler(void (*prioritize_task)(list_node *, list_node *))
{
task_struct *prev;
task_struct *next;
next = NULL;
prev = NULL;
printf("Time\tJob\t\tReady-Q\t\tBlocked-Q\t\tActions\n");
for (tick = 0; tick <= t_stop; ++tick)
{
handle_pending_queue();
handle_ready_queue();
handle_deadline_in_queue(ready_queue);
handle_deadline_in_queue(blocked_queue);
handle_tasks_to_delete(&ready_queue); /* remove all tasks marked with killed or removed flag */
handle_tasks_to_delete(&blocked_queue); /* remove all tasks marked with killed or removed flag */
pending_queue = NULL; /* reset pending queue */
prev = next;
do
{
prioritize_tasks(blocked_queue, prioritize_task);
prioritize_tasks(ready_queue, prioritize_task);
next = pick_next_task(ready_queue);
lock_resources(next);
} while ((next != NULL) && (next->task_state == blocked));
remove_task_if_finished_execution(next); /* tasks execution */
handle_reaction_time(); /* tasks reactiontime */
if (tick >= t_start)
{
/* Task */
printf("%d\t", tick); /* tick */
if (next != NULL)
{
--next->t_remaining_e;
printf("%s%d", next->name, next->task_num); /* job */
}
else
{
printf("I");
}
/* Ready-Q */
printf("\t\t");
print_list(ready_queue);
/* Blocked-Q */
printf("\t\t");
print_list(blocked_queue);
}
/* Actions */
unlock_resources_if_finished(next);
handle_preemption(next, prev);
add_tasks_from_blocked_queue_to_ready_queue(prioritize_task);
if (tick >= t_start)
{
printf("\t\t\t");
print_status(actions_status, actions_status_index);
if (resource_status_index > 0 && actions_status_index > 0)
{
printf(" + ");
}
print_status(resource_status, resource_status_index);
printf("\n");
}
/* remove tasks from ready_queue and blocked_queue if deadline is exceeded */
resource_status_index = 0;
actions_status_index = 0;
}
printf("-----------------------------------------------\n");
/* print reaction time */
if (stats != NULL) {
printf("Reaction Times: \n");
print_list(stats);
}
/* print all killed tasks from scheduler simulator */
if (killed_tasks != NULL)
{
printf("Killed Jobs: \n");
print_list(killed_tasks);
printf("\n");
}
/* free all lists */
free_all();
}
/**
* @brief Extracts a number from a string. This is useful because strtol returns 0 when failed.
* Then errno must be checked.
* @param token is a reference to the string.
* @return the number if successful or -1 if it failed.
*/
int get_number(const char *token)
{
long number;
char *endptr; /* endpointer for strtol error checking */
errno = 0;