Real Time Task Embedded System

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

1

Real-time Task
Definition
2

 Process
 is a sequence of instructions in the absence of other

activities
 is continuously executed by the processor until completion.

Arrival time Task τi

Start time

ai si fi t
Finishing time
Definition
3

 Real time tasks are generated due

to certain event occurrences


• Either internal or external
 Example:
• A task may get generated due to a temperature
sensor sensing high level or to the reverse.
• Or due to timing aspects.
Task States
4

 A task is said to be:


• ACTIVE: if it is under/ready for execution by
the CPU.
• BLOCKED: if it is waiting for one event to
happen/ to be done.
 An active task can be:
• RUNNING: if it is being executed by the CPU.
• READY: if it is waiting for the CPU.
Task State Transitions
5

signal BLOCKED wait

dispatching Termination
activation
READY RUNNING

Preemption
Ready Queue
6

The ready tasks are kept in a waiting

queue, called ready queue


The strategy for choosing the ready

task to be executed on the CPU is


based on a scheduling algorithm.
activation Dispatching Termination
τ3 τ2 τ1 CPU
Ready queue
Task Criticality
7

Based on task criticality it can either be


 hard or soft real time tasks

HARD real time tasks


• Missing a deadline is highly undesirable
• May cause catastrophic effects on the
system if deadline is missed
Cont…d
8

 SOFT real time tasks


• Occasional miss of a deadline is tolerable
• Causes a performance degradation

An operating system that can handle hard RT tasks


is called a hard real-time system
Examples
9

 HARD real time tasks


• Flight control system
• Car braking system
 SOFT real time tasks
• Reading data from the keypad
• Playing video
Real-time(RT) tasks
10

Based on their arrival RT tasks can be

classified as:
 Periodic
• Periodic tasks repeats itself after a certain fixed time
interval
 Sporadic
• Sporadic tasks recurs at random instant
 Aperiodic
• The same as sporadic except that the minimum
separation b/n two instants can be 0.
Activation modes of RT tasks
11

 RT tasks may be activated due to:


 Clock driven: is used for periodic tasks.
• A task will automatically be activated by the kernel at
regular intervals.
 Event driven: used for aperiodic and/or sporadic
tasks.
• A task will be activated upon the arrival of an event or
through an explicit invocation of the activation
primitive.
12

Scheduling Polices
Scheduling Polices
13

Are sets of rules /policies which will be used


to govern the way tasks are scheduled and
dispatched to the controller.
When do you think a schedule can be done?
 How?
During scheduling points.
• Using scheduling algorithms/policies.
Scheduling Point
14

 At these points on timeline


• A scheduler makes decisions regarding to the task to be
run next.
• We have two types of scheduling points. These are:

 Clock-driven
• Scheduling points defined by interrupts for a periodic timer.

 Event-driven
• Scheduling points defined by interrupts on task
completion & generation of events.
Task Scheduling On Uni-processor
15

• Real-time task scheduler can be activated by

either of the following:

#1 #2

Clock- Even
Driven Driven
Clock-driven scheduling
16

 Decision regarding which job to run

next is made only at the clock interrupt


instances.
• Timers are used to determine the scheduling
points
• Job lists as well as which task to be run for how
long will be stored in a table
Cont…d
17

 Also called offline/static scheduler

• Can’t schedule aperiodic/sporadic tasks


 Popular Examples
• Table-driven scheduler
• Cyclic scheduler
Table-driven scheduler
18

Task Start Time Stop


T1 0 100
T2 101 150
T3 151 225
Cont…d
19

 For scheduling n periodic tasks


• The scheduler develops a permanent schedule for
a period LCM (P1, P2, . . . Pn)
• Store the schedule in a table
• The schedule will be repeated forever.
Cont…d
20

 Table driven schedulers are


• Simple: Used in low cost applications
• Efficient: Very little runtime overhead
• Inflexible: Very difficult to accommodate dynamic
tasks.
Disadvantages of table driven
21

 When the number of tasks are large


• Requires setting a large number of timers
• Number of timers supported by an operating
system is restricted due to efficiency reasons.
Cyclic Schedulers
22

 Cyclic schedulers are :


• Very popular
• Being extensively used in the industry
A majority of small embedded applications
being manufactured uses cyclic scheduler.
Event-Driven Schedulers
23

 Unlike clock-driven schedulers:


• These can handle both sporadic & aperiodic tasks
• Used in more complex applications
 Scheduling is very complex and frequent.
Cont…d
24

 Scheduling points are:


• Defined by task completion & event arrival
 Event-driven schedulers are:
 Pre-emptive Schedulers, meaning
• On the arrival of higher priority task, the running task may be
pre-empted.
 Greedy schedulers:
• It will never keep the processor idle if a task is ready.

 Event driven schedulers can be classified as

static or dynamic.
Static Event-Driven Schedulers
25

 The task priorities once assigned by the


programmer
• Will not be changed during runtime.
• RMA (Rate Monotonic Algorithm) is the optimal
static priority scheduling algorithm.
Dynamic Event-Driven Schedulers
26

 The task priorities can be changed during


runtime
• Based on the relative urgency of completion of
tasks
• EDF (Earliest Deadline First) is the optimal uni-
processor scheduling algorithm
Scheduling Algorithms
27

Let us look about some specific


scheduling algorithms.
 Earliest Deadline First(EDF)
 Minimum Laxity First(MLF)
 Rate Monotonic Scheduling(RMS)
Earliest Deadline First (EDF)
28

 Algorithm of EDF
• Each time a new ready task arrives:
• It is inserted in to a queue of ready tasks,
sorted by their deadlines.
• If a newly arrived task is inserted at the head
of the ready queue, a task which is under
execution will be pre-empted.
Cont…d
29

 Scheduling is complex due to:


• Processes must be sorted based on their
deadline at each scheduling instant.
Example
30

Early Deadline First (EDF)


Arrival Duration Deadline
T1 0 10 33
T2 4 3 28
T3 5 10 29

T1
T2
T3

0 2 4 6 8 10 12 14 16 18 20 22 24
Accumulated Utilization
31

n
ei
Accumulated Utilization:  
i 1 pi

Necessary condition for


schedulability (with m = number of
processors) m
Since drawing and checking is very tedious,
accumulated utilization can be used to check the
schedulability of a task set using EDF.
EDF Schedulability Check
32

 Sum of utilization of tasks must be less

than or equal to one


n
ei

i 1 pi
  ui  1

• Both the necessary and sufficient condition


for schedulability
EDF is a dynamic algorithm
33

 The priority of a task can be determined

at any point of time.


 The longer the task waits in a ready

queue – the higher the chance


(probability) of its being taken up for
scheduling.
EDF Properties
34

 EDF is optimal for a uni-processor with

task pre-emption being allowed


• If a feasible schedule exists then EDF will schedule
the tasks
Minimum Laxity First (MLF)
35

 Priorities are decreasing function of the

laxity
 Laxity = Absolute deadline – the time of

scheduling – execution time left for


completion.
Cont…d
36

 The task that is most likely to fail first is

assigned to the highest priority.


Properties of MLF

• Less laxity, higher priority


• Dynamically changing priority
• Pre-emptive
Cont…d
37

• Requires calling the scheduler


periodically, and to re-compute the laxity
overhead for may calls of the scheduler &
many context switches.
• Detects missed deadlines early
• Requires the knowledge of the execution time.
Example
38

Arrival Duration Deadline


T1 0 10 33
T2 4 3 28
T3 5 10 29

T1
T2

T3

0 2 4 6 8 10 12 14 16 18 20 22 24
l(T1)=33-4-6=23 l(T1)=33-5-6=22 l(T1)=33-15-6=12
l(T2)=28-4-3=21 l(T2)=28-5-2=21 l(T2)=28-15-2=11
l(T3)=29-5-10=14
Rate monotonic Scheduling (RMS)
39

 is the most well-known technique for scheduling


independent periodic tasks.
 Assumptions of RMS:

• All tasks having hard deadlines are periodic


• All tasks are independent
• di = pi for all tasks

• ei is constant and known for all tasks


• The time required for context switching is
negligible
Cont…d
40

 Policy of RMS
• The priority of a task is a monotically decreasing
function of its period.
 The higher the frequency (or lower the period) of a

task, the higher is its priority


• At any time, a highest priority task among all those
that are ready for execution will be allocated to
the processor.
Rate monotonic (RM) Scheduling
41

Priority

Frequency
Cases of failing RMA scheduling
42

Task 1: period 5, execution time 2


Task 2: period 7, execution time 4
Missing computation
scheduled in the next
Missed period
deadline

T1

T2

0 2 4 6 8 10 12 14 16 18 20 t
RMA Schedulability(utilization
bound)
43

 Sum of utilization due to tasks is less

than one.
n
ei

i 1 pi
  ui  1

 Necessary condition for schedulability

but not sufficient condition.


RMA Schedulability
44

 Utilization bound II (Liu & Layland

1971)

u i n21n
 1

• ui is the processor utilization due to task Ti.


• n is the number of tasks
Example 1
45

 Check whether the ff task is schedulable

using RMA

T1: e1 = 1, p1 = 4, d1 = 4
T2: e2 = 2, p2 = 6, d2 = 6
T3: e3 = 3, p3 = 20, d3 = 20
Solution
46
3
ei 1 1 3 11
i 1 pi
  ui     1
4 3 20 15

n2  1n
  13

 1  3 2  1  0.778
11
 ui  15  0.733  0.778
Therefore, the task set is schedulable under RMS.
Completion Time Theorem
(Liu & Lehoczky)
47

 Even if a task fails Liu-Layland test, it

may still be RMA schedulable.


 How to check the schedulability of a task

set:
• Consider zero phasing of all tasks if not given.
• Draw up the schedules till the first deadline of
each task
• Observe if each task is schedulable
Completion Time Theorem: Basic
premise
48

 Worst case completion time for a task:


• Occurs when it is in phase with its higher
priority task
Completion Time Theorem: Basic premise
49

T1 = 10, 30, Ф = 0
T1 is in phase with T2 T2 = 60, 120, Ф = 0

T1 T2 T1 T2 T1 T2
10 30 40 60 70 90

T1 = 10, 30, Ф = 20
T1 has 20ms phase lag with T2 T2 = 60, 120, Ф = 0

T2 T1 T2 T1 T2
20 30 50 60 80
Completion Time Theorem
(Liu & Lehoczky)
50

 Drawing the schedule is cumbersome


• When the number of tasks is large.
 A task Ti will meet its first deadline, if

 i 1 
p  
e    i  e   p
 i j 1  p j  j  i
 
where pi is the period of Ti and
p1 ≤ p2 ≤ p3 . . . ≤ pn
Example51

Check whether the ff task is schedulable

using RMA

T1: e1 = 20ms, p1 = 100ms


T2: e2 = 30ms, p2 = 150ms
T3: e3 = 60ms, p3 = 200ms
Solution
52

 Using the formula in the completion time theorem:


For T1: (e1 + ┌ 0 ┐) ≤ p1, the summation part is 0 (zero) due to
upper bounds and also we are working with only one task.
Since, 20 < 100, task 1 is schedulable under RMA.
For T2: (e2 + ┌ p2/p1 ┐*e1) ≤ p2
= (30 + ┌ 150/100 ┐*20) ≤ 150
= (30 + 2*20) ≤ 150
= 70 ≤ 150 its true, hence task 2 is also schedulable under
RMA.
Cont…d
53

For T3: (e3 + ┌ p3/p1 ┐*e1 + ┌ p3/p2 ┐*e2) ≤ p3


= (60 + ┌ 200/100 ┐*20 + ┌ 200/150 ┐*30) ≤ 200
= (60 + 2*20 + 2*30) ≤ 200
= 160 ≤ 200 true.
So, task 3 is also schedulable under RMA
Generally, the given task set is feasibly schedulable
under RMA using the completion time theorem.
Example54

Check whether the ff task is schedulable

using RMA

T1: e1 = 20ms, p1 = 100ms


T2: e2 = 30ms, p2 = 150ms
T3: e3 = 90ms, p3 = 200ms
Overhead due to context
switching 55

 In the worst case, each task incurs at


most two context switches
1) When it runs possibly preempt the currently running
task
2) When it completes
Cont…d
56
 Let the context switching time is
constant & equal to C and ei be
execution time.
 Then, the execution time of each task
will be increased to (ei + 2*C)
 Assume three periodic tasks
Example
 Assume context switching time = 1ms,
57

determine whether the task is feasibly


schedulable.

T1: e1 = 10ms, p1 = d1 = 50ms


T2: e2 = 25ms, p2 = d2 = 150ms
T3: e3 = 50ms, p3 = d3 = 200ms
Solution
Task T1: 12msec < 50msec, schedulable
58
Task T2: 27 + 12*3 = 63 < 150,
schedulable.
Task T3: 52 + 4*12 + 2*27 = 154 < 200,
schedulable
 Hence, it is feasibly schedulable under
RMA.
Self Suspension
59

 When does a task suspend itself from

execution?
 Cases:
• When it performs input/output operations
• When it is waiting for some event to occur
Cont…d
60

 Self-suspension introduces an additional

scheduling point.
• The OS removes the self-suspended task from the
queue and places it in the blocked queue.
• The OS then dispatches the next eligible task.
Cont…d
61

 Scheduling point need to be redefined as:


• Task completion
• Task arrival
• Self-suspension events

 Self-suspension will introduce some delays

which will be significant on scheduling like


context switching.
Cont…d
62

 Revised RMA condition

 i 1 
p  
 e  bt    i   e   p
 i i
 p j 
j
 i
 j 1 
i 1
bti  bi   min e j , b j 
j 1

bti – Delay that task Ti incurs due to its own self-


suspension & that of all higher priority task.
bi – worst case self suspension time of task Ti.
Example 63

Consider the ff set of periodic real time tasks

T1: e1 = 10ms, p1 = 50ms


T2: e2 = 25ms, p2 = 150ms
T3: e3 = 50ms, p3 = 200ms
Self-suspension times: b1 = 3ms, b2 = 3ms, b3 = 5ms

Determine whether the tasks are schedulable


using RMA
Solution
64

 For T1: 10 + 3 + 0 = 13 < 50, schedulable


 For T2: 25 + (3 + 3) + 10 * 3 = 61<150, schedulable

 For T3: 50 + (5 + 6) + 10 * 4 + 25 * 2 = 151 < 200,

schedulable
Hence, it’s feasibly schedulable under RMA.
Interfacing LM35, LCD & Fan with PIC16F877A
65
// Lcd pinout settings trisa.b1=1;//for temperature sensor
sbit LCD_RS at Rc5_bit; trisd.b1=0;//for Fan
sbit LCD_EN at Rc4_bit; while(1) {
sbit LCD_D7 at Rc3_bit; x=adc_read(1);
sbit LCD_D6 at Rc2_bit; temp=x*0.488;
sbit LCD_D5 at Rc1_bit; if(temp>=40){
sbit LCD_D4 at Rc0_bit; portd.b1=1;
// Pin direction }
sbit LCD_RS_Direction at TRISc5_bit; else {
sbit LCD_EN_Direction at TRISc4_bit; portd.b1=0;
sbit LCD_D7_Direction at TRISc3_bit; }
sbit LCD_D6_Direction at TRISc2_bit; inttostr(temp,disp);/* converting data to string
sbit LCD_D5_Direction at TRISc1_bit; b/c LCD displays strings/chars */
sbit LCD_D4_Direction at TRISc0_bit;
int i; lcd_cmd(_lcd_clear);
double x,temp; lcd_out(1,1,"The current Temp";
char disp[3]; lcd_out(2,1,"=");
void main() { lcd_out(2,7,disp);
Lcd_Init(); delay_ms(1000);
adc_init(); }}

You might also like