scheduling algorithms
DESCRIPTION
Scheduling algorithms for embedded systemTRANSCRIPT
Scheduling Algorithms
1
Julian IlhamDept. of Electronic Engineering
Pukyong National UniversityEmail: [email protected]
Nov 2nd 2012
2
Content
Introduction Preemptive vs Non-Preemptive
First Come First Served (FCFS) Shortest Job First (SJF) Priority Based Scheduling Round Robin (RR)
3
Introduction
Choosing which task to run and for how long
What is scheduling?
- Improve the system performance- Support multitasking
Why do we need it?
4
Preemptive vs Non-Preemptive
Running task can be forced to release even though it is neither completed
Preemptive
Each running task keeps processing until it completed
Non - Preemptive
5
First Come First Served (FCFS)
Only available in Non-Preemptive mode Example:
Gantt chart
Tasks Arrival Times (ms) Burst Time (ms)
P2 0.0 12
P3 3.0 8
P4 5.0 4
P1 10.0 10
P5 12.0 6
P2 P3 P4 P1 P5
012
20 24 34 40 (ms)3 5 10
6
First Come First Served (FCFS)
Time CalculationP1 : 24 – 10 = 12 msP2 : 0 msP3 : 12 – 3 = 9 msP4 : 20 – 5 = 15 msP5 : 34 – 12 = 22 ms
Total waiting time : P1 + P2 + P3 + P4 + P5 = 58 msAverage waiting time : 58 ms / 5 = 11.6 ms
7
First Come First Served (FCFS)
Advantage: Very simple code and structure
Disadvantage: Large average waiting time
8
Shortest Job First (SJF)
Available in Preemptive and Non-Preemptive mode Example (Preemptive)
Gantt chart
Tasks Arrival Times (ms) Burst Time (ms)
P2 0.0 12
P3 3.0 8
P4 5.0 4
P1 10.0 10
P5 12.0 6
P2 P3 P4 P1P5
30
59 15
P3
21
P2
30 4010 12
(ms)
9
Shortest Job First (SJF)
Time Calculation (Preemptive)P1 : 30 – 10 = 20 msP2 : 0 + (21 – 3) = 18 msP3 : (3 – 3) + (9 – 5) = 4 msP4 : 5 – 5 = 0 msP5 : 15 – 12 = 3 ms
Total waiting time : P1 + P2 + P3 + P4 + P5 = 45 msAverage waiting time : 45 ms / 5 = 9 ms
10
Shortest Job First (SJF)
Example (Non-Preemptive)
Gantt chart
Tasks Arrival Times (ms) Burst Time (ms)
P2 0.0 12
P3 3.0 8
P4 5.0 4
P1 10.0 10
P5 12.0 6
P2 P4 P1P5
30
516
P3
22 30 4010 12
(ms)
11
Shortest Job First (SJF)
Time Calculation (Non-Preemptive)P1 : 30 – 10 = 20 msP2 : 0 msP3 : 22 – 3 = 19 msP4 : 12 – 5 = 7 msP5 : 16 – 12 = 4 ms
Total waiting time : P1 + P2 + P3 + P4 + P5 = 50 msAverage waiting time : 50 ms / 5 = 10 ms
12
Shortest Job First (SJF)
Advantage: Minimum average waiting time for a given set of process
Disadvantage: Difficult to estimate burst time, starvation (indefinite blocking)
13
Priority Based Scheduling
Available in Preemptive and Non-Preemptive mode Example (Preemptive)
Gantt chart
Tasks Arrival Times (ms) Burst Time (ms) Priority
P2 0.0 12 2
P3 3.0 8 5
P4 5.0 4 1
P1 10.0 10 4
P5 12.0 6 3
P2 P4 P1P5
30
516
P3
22 32 4010 12
9
P2
(ms)
14
Priority Based Scheduling
Time Calculation (Preemptive)P1 : 22 – 10 = 12 msP2 : 9 – 5 = 4 msP3 : 32 – 3 = 29 msP4 : 5 – 5 = 0 msP5 : 16 – 12 = 4 ms
Total waiting time : P1 + P2 + P3 + P4 + P5 = 49 msAverage waiting time : 49 ms / 5 = 9.8 ms
15
Priority Based Scheduling
Example (Non-Preemptive)
Gantt chart
Tasks Arrival Times (ms) Burst Time (ms) Priority
P2 0.0 12 2
P3 3.0 8 5
P4 5.0 4 1
P1 10.0 10 4
P5 12.0 6 3
P2 P4 P1P5
3 516
P3
22 32 4010 12
(ms)
16
Priority Based Scheduling
Time Calculation (Non-Preemptive)P1 : 22 – 10 = 12 msP2 : 0 msP3 : 32 – 3 = 29 msP4 : 12 – 5 = 7 msP5 : 16 – 12 = 4 ms
Total waiting time : P1 + P2 + P3 + P4 + P5 = 52 msAverage waiting time : 52 ms / 5 = 10.4 ms
17
Priority Based Scheduling
Advantage: Easier than SJF
Disadvantage: Can cause indefinite blocking
18
Round Robin (RR)
Only available in Non-Preemptive mode FCFS is used in sequencing policy Need to determine time slice/ quantum as a unit of time for
each process Then continue to the next process after reaching maximum of
quantum
- Too small time quantum -> too many tasks switched- Too large time quantum -> inherits the problem of FCFS
19
Round Robin (RR)
Example (assuming quantum is 5 ms):
Gantt chart
Tasks Arrival Times (ms) Burst Time (ms)
P2 0.0 12
P3 3.0 8
P4 5.0 4
P1 10.0 10
P5 12.0 6
P2
53
24 29
10 12
P3 P414
P1 P519
P5P2 P3 P1 P20 105 32 37 38 40
20
Round Robin (RR)
Time CalculationP1 : (14 – 10) + (32 – 19) = 17 msP2 : 0 + (24 – 5) + (38 – 29) = 28 msP3 : (5 – 3) + (29 – 10) = 21 msP4 : 10 – 5 = 5 msP5 : (19 – 12) + (37 – 24) = 20 ms
Total waiting time : P1 + P2 + P3 + P4 + P5 = 91 msAverage waiting time : 91 ms / 5 = 18.2 ms
21
Round Robin (RR)
Advantage: Guarantee fairness (bounded waiting time and no starvation)
Disadvantage: Average waiting time is long
22
End
Q n A