Real Time Systems

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Concepts of Real-Time Systems

Part 1

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 1

Hello and welcome to the seminar.

My name is Chinmay Patel. I am a principle applications engineer with Microchip’s


High Performance Microcontroller Division. The focus of my engineering career of
more than 15 years has been the development of real-time systems software.

This is the first in the series of short seminars that will introduce you to concepts of
real-time systems. In this seminar, we shall discuss the fundamental concept of
“task” and relevant topics. The expected length of this seminar is 15 minutes.

Let’s get started…

© 2008 Microchip Technology Inc. All Rights


Reserved. 1
System
Microcontroller
z A dishwasher is a
PIC® MCU system
− Water intake/drain
− Motor operation
− Door opening/closing
− Display status
z A microcontroller
manages system
operations

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 2

So what is a system?
A system is something that solves a problem.
For example, to solve the problem of dirty dishes, we can design a system of a
dishwasher.
As we know, when we start a dishwasher, many things happen; for example, water
intake, motor operation that swirls water to clean dishes, water drain, etc.
Dishwashers also provide a variety of wash-cycles to handle different types of
utensils and the amount of cleaning they need. Also, if you open the door of a
dishwasher in the middle of its operation, it most likely will sense that the door is
opened and will stop its operation. And the latest high-end dishwashers may even
have an LCD display that shows various status and control information on the front-
panel.

An intelligent unit, such as a microcontroller, can manage various functions in a


system in some coherent way.
For example, a Microchip PIC® microcontroller, embedded in a dishwasher, can
manage various functions to fulfill coherent operation of the dishwasher.

© 2008 Microchip Technology Inc. All Rights


Reserved. 2
Task

z Group of
Motor
Display Manager instructions
Manager
performing a
PIC® MCU function of a system
Water
Manager z Dishwasher system
Door may have following
Manager
tasks:
− Water Manager
− Motor Manager
− Door Manager
− Display Manager

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 3

So what is a task?
A task is a group of instructions that executes on a microcontroller to solve a
portion of the problem. This group of instructions performs a function of a
system.

For example, in a dishwasher system, the following tasks may exist:


1. Water Manager: This task manages the water intake and drain
2. Motor Manager: This task manages the direction and speed of a motor according
to the selected wash cycle
3. Door Manager: This task tracks the status of the dishwasher door and informs
other tasks of the current status or any change in the status
4. Display Manager: This task tracks the status of various functions of the
dishwasher and displays it on the LCD mounted on the front-panel

© 2008 Microchip Technology Inc. All Rights


Reserved. 3
Multi-tasking

z Doing multiple things at


once
z For a dishwasher
system, the embedded
microcontroller may
perform the following
tasks (all at once):
− Water Management
− Motor Management
− Respond to Door
Opening/Closing
− Display Various Status
Information

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 4

Let’s talk about multi-tasking.


While zigzagging on a highway with a cup of coffee in one hand and a mobile
phone in the other, we are multi-tasking. In this case, the central (non)-intelligent
unit in our system, the brain, is managing multiple tasks of driving, drinking,
talking, and (potentially safely) switching lanes, all at once.

Likewise, in the dishwasher system, the microcontroller may be concurrently


managing a variety of tasks such as Water Manager, Motor Manager, Door
Manager, Display Manager, etc. Each task individually “thinks” that it has the full
attention of the microcontroller; however, in reality, the microcontroller is attending
each one of them according to a pre-determined scheme, called scheduler. We will
talk about schedulers later in this seminar.

© 2008 Microchip Technology Inc. All Rights


Reserved. 4
Deadline
Unhappy
owner
z Essential aspect of
real-time system
z Tasks perform their
function within
prescribed deadline
− Failure may result in
severe consequences
z Deadlines can be
− Hard
ter
Wa ll
sp
i − Soft

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 5

Now let’s talk about the most essential aspect of a real-time system, the “deadline”.

In our dishwasher system, say the Water Manager task is currently filling the water in the chamber.
When the dishwasher is full with water, the Water Manager task should IMMEDIATELY shut the
water intake. Otherwise, the water may spill out of the dishwasher.

Every task in a real-time system is characterized by its deadline. A task is expected to perform its
function by the time allocated to it (meaning before a deadline). The time allocation, or the deadline,
may be absolute or relaxed. For example, if a task MUST perform its function within one second,
then the deadline is an absolute deadline. On the other hand, if the task SHOULD perform its
function in about one second or so, then the deadline is relaxed.

When the deadlines are absolute, the real-time system is called a hard real-time system. The tasks in
a hard real-time system are governed by rigid time constraints.

When the deadlines are relaxed, the real-time system is called a soft real-time system. The tasks in a
soft real-time system do not have rigid time constraints.

The deadline for the Water Manager task in the scenario mentioned above should be absolute;
otherwise the probability of spilling water would be quite high resulting in many unhappy
dishwasher owners with frequent wet floors! On the other hand, a Display Manager task may have a
relaxed deadline.

Obviously, if a real-time system task fails to meet its deadline, it may have dire consequences.

Since there is only one microcontroller performing many different tasks of varied importance, it is
imperative to determine task deadlines and their relative importance in the system.

© 2008 Microchip Technology Inc. All Rights


Reserved. 5
Priority

priority z Every task in a


system has priority
Dishwasher System
Task Priorities z Priority determines
the relative order of
Water Manager the importance of
High Priority
tasks in the system
− Important tasks have
higher priority
Display Manager − Less important tasks
Low Priority have lower priority

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 6

This leads us to the concept of priority.

Priority determines the importance of a task in the system. In a real-time system,


every task has an assigned priority. The higher the priority, the better the chance of
the task executing on the microcontroller and meeting its deadline.

So in our dishwasher system, we may assign higher priority to the Water Manager
than the Display Manager. This ensures that when water needs to be shut-off, the
on-going display update will not unduly delay the Water Manager from performing
its job on time. No water spills, no wet floors, happy owners!

But how does the priority actually help coherent functioning of the system?

© 2008 Microchip Technology Inc. All Rights


Reserved. 6
Preemption

z Prerogative of higher priority tasks in a system


z Allows higher priority task to pause a lower
priority task
z Necessary to maintain system integrity of a
real-time system

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 7

Using priorities of each task in a system, we can devise a scheme that allows
coherent operation of the system.

Simply by preventing the low priority tasks from doing their work, when a higher
priority task wants to do work, we can maintain coherency of operation. In other
words, by virtue of its priority, a task can PRE-EMPT lower priority tasks.

This prerogative of a higher priority task, to perform its job even when other lower
priority tasks are performing their job, is known as preemption.

Let’s see an example to explain the concept.

© 2008 Microchip Technology Inc. All Rights


Reserved. 7
Preemption Example
Priority

Higher
priority

Lower
priority

time
0 Time T1 Time T2 Time T3 Time T4
- Display - Dishwasher - Water Shut-off - Display update
update chamber is full - Display update Completed
started paused
Priority

Higher Water Manager


priority

Lower
priority Display Manager Display Manager Paused Display Manager

time
Time T1 Time T2 Time T3 Time T4
© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 8

As shown in this slide, consider two tasks in the dishwasher system, the higher
priority Water Manager, and the lower priority Display Manager. There are two
figures shown on this slide: the upper figure shows visual of the two tasks in form of
their function, while the lower figure shows the time-continuum of tasks executing
on the microcontroller embedded in the dishwasher.

At time T1, the water tap is ON and the dishwasher chamber is being filled with
water. Exactly at this time, the Display Manager is updating the display and has
printed “Goo” on the display, which is part of the complete message “Good Status”
that it wants to show on the display.

Next, at time T2, the dishwasher chamber is full so that there is a need to shut-off
the water tap.

At time T3, immediately after time T2, the Display Manager is paused (i.e.
preempted), and the Water Manager swings into action and shuts-off the water tap.
Notice that the display at time T3 is still showing “Goo”.

Only after the water is shut-off, at time T4, that the Display Manager is able to
complete its work and the display is showing “Good Status”. Notice on the lower
figure that the Display Manager is preempted by the Water Manager between Time
T2 and Time T4.

© 2008 Microchip Technology Inc. All Rights


Reserved. 8
Scheduler

z Maintains ‘ranked
list’ of tasks based
on priority
z Enforces ‘ranked
list’ of tasks by
allowing or
disallowing tasks to
perform their job
z Real-time systems
have a Priority
Based Preemptive
Scheduling scheme

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 9

But which authority in the system decides which task runs at what time?
Well, that’s the job of the Scheduler.

A Scheduler in the system lines-up tasks in accordance with their priority. It works
as an arbitrator. At any given time, it decides which task must perform its job and
which task should be paused.

Thus, when the water needs to be shut-off in a dishwasher, the Scheduler decides
that the lower priority task, the Display Manager, must give way to the higher
priority task, the Water Manager. It’s only after the Water Manager finishes its job
that the Scheduler allows the Display Manager to continue its work.

Thus, in effect, the Scheduler in a real-time system maintains a ‘ranked list’


according to the priorities of all the tasks in the system. And by means of
Preemption, the Scheduler enforces the ‘ranked list’ by allowing or disallowing any
particular task to perform its job at any given time.

Hence, the typical scheme employed for a Scheduler in the real-time system is
called Priority Based Preemptive Scheduling. Quite a mouthful! Huh!

© 2008 Microchip Technology Inc. All Rights


Reserved. 9
Summary

z Basic unit of a real-time system is a Task


z There may be multiple tasks in a system
z Tasks have deadlines
z Tasks have priority
z Higher priority task preempt lower
priority tasks
z Scheduler enforces priority based
preemption

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 10

That brings us to the end of this seminar. Let’s summarize the concepts we just
covered:
-A basic unit of operation in a system is a task
-A system may have multiple tasks running at any given time
-Tasks in real-time systems have deadlines. If tasks fail to meet deadlines, dire
consequences may follow.
-According to their importance, tasks get assigned priorities. The higher the priority,
the higher the importance.
-Higher priority tasks can do their job while pausing the lower priority tasks. This is
called preemption.
-A Scheduling scheme enforces priority based preemption to maintain coherency of
the system.

© 2008 Microchip Technology Inc. All Rights


Reserved. 10
Where to Get More Information

z Wikipedia (www.wikipedia.com)
− Search for the word “RTOS”
− Happy browsing
z MicroC OS II: The Real Time Kernel by
Jean J. Labrosse
− ISBN-13: 978-1578201037
z Modern Operating Systems by Andrew S.
Tanenbaum
− ISBN-13: 978-0136006633

© 2008 Microchip Technology Incorporated. All Rights Reserved. Concepts of Real Time Systems – Part 1 Slide 11

Lastly, if this seminar has incited enough interest, please do read more on the topic
at the references shown on this slide.

The easiest to find and have fun with, is the Wikipedia at www.wikipedia.com. On
this website search for the word “RTOS” and cruise-on from there on.

Another very helpful resource on real-time system and operating system concepts is
a book written by Jean Labrosse, who is also the author of MicroC OS. This real-
time operating system is ported on all Microchip microcontrollers. His book has
abundant reading material for novice as well as experienced programmer.

Finally, my all time favorite book on introductory concepts of operating systems is


written by Andrew Tanenbaum. It covers aspects of general purpose as well as real-
time operating system software.

With that I shall say good-bye, and please don’t forget to provide your feedback on
this seminar. Thanks for your time today!

© 2008 Microchip Technology Inc. All Rights


Reserved. 11

You might also like