Document limitations Author: Page: Mads Doré Hansen 1/8 Document number: Document class: Creation date: 5/0502-AUC003 UK Lecture Notes 13-022016 REAL-TIME SYSTEMS - EXERCISE 4 SOLUTIONS Abstract This document contains soluations for exercises at the fourth lecture in realtime systems. The content is mostly a collection of notes from previous courses in real-time systems held by Henrik Schiøler at Aalborg University, Denmark. The collection is made on approval from Henrik Schiøler. Content 1 1.1 1.2 1.3 1.4 LECTURE 4 – EXERCISE SOLUTIONS ................................................ 2 QUESTION 1.......................................................................................... 2 QUESTION 2.......................................................................................... 4 QUESTION 3.......................................................................................... 6 QUESTION 4.......................................................................................... 8 Document limitations 1 Author: Page: Mads Doré Hansen 2/8 Document number: Document class: Creation date: 5/0502-AUC003 UK Lecture Notes 13-022016 Lecture 4 – Exercise Solutions Answers to questions three and four are just one possible set of solutions. As with almost all real-time systems the solution to questions depend on who, how and when the system is designed. 1.1 Question 1 Given a periodic taskset : T1 = 4, c1 = 3, T2 = 12, C2 = 2 with relative deadlines d1 = 4, d2 = 7 a. Examine the taskset w.r.t. scehdulability using the exact criterion for DMA scheduling. b. Is the taskset EDF schedulable ? c. Create an EDF schedule for the taskset. add. a. According to DMA task1 has priority over task2 since d1< d2. Task1: Task2: c1 = 3 < d1 = 4 , so task1 meets all its deadlines. Solved graphically below. Thus the taskset is not DMA schedulable. add. b. If di = Ti we need only check if U < 1 but since d2 < T2 we cannot answer the question directly. Document limitations Author: Page: Mads Doré Hansen 3/8 Document number: Document class: Creation date: 5/0502-AUC003 UK Lecture Notes 13-022016 add. c. Since lcm(T1, T2) = 12 we may lay out a cyclic schedule, where the 1. period is scheduled according to EDF Thus the taskset is EDF schedulable. Document limitations 1.2 Author: Page: Mads Doré Hansen 4/8 Document number: Document class: Creation date: 5/0502-AUC003 UK Lecture Notes 13-022016 Question 2 Given a periodic taskset T1 = 4, c1 = 1, d1 = 2, T2 = 6, c2 = 2, d2 = 3, T3 = 8, c3 = 2, d3 = 8, where task1 and task2 share data protected by semaphore S. Accessing the shared data lasts no more than 1 (time units) a. Is the taskset DMA schedulable ? b. If the data above are instead shared between task1 and task3, is the taskset then DMA schedulable. add a. C1 = c1+1 (blocking from task2) = 2 = d1 so task1 meets all deadlines. Task2 is analyzed graphically. as for task3 Thus the taskset is DMA schedulable. Document limitations Author: Page: Mads Doré Hansen 5/8 Document number: Document class: Creation date: 5/0502-AUC003 UK Lecture Notes 13-022016 add. b. Due to priority inversion the completion of task1 may last c1+c2+1 = 1+2+1 = 4 > d1 = 2. To prevent priority inversion we assume that we apply the priority ceiling algorithm. Under the priority ceiling algorithm C1 <= c1+1 = 2 = d1, hence task1 meets its deadlines. Task2 is analyzed graphically Thus task2 may miss its deadline and the taskset is not DMA schedulable even with priority ceiling. Document limitations 1.3 Author: Page: Mads Doré Hansen 6/8 Document number: Document class: Creation date: 5/0502-AUC003 UK Lecture Notes 13-022016 Question 3 Create a SW design for a preemptive EDF scheduler, the design must contain: a. Data type/structures b. Scheduler data/process flows This design is analog to the one of round-robin. typedef struct sTask tTask; struct sTask { unsigned long unsigned long tTaskFunction } RelativeDeadLine; AbsoluteDeadLine; *pTaskFunction; typedef struct sTask tTask; unsigned long CurrentTime = 0; void TimerInterrupt(void) { CurrentTime++; } void TaskReady(tTask Task) { Task.AbsoluteDeadLine = Task.RelativeDeadLine + CurrentTime; if (TaskRunningDeadLine()>Task.AbsoluteDeadLine) { TaskRunningStore(); TaskSetCurrent(Task); } else { QueueAddTask(Task); } } void Scheduler(void) { unsigned char stop = 0; unsigned char newtick = 0; while(!stop) { if (QueueDeadLine()<0xFFFFFFFF) { QueueRunNextTask(); } } } As one can see from the design tasks are switched in the TaskReady function, and not in the interrupt, this due to the fact that all dynamic priorities are dependend on ready time and relative deadline, from which the absolute deadline and thereby also priority can be calculated. If a schedule user initiates a task with TaskReady this might change the task for a while. Document limitations Author: Page: Mads Doré Hansen 7/8 Document number: Document class: Creation date: 5/0502-AUC003 UK Lecture Notes 13-022016 The arbitrary undesigned functions are: TaskRunningDeadLine Gets the absolute dead line of the current running task. 0xFFFFFFFF if no task is running. TaskRunningStore Stores the stack of the current running task as the top element of a ”preemted” stack. TaskSetCurrent Set the current task for specific task execution. QueueAddTask Adds a task to a sorted queue. The task with the lowest absolute deadline is placed as the first element of the queue. QueueDeadLine Gets the deadline of the first task in the sorted queue. 0xFFFFFFFF if no task in queue. QueueRunNextTask Executes the first task in the sorted queue. This by blocking a return from the function until the started task is executed. It is possible to enhance this design to handle periodical tasks and delayed ready time. struct sTask { unsigned long unsigned long unsigned long unsigned long tTaskFunction } RelativeDeadLine; AbsoluteDeadLine; Periode; ReadyTime; *pTaskFunction; unsigned long CurrentTime = 0; void TaskInterrupt(void) { PutPeriodicTasksOnQueue(); CurrentTime++; } Instead of using QueueAddTask to enable a task the user of the scheduler should use another arbitrary function, e.g. AddPeriodicalTask, to add periodical tasks to a simple list of tasks. The new function PutPeriodicTasksOnQueue runs through the list to finde valid tasks and initiates the task by calling TaskReady. A valid tasks is one complying to: (CurrentTime-Task.ReadyTime)%Task.Periode == 0 AddPeriodicalTask also updates Task.ReadyTime to CurrentTime when putting the task into the list of periodical tasks. Document limitations 1.4 Author: Page: Mads Doré Hansen 8/8 Document number: Document class: Creation date: 5/0502-AUC003 UK Lecture Notes 13-022016 Question 4 Create a SW design for a semaphore for mutual exclusion under immediate ceiling to your preemptive fixed priority scheduler, the design must contain: a. Data type/structures b. Semaphore data/process flows One of the preemptive fixed priority schedulers from the previous lectures must be chosen. This solution uses the preemptive round-robin scheduler from lecture 2. The structure below could be used to store ceiling priority values for each resource that can be locked in the system. (e.g. semaphores) struct sSemaphorePriority { unsigned long SemaphoreID; unsigned char MaxPriority; } The only change needed is in TaskRunningPriority where the priority return is not just the static priority of the current running task, but the results of: max(CurrentTask.Priority, MaxLockedPriority()) Where MaxLockedPriority returns the largest sSemaphorePriority.MaxPriority value of semaphores locked by the current running task.