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

Embedded Systems

Department of Electrical and Computer Engineering


MWU University

Lecture 4
Chapter 4
Real-time Operating systems

 Context switching mechanisms


 Scheduling policies
 Message passing and shared memory
communications
 Inter-process communication
Real-Time Operating System (RTOS)
 Real-time and embedded systems operate in constrained
environments in which computer memory and processing
power are limited
 They often need to provide their services within strict
time deadlines to their users and to the surrounding
world.
 The memory, speed and timing constraints are that dictate
the use of real-time operating systems

3
Real-Time Operating System (RTOS)

RTOS Architecture

RTOS are said to require:


Multitasking (multiple tasks)
Multithreading (multiple users, concurrent requests)
A sufficient number of interrupt levels

4
Real-Time Operating System (RTOS)

 Multitasking
 allowing a user to perform more than one
computer task at a time
 to keep track of where you are in these tasks
 go from one to the other without losing
information
 multitasking not equal multithreading
 Examples:-
MS Windows 2000, IBM's OS/390, Linux

5
Real-Time Operating System (RTOS)
• Multithreading
 thread is placeholder information associated with a
single use of a program that can handle multiple
concurrent users
 If multiple users are using one program, a thread is
created and maintained for each of them
 thread information is kept in a special data area and
putting the address in a register
 Languages that support OOP also accommodate and
encourage multithreading, like Java

6
Real-Time Operating System (RTOS)

 Interrupt
 Is a signal from a program (or from a device) that
causes the main program (the operating system) to
stop and figure out what to do next
 Almost all personal (or larger) computers today are
interrupt-driven
 Basically, a computer can perform only one
instruction at a time. But, because of interrupt, it can
handle many tasks. (multitasking and multithreading)
 Every OS has an interrupt handler (to handler priority,
save the queue, etc)
 There are HW interrupts and SW interrupts

7
Real-Time Operating System (RTOS)

 RTOS kernel supplies five main categories of


basic services to application software
 Task Management (with priority-based preemptive task
scheduling, assign priorities, control execution etc)
 Inter-task Communication and Synchronization (pass
information from one task to another, task coordination, etc)
 Timer services (task delays, time-outs, etc)
 Dynamic Memory Allocation (non-fragmenting memory
allocation, tasks may "borrow" RAM temporarily, memory often passes
from task to task, etc)
 Device I/O Supervisor (accessing the many hardware device
drivers )

8
Real-Time Operating System (RTOS)

 RTOSs vs. General-Purpose OSs


RTOSs OSs
similar kernel services similar kernel services
Deterministic (service times could be Non-deterministic (service times could
expressed as mathematical formulas) not be expressed as mathematical
formulas)

no random timing random timing


load-independent timing:- load-dependent timing
T(message_send) = constant,
irrespective of the size of the message
Windows XP Embedded (XPE), Windows, Unix or Linux, etc
Real-Time Linux, LynxOS RTOS

9
Real-Time Operating System (RTOS)

• Scheduling Techniques
 Most RTOSs do their scheduling of tasks using a
scheme called "priority-based preemptive scheduling"
 Each task in a software application must be assigned a
priority (high, low)
 "Preemptive" means that the scheduler is allowed to
stop any task at any point in its execution, if it
determines that another task needs to run
immediately
 The Highest Priority Task that is Ready to Run, will be
the Task that Must be Running.

10
Real-Time Operating System (RTOS)

• If a higher-priority task is triggered (ready to run), the lower-priority


task is interrupted to run the higher-priority task
• If a low-priority task and a higher-priority task are ready to run, the
scheduler will allow the higher-priority task to run first. The low-
priority task will only get to run after the higher-priority task has
finished with its current work

11
Real-Time Operating System (RTOS)

 Each time the priority-based preemptive scheduler is


alerted by an external world trigger (such as a switch
closing) or a software trigger (such as a message arrival), it
must go through the following 5 steps:
 Determine whether the currently running task should continue to run. If
not ?
 Determine which task should run next.
 Save the environment of the task that was stopped (so it can continue
later).
 Set up the running environment of the task that will run next.
 Allow this task to run.
 These 5 steps together are called "task switching."

12
Real-Time Operating System (RTOS)

Timeline for Priority-based Preemptive Scheduling


Task Switching Time

13
Real-Time Operating System (RTOS)

 Task Switching Time


 The time OS takes to do task switching is of
interest when evaluating an OSs
 This time may vary, if there are more tasks in a
queue to search through, the search will take
longer; this make the OS non-deterministic
(non-real-time OS)
 To avoid the time variation, RTOS applies
1. Using incrementally updated tables that allow the task
scheduler to identify the task that should run next in a rapid
fixed-time
2. Message Transfer via Message Queue (see next)

14
Real-Time Operating System (RTOS)

Message Transfer via Message Queue


Uses one of the two methods:-
 Method 1:- copying message to an operating system-owned "secret"
area of RAM memory (implementing the "message queue"); and then
copying from the "secret" RAM area to the receiver. Clearly this is
non-deterministic in its timing, as these copying activities take longer
as message length increases.
 Method 2:- operating system copy a pointer to the message and deliver
that pointer to the message-receiver task without moving the message
contents at all. This approach avoids the non-determinism and also
accelerates performance

Note:- the second method is used RTOS

15
Real-Time Operating System (RTOS)

Task switching time might in special cases be quicker


for non-RTOS then RTOS, it means, the term "real-
time" does not mean always "as fast as possible" but
rather "real-time" demands consistent, repeatable,
known timing performance.

• RTOS – constant task switching time (independent of any load)


• Non-RTOS – time increase, if the queue increases

16
Real-Time Operating System (RTOS)

Inter-task Communication and Synchronization


Case:- A task might be preempted when it is in the middle of
updating a table of data. If a second task that preempts it reads
from that table, it will read a combination of some areas of
newly-updated data plus some areas of data that have not yet
been updated (will communicate corrupted information)
 A mechanisms for communication and synchronization
between tasks are provided to avoid these kinds of errors
 If you can’t apply synchronization, it delays the preemption to
make the communication correct and this again contradicts
with constant task switching time of RTOS
 RTOS applies:- the passing of synchronization information
from one task to another, Each message can contain an array or
buffer of data provide message queues for holding the
messages until they can be processed

17

You might also like