Real-Time Scheduling Resource Access Control [Some parts of this lecture are based on a real-time systems course of Colin Perkins http://csperkins.org/teaching/rtes/index.html] 1 Current Assumptions Single processor Individual jobs (that possibly belong to periodic/aperiodic/sporadic tasks) Jobs can be preempted at any time and never suspend themselves Jobs are scheduled using a priority-driven algorithm i.e., jobs are assigned priorities, scheduler executes jobs according to these priorities n resources R1, . . . , Rn of distinct types used in non-preemptable and mutually exclusive manner; serially reusable 2 Motivation & Notation Resources may represent: Hardware devices such as sensors and actuators Disk or memory capacity, buffer space Software resources: locks, queues, mutexes etc. Assume a lock-based concurrency control mechanism A job wanting to use a resource Rk executes L(Rk ) to lock the resource Rk When the job is finished with the resource Rk , unlocks this resource by executing U(Rk ) If lock request fails, the requesting job is blocked and has to wait, when the requested resource becomes available, it is unblocked In particular, a job holding a lock cannot be preempted by a higher priority job needing that lock The segment of a job that begins at a lock and ends at a matching unlock is a critical section (CS) CS must be properly nested if a job needs multiple resources 3 Example At 0, J3 is ready and executes At 1, J3 executes L(R) and is granted R J2 is released at 2, preempts J3 and begins to execute At 4, J2 executes L(R), becomes blocked, J3 executes At 6, J1 becomes ready, preempts J3 and begins to execute At 8, J1 executes L(R), becomes blocked, and J3 executes 4 Example At 9, J3 executes U(R) and both J1 and J2 are unblocked. J1 has higher priority than J2 and executes At 11, J1 executes U(R) and continues executing At 12, J1 completes, J2 has higher priority than J3 and has the resource R, thus executes At 16, J2 executes U(R) and continues executing At 17, J2 completes, J3 executes until completion at 18 4 Priority Inversion Priority inversion occurs when a low-priority job executes while some ready higher-priority job waits Contention for resources can cause priority inversions to occur even if the jobs are preemptable, since a lower-priority job holding a lock on a resource will block a higher-priority job Uncontrolled priority inversion: High priority job (J1) can be blocked by low priority job (J3) for unknown amount of time depending on middle priority jobs (J2) 5 Deadlock Deadlock can result from piecemeal acquisition of resources: classic example of two jobs J1 and J2 both needing both resources R and R J2 locks R and J1 locks R J1 tries to get R and is blocked J2 tries to get R and is blocked The classic solution is to impose a fixed locking order over the set of lockable resources, and all jobs attempt to lock the resources in that order 6 Controlling Timing Anomalies Contention for resources causes timing anomalies due to priority inversion and deadlock Several protocols exist to control the anomalies Non-preemptive CS Priority inheritance protocol Priority ceiling protocol .... 7 Non-preemptive Critical Sections The protocol: when a job locks a resource, it is scheduled with highest priority in a non-preemptive manner Example 1 Jobs J1, J2, J3 with release times 2, 5, 0, resp., and with execution times 4, 5, 7, resp. Disadvantage: every job can be blocked by every lower-priority job with a critical section, even if there is no resource conflict 8 Priority-Inheritance Protocol Idea: adjust the scheduling priorities of jobs during resource access, to reduce the duration of timing anomalies Features: Works with any preemptive, priority-driven scheduling algorithm Does not require any prior knowledge of the jobs resource requirements Does not prevent deadlock, but if some other mechanism is used to prevent deadlock, ensures that no job can block indefinitely due to uncontrolled priority inversion 9 Priority-Inheritance Protocol Notation: assigned priority = priority assigned to a job according to a standard scheduling algorithm At any time t, each ready job Jk is scheduled and executes at its current priority πk (t) which may differ from its assigned priority and may vary with time The current priority πk (t) of a job Jk may be raised to the higher priority πh(t) of another job Jh In such a situation, the lower-priority job Jk is said to inherit the priority of the higher-priority job Jh, and Jk executes at its inherited priority πh(t) 10 Priority-Inheritance Protocol Scheduling rules: Jobs are scheduled in a preemptable priority-driven manner according to their current priorities At release time, the current priority of a job is equal to its assigned priority The current priority remains equal to the assigned priority, except when the priority-inheritance rule is invoked Priority-inheritance rule: When job Jh becomes blocked on a resource R, the job Jk which blocks Jh inherits the current priority πh(t) of Jh; all priorities are updated transitively Jk executes at its inherited priority until it releases R; at that time, the priority of Jk returns to its priority πk (t ) at the time t when it acquired the resource R (note that πk (t ) may still be an inherited priority) Resource allocation: when a job J requests a resource R at t: If R is free, R is allocated to J until J releases it If R is not free, the request is denied and J is blocked J is only denied R if the resource is held by another job 11 Priority-Inheritance Example At 0, J5 starts executing at priority 5, at 1 it executes L(Black) At 2, J4 preempts J5 and executes At 3, J4 executes L(Shaded), J4 continues to execute At 4, J3 preempts J4; at 5, J2 preempts J3 At 6, J2 executes L(Black) and is blocked by J5. Thus J5 inherits the priority 2 of J2 and executes 12 Priority-Inheritance Example At 8, J1 executes L(Shaded) and is blocked by J4. Thus J4 inherits the priority 1 of J1 and executes At 9, J4 executes L(Black) and is blocked by J5. Thus J5 inherits the current priority 1 of J4 and executes At 11, J5 executes U(Black), its priority returns to 5 (the priority before locking Black). Now J4 has the highest priority (1) and executes 12 Priority-Inheritance Example At 13, J4 executes U(Shaded), its priority returns to 4. J1 has now the highest priority and executes At 15, J1 completes, J2 is granted Black and has the highest priority and executes At 17, J2 completes, afterwards J3, J4, J5 complete. 12 Priority-Inheritance – Blocking Time z ,k = the k-th critical section of J A job Jh is blocked by z ,k if Jh has higher priority than J but has to wait for J to exit z ,k in order to continue β∗ h, = the set of all maximal critical sections z ,k that may block Jh we assume that CS are properly nested, maximal CS which may block Jh is the one which is not contained within any other CS which may block Jh Theorem 2 Let Jh be a job and let Jh+1, . . . , Jh+m be jobs with lower priority than Jh. Then Jh can be blocked for at most the duration of one critical section in each of β∗ h, where ∈ {h + 1, . . . , h + m}. Proof. Lemma 3 Jh can be blocked by J only if J is executing within a critical section z ,k of β∗ h, when Jh is released It follows that Jh can be blocked by a lower priority job J for at most duration of one critical section of β∗ h, 13 Priority-Inheritance – The Worst Case 14 Properties of Priority-Inheritance Protocol Simple to implement, does not require prior knowledge of resource requirements Jobs exhibit two types of blocking Direct blocking due to resource locks i.e., a job J locks a resource R, Jh executes L(R) is directly blocked by J on R Priority-inheritance blocking i.e., a job Jh is preempted by a lower-priority job that inherited a higher priority Jobs may exhibit transitive blocking Deadlock is not prevented Can reduce blocking time compared to non-preemptable CS but does not guarantee to minimize blocking 15 Priority-Ceiling Protocol The goal: to furhter reduce blocking times due to resource contention in its basic form priority-ceiling protocol works under the assumption that the priorities of jobs and resources required by all jobs are known apriori can be extended to dynamic priority (job-level fixed priority), see later Notation: The priority ceiling of any resource Rk is the highest priority of all the jobs that require Rk and is denoted by Π(Rk ) At any time t, the current priority ceiling Π(t) of the system is equal to the highest priority ceiling of the resources that are in use at the time If all resources are free, Π(t) is equal to Ω, a newly introduced priority level that is lower than the lowest priority level of all jobs 16 Priority-Ceiling Protocol The scheduling and priority-inheritance rules are the same as for priority-inheritance protocol Scheduling rules: Jobs are scheduled in a preemptable priority-driven manner according to their current priorities At release time, the current priority of a job is equal to its assigned priority The current priority remains equal to the assigned priority, except when the priority-inheritance rule is invoked Priority-inheritance rule: When job Jh becomes blocked on a resource R, the job Jk which blocks Jh inherits the current priority πh(t) of Jh; also priorities are inherited transitively Jk executes at its inherited priority until it releases R; at that time, the priority of Jk returns to its priority πk (t ) at the time t when it acquired the resource R (note that πk (t ) may still be an inherited priority) 17 Priority-Ceiling Protocol Resource allocation rule: When a job J requests a resource R held by another job, the request fails and the requesting job blocks When a job J requests a resource R at time t, and that resource is free: If J’s priority π(t) is higher than current priority ceiling Π(t), R is allocated to J If J’s priority π(t) is not higher than Π(t), R is allocated to J only if J is the job holding the resource(s) whose priority ceiling is equal to Π(t), otherwise J is blocked (Note that only one job may hold the resources whose priority ceiling is equal to Π(t)) Note that unlike priority-inheritance protocol, the priority-ceiling protocol can deny access to an available resource 18 Priority-Ceiling Protocol At 1, Π(t) = Ω, J5 executes L(Black), continues executing At 3, Π(t) = 2, J4 executes L(Shaded); because the ceiling of the system Π(t) is higher than the current priority of J4, job J4 is blocked, J5 inherits J4’s priority and executes at priority 4 At 4, J3 preempts J5; at 5, J2 preempts J3. At 6, J2 requests Black and is directly blocked by J5. Consequently, J5 inherits priority 2 and executes until preempted by J1 19 Priority-Ceiling Protocol At 8, J1 executes L(Shaded), its priority is higher than Π(t) = 2, its request is granted and J1 executes; at 9, J1 executes U(Shaded) and at 10 completes At 11, J5 releases Black and its priority drops to 5; J2 becomes unblocked, is allocated Black and executes 19 Priority-Ceiling Protocol At 14, J2 and J3 complete, J4 is granted Shaded (because its priority is higher than Π(t) = Ω) and executes At 16, J4 executes L(Black) which is free, the priority of J4 is not higher than Π(16) = 1 but J4 is the job holding the resource whose priority ceiling is equal to Π(16). Thus J4 gets Black, continues to execute; the rest is clear 19 Priority-Ceiling Protocol Theorem 4 Assume a system of preemptable jobs with fixed assigned priorities. Then deadlock may never occur, a job can be blocked for at most the duration of one critical section. 20 21 Differences between the priority-inheritance and priority-ceiling Priority-inheritance is greedy, while priority ceiling is not The priority-ceiling protocol may withhold access to a free resource, i.e., a job can be blocked by a lower-priority job which does not hold the requested resource – avoidance blocking The priority ceiling protocol forces a fixed order onto resource accesses thus eliminating deadlock 22 Resources in Dynamic Priority Systems The priority ceiling protocol assumes fixed and known priorities In a dynamic priority system, the priorities of the periodic tasks change over time, while the set of resources is required by each task remains constant As a consequence, the priority ceiling of each reasource changes over time What happens if T1 uses resource X, but T2 does not? Priority ceiling of X is 1 for 0 ≤ t ≤ 4, becomes 2 for 4 ≤ t ≤ 5, etc. even though the set of resources is required by the tasks remains unchanged 23 Resources in Dynamic Priority Systems If a system is job-level fixed priority, but task-level dynamic priority, a priority ceiling protocol can still be applied Each job in a task has a fixed priority once it is scheduled, but may be scheduled at different priority to other jobs in the task (e.g. EDF) Update the priority ceilings of all jobs each time a new job is introduced; use until updated on next job release Has been proven to prevent deadlocks and no job is ever blocked for longer than the length of one critical section But: very inefficient, since priority ceilings updated frequently May be better to use priority inheritance, accept longer blocking 24 Schedulability Tests with Resources How to adjust schedulability tests? Add the blocking times to execution times of jobs; then run the test as normal The blocking time bi of a job Ji can be determined for all three protocols: non-preemptable CS ⇒ bi is bounded by the maximum length of a critical section in lower priority jobs priority-inheritance ⇒ bi is bounded by the total length of the m longest critical sections where m is the number of jobs that may block Ji (For a more precise formulation see Theorem 2.) priority-ceiling ⇒ bi is bounded by the maximum length of a critical section 25