Real-Time Scheduling CS4730 Fall 2010 Dr. José M. Garrido Department of Computer Science and Information Systems Kennesaw State University
Jan 01, 2016
Real-Time Scheduling
CS4730 Fall 2010Dr. José M. Garrido
Department of Computer Science and Information Systems
Kennesaw State University
CPU Scheduling
Overview of basic non-real time Scheduling techniques
The most relevant RT scheduling techniques
General Processor Scheduling
Allocation of tasks to processor for execution, in some order
A set of performance metrics is usually derived
3
CPU Scheduling
The scheduler selects the next process to execute from among several processes waiting in the ready queue.
The dispatcher allocates the CPU to the selected process at the appropriate time.
Scheduling (General)
Issues in scheduling Basic scheduling algorithms
First-come First-served Round Robin Shortest Process Next Priority based
Real-time scheduling
5
6
Scheduling Issues
Application Profile: A program alternates between CPU usage and
I/O Relevant question for scheduling: is a program
compute-bound (mostly CPU usage) or I/O-bound (mostly I/O wait)
Multi-level scheduling (e.g., 2-level in Unix) Swapper decides which processes should
reside in memory Scheduler decides which ready process gets
the CPU next
Scheduling Issues
When to context-switch When a process is created When a process terminates When a process issues a blocking call (I/O,
semaphores) On a clock interrupt On I/O interrupt (e.g., disk transfer finished,
mouse click) System calls for IPC (e.g., up on semaphore,
signal, etc.)
7
8
Scheduling Issues
Is preemption allowed? Nonpreemptive scheduler does not use clock interrupts to stop a
process What performance metrics should be optimized?
CPU utilization: Fraction of time CPU is in use Throughput: Average number of jobs completed per time unit Turnaround Time: Average time between job submission and
completion Waiting Time: Average amount of time a process is ready but waiting Response Time: in interactive systems, time until the system responds
to a command Response Ratio: (Turnaround Time)/(Execution Time) -- long jobs
should wait longer
9
Scheduling Issues
Different applications require different optimization criteria Batch systems (throughput, turnaround time) Interactive system (response time, fairness, user
expectation) Real-time systems (meeting deadlines)
Overhead of scheduling Context switching is expensive (minimize context switches) Data structures and book-keeping used by scheduler
What’s being scheduled? Processes and Threads
Process Activities
Every process that request CPU service, carries out the following sequence of actions:
1. Join the ready queue and wait for CPU service. 2. Execute (receive CPU service) for the duration of the
current CPU burst or for the duration of the time slice (timeout).
3. Join the I/O queue to wait for I/O service or return to the ready queue to wait for more CPU service.
4. Terminate and exit if service is completed, i.e., there are no more CPU or I/O bursts. If more service is required, return to the ready queue to wait for more CPU service.
Scheduler
Insertion of processes that request CPU service into the ready queue. This function is carried by the enqueuer, a component of the scheduler.
The occurrence of a context switch, carried by the context switcher that saves the context of the current process and de-allocates the CPU from that process.
The selection of the next process from the ready queue and loading its context. This can be carried out by the dispatcher, which then allocates the CPU to the newly selected process.
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler context switching switching to user mode jumping to the proper location in the user program to
restart (resume) that program Dispatch latency – the time that elapses from
the stopping one process to the starting of the newly selected process
14
RT SCHEDULING
In real-time systems, the purpose of scheduling is to determine whether or not a set of computations are feasible in time
A set of computations are feasible or schedulable on one or more processors if there exist: sufficient processor cycles, and some execution orderingto execute all the computations
Schedule
A specific time allocation of tasks to the processors is called a schedule
If the scheduling algorithm can allocate the tasks so that all constraints are met, then the resulting schedule is said to be feasible
15
Cyclic Executives
General traditional algorithmic and software methods for organizing real-time processes
Are designed mainly for periodic processes The principal goal is to allocate one or more
processors to a set of processes in such a manner that they meet their deadlines
A feasible execution schedule is constructed by interleaving process executions
Fall 2010 (C) J M Garrido 16
Organizing Proceeses for CE
A periodic process consists of a sequence of computations, each called: slice, action, scheduling block
Each computation has a worst case computing time interval and is scheduled as an atomic unit by the CE
A major schedule is an allocation of these process computations for all processes, such that all deadlines are met
Fall 2010 (C) J M Garrido 17
Foreground/Background Computing
The periodic real-time processes are allocated to the foreground
The non-real-time, soft real-time, and non-critical processes are allocated to the background
The processes in the background can be preempted by those in the foreground
Fall 2010 (C) J M Garrido 18
20
Periodic Tasks
Typical real-time application has many tasks that need to be executed periodically Reading sensor data Computation Sending data to actuators Communication
Standard formulation: Given a set of tasks T1, … Tn. Each task Ti has period Pi and computation time Ci
Schedulability Problem
Can all the tasks be scheduled so that every task Ti gets the CPU for Ci units in every interval of length Pi ?
22
Periodic Tasks Example
Example: Task T1 with period 10 and CPU time 3 Task T2 with period 10 and CPU time 1 Task T3 with period 15 and CPU time 8
Possible schedule: repeats every 30 sec T1 from 0 to 3, 12 to 15, 24 to 27 T2 from 3 to 4, 15 to 16, 27 to 28 T3 from 4 to 12, 16 to 24 If T2 has period 5 (instead of 10) then there is no feasible schedule
Simple test: Task Ti needs to use CPU for Ci/Pi fraction per unit Utilization = Sum of Ci/Pi
Task set is schedulable if and only if utilization is 1 or less.
Issues in RT Scheduling Example1
23
Two processes: P1 (1,2,2) and P2 (2, 5, 5)prio (P1) > prio (P2)No preemption
Schedule can be repeated at time t = 10
If the priorities are reversed, prio (P2) > prio(p1)
P2 starts at t = 0 P2 will compute for 2 time units, which is the
first cycle of P1 P1, will therefore, miss its deadline
RR Scheduling Example 2
26
Two processes: P1 (3, 4, 4) and P2 (2, 8, 8) using RR scheduling with one unit time intervals. At time t = 4, P1 has executed only 2 time units
RR Scheduling Example 2
27
Two processes: P1 (3, 4, 4) and P2 (2, 8, 8) using RR scheduling with one unit time intervals. P1 has higher priority than P2.P2 is preempted by P1 at t = 4
Rate Monotonic Scheduling
Based on fixed priorities The shorter the period of the task, the higher
the priority assigned Example:
P1 = (3, 4, 4)
P2 = (2, 8, 8)
29
Least Compute Time (LCT)
Also based on fixed priorities The shorter the compute time of the task, the
higher the priority assigned Example:
P1 = (3, 4, 4)
P2 = (2, 8, 8)
P2 will have higher priority. P1 will missed its first deadline
31
RT Scheduling with Dynamic Priorities
Shortest Completion Time (SCT) Earliest Deadline First (EDF) Least Slack Time (LST)
32
33
Earliest Deadline First Scheduling: EDF
Based on dynamic priorities. EDF scheme: Choose the task with the earliest
(current) deadline Preemptive: scheduling decision made when a
task finishes as well as when a new task arrives Theorem: If there is a possible schedule, then
EDF will find one Example:
Two processes: P1 (2, 4, 4) and P2 (5, 10, 10)
35
Task T1 with period 10 and CPU time 3 Task T2 with period 10 and CPU time 1 Task T3 with period 15 and CPU time 8
0 3 4 10 12 1516 20 2324 28
EDF Example 2
T1 T2 T3 T3 T1 T2 T3 T1 T2 T3
36
Scheduling Algorithm: RMS
Rate Monotonic Scheduling (Liu and Layland, 1973) Based on static priorities. Preemptive: scheduling decision made when a task finishes
as well as when a new task arrives Scheduling algorithm: Choose the task with smallest period
(among ready tasks) Theorem: If utilization is smaller than 0.7, then RMS is
guaranteed to find one If utilization is between 0.7 to 1, RMS may or may not succeed
Example:
37
The Priority Inversion Problem
T1
T2
T3
failed attempt to lock R lock(R)
unlock(R)
lock(R)
unlock(R)
Priority order: T1 > T2 > T3
T2 is causing a higher priority task T1 wait !
38
Priority Inversion
1. T1 has highest priority, T2 next, and T3 lowest2. T3 comes first, starts executing, and acquires some
resource (say, a lock).3. T1 comes next, interrupts T3 as T1 has higher priority4. But T1 needs the resource locked by T3, so T1 gets blocked5. T3 resumes execution6. T2 arrives, and interrupts T3 as T2 has higher priority than
T3, and T2 executes till completion7. In effect, even though T1 has priority than T2, and arrived
earlier than T2, T2 delayed execution of T18. This is “priority inversion” !! Not acceptable.
9. Solution T3 should inherit T1’s priority at step 5