You are on page 1of 80

5/14/2020 ITI_Medhat Fahmy

1
AGENDA

5/14/2020
 Real Time System Over view

ITI_Medhat Fahmy
 Types of real time systems

 Misconception of RTs

 Multitasking Vs Concurrency

 Real Time Systems Architecture

 Kernel Structure

 RTOS Services

 RTOS Concepts

2
AGENDA

5/14/2020
 Run Lap1 then discuss the following
 What is the Task ?

ITI_Medhat Fahmy
 Task types
 Resources

 Scheduler and Dispatcher


 Scheduler Types …Check the FreeRTOSConfig.h

 Scheduling Algorithms

 FreeRTOS Scheduer Algorithm

 Memory Management
 Where is the TCB located in memory
3
 The Dynamic memory allocation
REAL-TIME

5/14/2020
ITI_Medhat Fahmy
SYSTEMS
OVERVIEW
4
REAL-TIME SYSTEMS

 What are the Real-Time Systems?


 Simple definition:
“Those systems that respond to external events in a timely
fashion”
“A real-time system is a software system where the correct
functioning of the system depends on the results
produced by the system and the time at which these
results are produced.”

5
REAL-TIME SOFTWARE DESIGN
 Designing embedded software systems whose behaviour is subject
to timing constraints

6
TYPES OF REAL TIME SYSTEMS

 Hard real-time system:


 Also known as an immediate real-time system.
 The system that must operate within the confines of
a stringent deadline. It is considered to have failed if
it does not complete its function within the allowed
time span.
 Examples:
 Car Air bag.
 anti-lock Braking Systems(ABS).

 aircraft control systems.

7
TYPES OF REAL TIME SYSTEMS (CONT.)
 Soft real-time system:
 The system that can operate with the presence of
latency at deadline.
 It is not considered to have failed if it does not
complete its function at deadline but that affects the
quality of the output.
 Examples:
 Video Processing
 Audio processing

 Digital cameras

 Mobile phones

8
EMBEDDED SYSTEMS
VS.
REAL TIME SYSTEMS

9
Real-time computing is equivalent to fast

5/14/2020
computing.

ITI_Medhat Fahmy
Truth=> Real-time computing is equivalent to
PREDICTABLE computing

10
 Real-time programming is assembly coding.

5/14/2020
Truth=> It is better to automate (as much as

ITI_Medhat Fahmy
possible) real-time system design, instead of
relying on a clever hand-crafted code.

11
 “Real time” means performance engineering.

5/14/2020
ITI_Medhat Fahmy
Truth => In real-time computing, timeliness is
always more important than performance.

12
 Real-time systems function in a static

5/14/2020
environment.

ITI_Medhat Fahmy
Truth =>Real-time systems consider systems
in which the operating mode may change
dynamically. On the other hand everything
should be deterministic!

13
REAL-TIME SYSTEMS

5/14/2020
 Systems which monitor and control their
environment

ITI_Medhat Fahmy
 Inevitably associated with hardware devices
 Sensors: Collect data from the system environment
 Actuators: Change (in some way) the system's
environment
 Time is critical. Real-time systems MUST
respond within specified times

14
STIMULUS/RESPONSE SYSTEMS

5/14/2020
 Given a stimulus, the system must produce a
response within a specified time

ITI_Medhat Fahmy
 Periodic stimuli: Stimuli which occur at
predictable time intervals
 For example, a temperature sensor may be polled 10
times
per second
 Aperiodic stimuli. Stimuli which occur at
unpredictable times
 For example, a system power failure may trigger an
interrupt which must be processed by the system
15
STRUCTURE OF REAL-TIME SYSTEM

5/14/2020
ITI_Medhat Fahmy
Periodic i.e.: a temperature sensor may be polled 10 times per second.
Aperiodic i.e.: a system power failure may trigger an interrupt which must be
processed by the system

16
ARCHITECTURAL CONSIDERATIONS

5/14/2020
 Because of the need to respond to timing demands made by
different stimuli/responses, the system architecture must allow
for fast switching between stimulus handlers.

ITI_Medhat Fahmy
“The need of fast switching between handlers.”

17
ARCHITECTURAL CONSIDERATIONS CONT.

5/14/2020
 Timing demands of different stimuli are different so a simple
sequential loop is not usually adequate.

ITI_Medhat Fahmy
18
ARCHITECTURAL CONSIDERATIONS CONT.

5/14/2020
 Real-time systems are usually designed as cooperating processes
with a real-time executive controlling these processes.

ITI_Medhat Fahmy
19
A REAL-TIME SYSTEM MODEL

5/14/2020
Sensor Sensor Sensor Sensor Sensor Sensor

ITI_Medhat Fahmy
Real-time
control system

Actuator Actuator Actuator Actuator

20
SYSTEM ELEMENTS

5/14/2020
 Sensors control processes
 Collect information from sensors. May buffer information
collected in response to a sensor stimulus

ITI_Medhat Fahmy
 Data processor
 Carries out processing of collected information and computes
the system response
 Actuator control
 Generates control signals for the actuator

21
SENSOR/ACTUATOR PROCESSES

5/14/2020
ITI_Medhat Fahmy
Sensor Actuator

Stimulus Response

Sensor Data Actuator


control processor control

22
5/14/2020
ITI_Medhat Fahmy
NEED TO USE MULTI-TASKING

23
WHY USE MULTI-TASKING ?

5/14/2020
 I/O devices are usually very slow

ITI_Medhat Fahmy
 While one task is waiting for the I/O operation, other tasks can
execute
 Better CPU utilization
 Parallel I/O access can be achieved
 Tasks can be scheduled so that slower blocking jobs are handled
by lower priority tasks, while faster more time critical tasks are
given more CPU time

24
MULTITASKING VS CONCURRENCY

5/14/2020
ITI_Medhat Fahmy
25
 Multi-Tasking (Foreground/Background

5/14/2020
with multiple backgrounds)
main()

ITI_Medhat Fahmy
{
StartMultitasking()
}
Forground
Task1() Task2() TaskN() ISR’s
{ { {

} } }

Multiple Background Tasks


26
5/14/2020
ITI_Medhat Fahmy
Application SW

RTOS

Hardware 27
 A real-time operating system (RTOS) is an operating system

5/14/2020
(OS) intended for real-time applications.
 The idea of the Operating Systems(OS) comes with the

ITI_Medhat Fahmy
definition of the abstraction.
 Abstraction means here is not to put in consideration what is
being happened under the abstraction layer, only think with the
interfacing issues.
 Any OS specially RTOS acts as abstraction layer between the
application layer, and the hardware abstraction layer (HAL).

28
5/14/2020 ITI_Medhat Fahmy
29
5/14/2020 ITI_Medhat Fahmy
30
5/14/2020
 Reliability

ITI_Medhat Fahmy
 Predictability

 Performance

 Compactness

 Scalability

31
5/14/2020 ITI_Medhat Fahmy
RTOS CONCEPTS

32
A Task is typically an infinite loop

5/14/2020
(background)
 Each task sees the CPU as if it is the only task

ITI_Medhat Fahmy
running on the system
 This requires that each task should have its own
context (values of CPU registers)
 Each task should have its own stack
 Task’s context is stored in its stack when the task is
not running.
 Task’s context should be popped in CPU registers
when it starts running
33
There are four different types of tasks:
 Initialization task: executed only at the initialization

5/14/2020
of the system to initialize system components and
peripherals

ITI_Medhat Fahmy
 Cyclic task: once created it executes with fixed
periodicity
 Event based task: executed only if a specific event
has occurred, this task is not triggered by interrupts.
Example: after the occurrence of a fault in the system it is
saved in EEPROM
 Interrupt based task: executed only if a specific
event has occurred, this task is triggered by interrupts,
however it's not preferred to be executed in the ISR
34
Priorities: there are two types of priorities

5/14/2020
 Task Priorities
 Interrupt Priorities

ITI_Medhat Fahmy
 Priorities is assigned to the tasks, the more important
the task is, the higher the priority of it is.

 There is two types of priorities:


 Static priority.
 Dynamic priority.

35
 Context:

5/14/2020
CPU environment and system resources seen by the

ITI_Medhat Fahmy
task each time it is scheduled.
 Saved in TCB
 includes
 Task PC
 CPU register
 Stack for dynamic variables and function calls

36
• Kernel : the main part of OS that manages task execution

5/14/2020
and communication between task
• The scheduler is the part of the kernel responsible for deciding
which task should be executing at any particular time. The kernel

ITI_Medhat Fahmy
can suspend and later resume a task many times during the task
lifetime.

37
 ISR - a routine connected to an interrupt

5/14/2020
 All ISRs use the same interrupt stack

 Interrupt stack is allocated and initialized by the

ITI_Medhat Fahmy
system at start up
 ISRs must not invoke routine that might cause the
caller to block

38
 Interrupt Latency : The Maximum amount of
Time interrupts are delayed before executing the

5/14/2020
first instruction in the ISR
 Interrupt Response : Interrupt latency + Time

ITI_Medhat Fahmy
to save the CPU context
 Interrupt Recovery : Time to restore the CPU
context

39
 True Concurrent execution : Two or more tasks being

5/14/2020
executed at the same time

ITI_Medhat Fahmy
 Pseudo Concurrent execution : The processor is
switching between two or more tasks, each task is being
executed for a small time slot and then the processor
switches to the other task. This operation occurs with a
very fast rate giving the impression that these tasks are
being executed at the same time.

40
Foreground/Background Systems (one background

5/14/2020
infinite loop + ISRs)

ITI_Medhat Fahmy
main()
{
for(;;)
{
/*Background code*/
} Forground ISR’s

}
41
5/14/2020 ITI_Medhat Fahmy
42
 It called also super-loops systems.

5/14/2020
 An application consists of an infinite loop that calls modules
to perform the desired operations (background).
 Interrupt Service Routines (ISRs) handle asynchronous

ITI_Medhat Fahmy
events (foreground)

“So the task level (Background) perform the main functionality,


but the interrupt level (Foreground) perform the
Asynchronous events “

43
SESSION TWO

5/14/2020
 Review session one
 FreeRTOSConfig.h

ITI_Medhat Fahmy
 vTaskDelay( )

 vTaskDelayUntil()

 Idle task

 Idle task HOOK

 Porting FreeRTOS on ARM cortex M4

 Resources

 Scheduling polices

44
Resources: there are two types of resources

5/14/2020
 Software resources: variables
 Hardware resources: peripherals, registers

ITI_Medhat Fahmy
45
 Scheduler / dispatcher:

5/14/2020
is the part of kernel that is responsible for task
execution based on Scheduling algorithm like task

ITI_Medhat Fahmy
priorities.

 Responsible for running multi-tasking on a certain


Scheduling algorithm.
 It should give the control to the task which meets the
scheduling algorithm criteria (time slice, priority, etc…)

Types of schedulers:
 Non-preemptive
46
 preemptive
Preemptive Non-preemptive

5/14/2020
ITI_Medhat Fahmy
47
5/14/2020
 Scheduling Algorithms
 Most of Embedded systems provides one of the
following scheduling algorithms:

ITI_Medhat Fahmy
 Preemptive priority-based scheduling
 Non-Preemptive Priority based
 Round Robin and Preemptive (RR) scheduling

 Most of RTOS provides preemption in its scheduling


algorithms

48
5/14/2020 ITI_Medhat Fahmy
 Preemptive priority-based scheduling
Task priority
Task
Completion
Preemption

Task3
High
Task2 Task2

Low Task1 Task1

Time
49
5/14/2020 ITI_Medhat Fahmy
 Round Robin and Preemptive scheduling
Task priority
Task
Completion
Preemption

High Time Slice ITI_Medhat Fahmy


Task4

Low Task1 Task2 Task3 Task3 Task1

Time
50
FREERTOS SCHEDULER ALGORITHM

5/14/2020
 FreeRTOS using :

ITI_Medhat Fahmy
Fixed priority :
Pre-emptive :
Time Slicing :

ConfigUSE_PREEMPTION
ConfigUSE_TIME_SCLICING

51
 It is the process of transferring the control from a certain

5/14/2020
task to another one based on the scheduling algorithm

 When the RTOS decide to switch to another task, it must to

ITI_Medhat Fahmy
save the CPU registers into the demolished task stack.

 Once that happens the new task begin to restore its context
from its storage area to begin to execute the code of the new
task.

 Context switching apply overhead to the CPU, the more the


CPU has register the more the overhead is applied.
52
FREERTOS CONTEXT SWITCHING

5/14/2020
53
5/14/2020 ITI_Medhat Fahmy
54
CONTEXT SWITCHING
5/14/2020 ITI_Medhat Fahmy
55
ITI
CONTEXT SWITCHING
CONTEXT SWITCHING (CONT..)

5/14/2020
ITI_Medhat Fahmy
56
CONTEXT SWITCHING (CONT..)

5/14/2020
ITI_Medhat Fahmy
57
CONTEXT SWITCHING (CONT..)

5/14/2020
ITI_Medhat Fahmy
58
CONTEXT SWITCHING (CONT..)

5/14/2020
ITI_Medhat Fahmy
59
CONTEXT SWITCHING (CONT..)

5/14/2020
ITI_Medhat Fahmy
60
CONTEXT SWITCHING (CONT..)

5/14/2020
ITI_Medhat Fahmy
61
Task waits for a
Waiting time or event

5/14/2020
(By the Task)
Delete Task timeout or event occurred
(By other Task or interrupt)

ITI_Medhat Fahmy
Run Task Task Interrupted
Create Task (By Scheduler) (By Interrupt)

ISR
Ready Running Running
Dormant
Task preempted
(By Scheduler)
Delete Task Run Task

Delete Task (By Scheduler)

62
MULTITASKING PROBLEMS

5/14/2020
 Deadlock

ITI_Medhat Fahmy
63
Non-Reentrant
Reentrant Function

5/14/2020
Function

ITI_Medhat Fahmy
64
5/14/2020 ITI_Medhat Fahmy
65
 The simplest way to make the tasks to communicate and

5/14/2020
interact with each other through shared resources.

ITI_Medhat Fahmy
 Each tasks must access the shared resource exclusively,
in order not to allow conflict to occur between tasks, and
this is called mutual exclusion.

 RTOS offer services to handle two issues which impact


real time application greatly, this two issues are:
 Shared Resources.
 Synchronization.

66
 For Synchronization:

5/14/2020
 Semaphore

ITI_Medhat Fahmy
 Mutex

 For Communication:
 Mail Box
 Message Queue

67
 a Semaphore is a variable or abstract data type that

5/14/2020
provides a simple but useful abstraction for controlling
access by multiple processes to a common resource in a
parallel programming environment.

ITI_Medhat Fahmy
 Suppose a library has 10 identical study Rooms, intended
to be used by One Student at a time. To prevent disputes,
students must request a room from the Front Counter if
they wish to make use of a study room.
 When a Student has finished using a Room, the student must
return to the Counter and indicate that one room has become free.
 If no Rooms are free, Students wait at the Counter until
someone relinquishes a room.
68
5/14/2020 ITI_Medhat Fahmy
69
 Used for both mutual exclusion and synchronization.

5/14/2020
 Semaphore types:

ITI_Medhat Fahmy
 Binary Semaphore  For Synchronization
 N-Counting Semaphore  For Synchronization
 Mutex  For Mutual Exclusion

70
 Mutual Exclusion:

5/14/2020
Semaphore is initially full, each task first takes and
returns back the semaphores.

ITI_Medhat Fahmy
 Synchronization:
Semaphore is initially empty and one task waits to
take the semaphore given by another task.

71
 Mutexes are used by task to gain exclusive

5/14/2020
access to resources.
 Mutex is a binary semaphore.

ITI_Medhat Fahmy
 Mutex has additional features which reduce
priority inversion problem(Discussed Later).

 Recursive Mutex.

72
5/14/2020 ITI_Medhat Fahmy
73
5/14/2020 ITI_Medhat Fahmy
74
mutual exclusion
5/14/2020 ITI_Medhat Fahmy
75
5/14/2020
• Priority Ceiling Protocol
 A ceiling priority is assigned for each

ITI_Medhat Fahmy
mutex
 Whenever a task locks the mutex, it
takes the ceiling priority
 Ceiling priority is always >= highest
priority of tasks that may lock the mutex

76
5/14/2020 ITI_Medhat Fahmy
77
MESSAGE QUEUES(CONT.)

5/14/2020
 Typical Message Queue Operations
 Creating and deleting Queues

ITI_Medhat Fahmy
 Obtaining Queue Information
 Sending and Receiving messages

78
MESSAGE QUEUES(CONT.)

5/14/2020
 Creating and Deleting Queues
 Create Q with parameters:

ITI_Medhat Fahmy
 Q length (maximum number of messages)
 Q element length

 Deleting
 Unblocks all waiting tasks
 Deleting all messages and free resources

79
5/14/2020
Thank you for
your attention!

ITI_Medhat Fahmy
Any Questions?
Contact IMTSCHOOL team

80

You might also like