Real-time operating systems course 6 Definitions Non real-time scheduling algorithms Real-time scheduling algorithm
Definitions
Scheduling Scheduling is the activity of selecting which process/thread should be executed next In general operating systems, we can distinguish 3 types of scheduling Long term scheduling before creating a process, decide if it must be activated Medium term scheduling decide if a process has to be swapped out/in Short term scheduling decide which process has to be executed next 3
Scheduling long-term scheduling new long-term scheduling ready/ suspend medium-term scheduling ready short-term scheduling running blocked/ suspend medium-term scheduling blocked 4
Long-term scheduling When a program is created, it goes through an admission test If the program is admitted, the process is created and put into the ready queue If the program is not admitted, it is held back, until the multiprogramming level allows the process to start The key decision is the multiprogramming level if the multiprogramming level is too high, each process executes too slowly, and we risk the thrashing so we can hold back the new processes until the load is decreased 5
Medium-term scheduling It is the part of the OS that decides with processes to swap-in and out it has to do with the multiprogramming level! it has been already discussed in the previous part 6
Short-term scheduling We distinguish Selection function: decides which process is selected from the ready queue, according to some rule Decision mode: when the decision is taken Non-preemptive: once the process goes into running state, no scheduling decision can be taken until it finishes Preemptive: periodically (like in round-robin) when a new process is created when a process in unblocked when an interrupt arrives 7
Scheduling criteria How to evaluate a scheduler? User-oriented criteria response time of processes System-oriented criteria throughput: how much work the system can do in any interval of time Performance is important but not only! in some case we may want some other criteria, predictability (see real-time systems) fairness 8
CPU-Bound and I/O bound CPU-bound processes perform very little I/O operations Most of the time, they execute on the processor For example, the gcc compiler, a scientific computation program,... I/O bound processes perform many I/O operations Most of the time, they are blocked waiting for an I/O device For example, The find and grep commands, the disk de-fragmenter Most processes are a mixture of both 9
Definitions A schedule is said to be feasible if all the tasks are able to complete within a set of constraints. A set of tasks is said to be schedulable if there exists a feasible schedule for it. 10
The general scheduling problem Given a set of n tasks, a set P of m processors, and a set R of r resources, find an assignment of P and R to which produces a feasible schedule. P R Scheduling algorithm feasible 11
Complexity In 1975, Garey and Johnson showed that the general scheduling problem is NP hard. However, polynomial time algorithms can be found under particular conditions. 12
Complexity It is important to find polynomial time algorithms number of tasks = 30 elementary step = 1 s alg. 1: O(log n) 2 alg. 2: O(n) alg. 3: O(n 6 ) alg. 4: O(6 n ) ~6 s 30 s 12 min 7 billion of years 13
Simplifying assumptions Single processor Fully preemptive tasks Simultaneous activations No precedence constraints No resource constraints 14
Algorithm taxonomy preemptive vs. non preemptive off line vs. on line static vs. dynamic best effort vs. optimal 15
Off-line vs. On-line Off-line all scheduling decisions are taken before task activation: the schedule is stored in a table (table-driven scheduling). On-line scheduling decisions are taken at run time on the set of active tasks. 16
static vs. dynamic static scheduling decisions are taken based on fixed parameters, statically assigned to tasks before activation dynamic scheduling decisions are taken based on parameters that can change with time 17
optimal vs. best-effort optimal Always find a feasible schedule, if there exists one. best-effort do their best to find a feasible schedule, if there exists one, but they do not guarantee that. 18
non real-time scheduling algorithms
classical scheduling policies First Come First Served (FCFS) Shortest Job First (SJF) Priority Scheduling Round Robin (RR) not suited for real-time systems 20
First Come First Served (FCFS) It assigns the CPU to tasks based on their arrival times. READY queue CPU non preemptive t dynamic on line best effort 21
First Come First Served (FCFS) (2) very unpredictable response times strongly depend on task arrivals. r 1 r 2 r 3 R 1 = 20 R 2 = 26 R 3 = 26 1 2 3 0 4 8 12 16 20 24 28 32 r 3 r 2 r 1 R 1 = 26 R 2 = 8 R 3 = 2 3 2 1 0 4 8 12 16 20 24 28 32 t t 22
Shortest Job First (SJF) it selects the task with the shortest computation time. non preemptive or preemptive static (C i is a constant parameter) it can be used on line or off-line it minimizes the average response time 23
SJF optimality SJF L S r 0 S L f S < f L f L = f S t f S + f L f S + f L n R '= 1 n i =1 f ' i r i 1 n i=1 n f i r i =R 24 24
SJF optimality (2) *... RR ' R ' ' R * = SJF R SJF is the minimum average response time achievable by any algorithm 25 25
SJF suited for Real-Time? it is not optimal in the sense of feasibility A SJF feasible d 1 d 2 d 3 1 2 0 4 8 12 16 20 24 28 32 3 t SJF not feasible d 1 d 2 d 3 3 2 1 0 4 8 12 16 20 24 28 32 t 26
priority scheduling each task is assigned a priority example, pi [0, 255] the task with the highest priority is selected for execution. tasks with the same priority are served FCFS or RR. can be used for real-time purposes if priorities are assigned following specific rules the POSIX scheduler is a priority scheduler with 32 priorities static or dynamic 27
priority scheduling problem: starvation low priority tasks may experience long delays due to the preemption of high priority tasks. a solution: aging priority increases with waiting time NOTE: p i 1/C i SJF p i 1/r i FCFS 28
round robin the ready queue is served as FCFS, but... the system has a concept of time quantum (Q). each task i cannot execute more than Q time units when Q expires, i is put back in the queue. READY queue CPU Q expired 29
round robin (2) n = number of tasks in the system nq nq t Q time sharing R i nq C i Q =nc i each task runs as it was executing alone on a virtual processor n times slower than the real one. 30
round robin (3) if Q > max (Ci) then RR FCFS if Q context switch time (δ) then n(q + δ) n(q + δ) t Q + δ R i nq C i i Q =nc Q Q 31
priority - based RQ0 running admit RQ1 RQ2 preemption RQn 32
multiple-feedback queues this scheduler consists of N queues, RQ 0,..., RQ N each queue is ordered in FIFO order a fixed time quantum Q (like in Round Robin) rules 1. the scheduler chooses the first process in the highest priority queue and sets the timer equal to Q. Let RQ k be the highest priority queue that has some task ready to execute 2. if the process completes or blocks before the timer expires, select the next process (go to 1) 3. if the process has not yet completed by Q, move the process to the next queue RQ (k+1) 4. when a process is activated or it is unblocked, it goes in the highest priority queue RQ 0 5. periodically, processes can be moved up if they were not able to execute enough (to avoid starvation) 33
multiple-feedback queues (2) this scheduling policy tries to overcome the penalty for I/O bound processes a process always starts from RQ 0 a short process, will probably complete very soon an I/O-bound process is always executed with very short delay a long CPU-bound eventually goes in the last queue RQ N if the system is heavily loaded, periodically the CPU-bound processes in the last queue are promoted to higher priority queues 34
real-time scheduling algorithms
Real-Time algorithms tasks can be scheduled by relative deadlines D i (static) absolute deadlines d i (dynamic) D i d i 36
Earliest Due Date it selects the task with the earliest relative deadline [Jackson 55]. all tasks arrive simultaneously fixed priority (D i is known in advance) preemption is not an issue it minimizes the maximum lateness (L max ) 37
lateness L i = f i - d i L i > 0 r i d i L i < 0 f i r i f i d i 38
maximum lateness L max = max i (L i ) if (L max < 0) then no task misses its deadline 39
EDD Optimality EDD B A A r 0 d a d b B t f a < f a f b = f a L max = L a = f a - d a L a = f a - d a < f a - d a L b = f b - d b = f a - d b < f a - d a L max < L max 40 40
EDD Optimality *... L max L max ' L max ' 'L max * = EDD L max EDD is the minimum value achievable by any algorithm 41
EDD - guarantee test (off line) 1 2 3 4 t f 1 f 2 f 3 f 4 a task set is feasible if i f i d i i f = i k=1 i C k i k =1 C k D i 42
Earliest Deadline First (EDF) it selects the task with the earliest absolute deadline [Horn 74] tasks may arrive at any time dynamic priority (d i depends on arrival) fully preemptive tasks it minimizes the maximum lateness (L max ) 43
EDF example 44
EDF Guarantee test (on line) c 1 (t) c 2 (t) c 3 (t) c 4 (t) t i i k =1 c k td i t 45
complexity issues EDD EDF O(n log n) to order the task set O(n) to guarantee the whole task set O(n) to insert a new task in the queue O(n) to guarantee a new task 46