Вы находитесь на странице: 1из 268

REAL TIME OPERATING

SYSTEMS

Lesson--1:
Lesson
OPERATING SYSTEM SERVICES
GOAL, MODES AND STRUCTURE

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. OS Services Goal

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Goal

The OS Service Goal – Perfection and


correctness during a service

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 3


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Services Goal
(i) Facilitating easy sharing of resources as
per schedule and allocations─
No processing task or thread uses any
resource until it has been allocated by the
OS at a given instance

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 4


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Meaning of Resource of a system
• processor (s), memory, I/O
• devices, virtual devices,
• system timer, software timers
• keypad, displays, printer and other such
resources, which processes (tasks or
threads) request from the OS.

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Services Goal…
 (ii) Facilitating easy implementation of the
application program with the given system-
hardware through the system software

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Services Goal…
 (iii) Optimally scheduling the processes on
one (or more CPUs if available) by
providing an appropriate context switching
mechanism

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 7


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Services Goal…
 (iv) Maximizing the system performance to
let different processes (or tasks or threads)
share the resources most efficiently with
protection and without any security breach.

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 8


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Services Goal…
 (v) Providing management functions for the
processes (tasks or threads), memory, file,
physical and virtual devices and I/Os and
for other functions for which it is designed
 (vi) Providing management and
organisation functions for the I/Os, devices
and files and file -like devices

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Services Goal…
 (vii) Providing interoperability of the
application on different networks
 (viii) Providing a common set of
interfaces that integrates various
devices and applications through
standard and open systems

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 10


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Services Goal…
 (ix) Providing easy interfacing and
management functions for the network
protocols and network
 (x) Providing portability of the application
on different hardware configurations

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 11


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. User and Supervisory mode
structure

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Processor in the system running in two
modes
 Clock, called system clock.
 At every clock tick of the system-clock,
there is system interrupt.
 On each system interrupt, the system time
updates, the system context switches to the
supervisory mode from user mode.
 After completing the supervisory functions
in the OS, the system context switches back
to user mode

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 13


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
User mode

 User process─ permitted to run and use


only a subset of functions and instructions
in OS.
 Use of OS functions in user mode─ either
by sending a message to the waiting process
associated in the OS kernel space or by
system call (calling an OS function).

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 14


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
User mode

 The use of hardware resources including


memory is not permitted without OS
making the call to the OS functions, called
system call.

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 15


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
User mode
 User function call, which is not a system
call, is not permitted to read and write into
the protected memory allotted to the OS
functions, data, stack and heap
 That protected memory space is also called
kernel space. Hence execution of user
functions calls is slower than the execution
of the OS functions (on system call) due to
need to spend time in first checking the
access permission to the protected space
2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 16
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Supervisory mode
 Also called kernel mode
 OS runs in protected mode the privileged
functions and instructions in protected mode
that are the privileged ones and the OS
(more specifically, the kernel) is only one
permitted to access the hardware resources
and protected area memory
 Kernel space functions and processes
execute faster than the user space functions
and processes.
2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 17
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Supervisory mode

 Only a system call is permitted to read and


write into the protected memory allotted to
the OS functions, data, stack and heap

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 18


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3. Example─
Example─ Improving system
performance by running application
program threads in supervisory (kernel)
mode

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 19


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS Windows CE and several RTOSes

 Run all the threads run in the supervisory


mode (kernel mode). Therefore, the threads
executes fast.
 Improves the system performance.
 If the threads are to execute in user mode,
as in Unix or in non-real time OS, then the
execution slows down due to time spent in
checks on the code access to the protected
kernel space
2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 20
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3. Structure

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 21


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Top to down Structural Layers

 Software Application Programming


Interface (API)
 System software other than the one
provided at the OS
 OS Interface
 OS
 Hardware–OS Interface
 Hardware

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 22


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Top to down Structural Layers

• OS is the middle in-between layer


between the application software and
system hardware

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 23


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4. Kernel Services in the OS

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 24


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Kernel
 Process, memory and IO managers are
essential components of a kernel
 Kernel may include file and device
management functions as part of the
kernel in a given OS
 Kernel in certain OSes is without File and
Device management functions as part of
the kernel in the given OS and any other
needed functions other than process,
memory and IO device managers, which
are essential components of a kernel are
not provided for at the kernel
2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 25
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process Management Kernel Services in an
OS
 Creation to deletion of Processes
 Process structure maintenance
 Processing resource requests
 Scheduling Processes
 Inter process Communication (IPC)
(communication between Tasks, ISRs, OS
functions)

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 26


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory Management Kernel Services in an
OS
 Allocation and de-allocation between
Tasks, ISRs, OS functions

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 27


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Kernel Services in an OS

 File Management
 Device Management,
 Device Drivers
 I/O Management
 Interrupts Control (by handling ISR)
Mechanism

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 28


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 29


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
 OS goal is perfection, correctness,
portability, interoperability, and providing a
common set of interfaces for the system,
resources and orderly access and control
when managing the processes

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 30


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• On each system clock interrupt, the system
time updates, the system context switches to
supervisory mode from user mode. After
completing the supervisory functions in the
OS, the system context switches back to
user mode

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 31


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
 OS structure consists of kernel and other
service functions outside the kernel. System
software includes the kernel. The OS lets
the application run on the hardware
 OS is the middle in-between layer between
the application software and system
hardware
 Process, memory and device managers,
which are essential components of a kernel

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 32


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• Kernel - A basic unit of any OS that
includes the functions for processes,
memory, task scheduling, inter process
communication, management of
devices, IOs, and interrupts and may
include the file systems and network
subsystems in certain OSes

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 33


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• OS lets a process execute on the CPU─
some process at OS for a resource-
management mechanism lets it use the
system-memory and other system-
resources such as network, file, display
or printer

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 34


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt

 Kernel functions are management of


processes, process and resources scheduling
memory, file, devices, IOs, network and
handling the interrupts

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 35


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 1 of Chapter 8

2008 Chapter-8 L1: "Embedded Systems - Architecture, Programming 36


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING
SYSTEMS

Lesson--2:
Lesson
PROCESS MANAGEMENT

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. Process Creation

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process creation

• Step 1: At the reset of the processor. in a


first─
computer system, an OS is initialized first─
enabling the use of the OS functions, which
includes the function to create the processes
Step 2: Using OS process creation function, a
process, which can be called initial process, is
created.
Step 3: OS started and that calls the initial
process to run.
2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 3
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process creation
 Step 4: When the initial process runs, it
creates subsequent processes.
Processes can be created hierarchically.
 OS schedules the threads and provide for
context switching between the threads (or
tasks).

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 4


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Creation of a process
 Means defining the resources for the
process and address spaces (memory
blocks) for the created process, its stack, its
data and its heap and placing the process
initial information at a PCB

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
PCB
 (a) Context
 (ii) Process stack pointer
 (iii) Current state [Is it created, activated or
spawned? Is it running? Is it blocked?]
 (iv) Addresses that are allocated and that are
presently in use

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
PCB
 (v) Pointer for the parent process
 (vi) Pointer to a list of daughter processes.
 (vii) Pointer to a list of resources, which are
usable (consumed) only once. For
examples, input data, memory buffer or
pipe, mailbox message, semaphore
 (viii) Pointer to a list of resources, which
are usable (consumed) only once

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 7


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
PCB
 (ix) Pointer to queue of messages.
 (x) Pointer to Access-permissions
descriptor
 (xi) ID

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 8


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. Example of Process Creation

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS_Task_Create ( ) function
 OS function creates a process using
OS_Task_Create ( ) function
Task_Send_Card_Info in the mai
 Task_Send_Card_Info task creates two
other tasks, Task_Send_Port_Output and
Task_Read_Port_Input.
 OS then controls the context switching
between the processes

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 10


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Process Creation function
OS function first creates the Display_process.
Display_process creates─
 Display_Time_DateThread
 Display_BatteryThread
 Display_SignalThread
 Display_ProfileThread
 Display_MessageThread
 Display_Call StatusThread
 Display_MenuThread
2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 11
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3. Message passing and System call to
OS by Processes

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Message Passing by process running in
user mode
 Generates and puts (sends) a message
 OS lets the requested resource (for example, input
from a device or from a queue) use or run an OS
service function (for example, define a delay
period after which process needs to be run again).
 A message can be sent for the OS to let the LCD
display be used by a task or thread for sending the
output.
 An ISR sends a message to a waiting thread to
start on return from the ISR
2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 13
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
System call by process running in user
mode
 Call to a function defined at the OS.
 For example, OSTaskCreate ( )─ to create a task.
 First an SWI instruction is issued to trap the
processor and switch to supervisory mode.
 OS then executes a function like a library function
 Processor on finishing the instructions of a called
function, the processor again switches back to user
mode and lets the calling process run further

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 14


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4. Process Manager Functions

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 15


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process manager
 (i) makes it feasible to let for a process to
sequentially execute or block when needing
a resource and to resume when it becomes
available,
 (ii) implements the logical link to the
resource manager for resources
management (including scheduling of
process on the CPU),

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 16


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Process manager
 (iii) allows specific resources sharing
between specified processes only,
 (iiiv) allocates the resources as per the
resource allocation- mechanism of the
system and
 (iv) manages the processes and resources of
the given system.

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 17


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 18


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
 Process manager has functions to create the
processes,
 allocates a PCB to each process,
 manages access to the resources and
facilitates the switching from one process
state to another.
 The PCB defines the process structure for a
process state.

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 19


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
 Process can send a message and make a
system call to enable OS actions and run OS
function

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 20


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 2 of Chapter 8

2008 Chapter-8 L2: "Embedded Systems - Architecture, Programming 21


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-3:
Timer Functions

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. Timer Functions

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Timer Functions

 Real time clock─ system clock, on each


tick SysClkIntr interrupts
 Based on each SysClkIntr interrupts─
there are number of OS timer functions

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 3


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Timer functions at OS
 OS_TICK_PER_SEC─ to set the system
clock ticks and hence SysClkIntr
interrupts per s
 OSTickInit ( )─ to initiate system clock
ticks

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 4


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Timer functions at OS
 OSTimeDelay ( )─ to delay the process
making call by a fixed number of system
clock ticks specified in argument
 OSTimeDelay-Resume ( )─ to resume a
delayed process specified in the argument

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Timer functions at OS
 OSTimeSet ( )─ to set the counts of system
clock ticks
 OSTimeGet ( ) ─ to read the counts of
system clock ticks to find the time interval
from the previous read or write of the
counts

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Timer functions at OS
 OSSemPend (semVal, twait, *semErr)─ to
wait for a semaphore release
 OSMboxPend (semVal, twait, *mboxErr)
─ to wait for a message in mailbox (wait for
message pointed not Null)

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 7


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. System clock ticks and hence
SysClkIntr interrupts

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 8


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
SysClkIntr interrupts
 Before servicing of SysClkIntr, the
presently running task or thread or process
context saves on the TCB or thread stack or
PCB data structure,
 System switches to supervisory mode
 SysClkIntr service routine call the OS to
increment system time and to find the new
messages or IPCs, which the OS event
control blocks received from the system call
for the IPC functions,
2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 9
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
SysClkIntr service routine
 call the OS functions (i) to increment
system time and (ii) to find the new
messages or IPCs, which the OS event
control blocks received from the system call
for the IPC functions,
 (iii) call the OS function as per the message
or system call

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 10


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
SysClkIntr service routine…
 (iv) Then OS either selects the same task or
selects new task or thread [by preemption in
case of preemptive scheduling and switches
the context to the new one, and
 Then after return from the interrupt the new
task runs from the code, which was blocked
from running

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 11


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. Examples of Timer functions

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS_TICK_PER_SEC
 # define OS_TICK_PER_SEC 100
/*COS-II function to define the number of
ticks per second = 100 before the beginning
of the main ( ) and the initiating the OS by
OSInit ( ) function*

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 13


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OSTickInit ( )
 OSTickInit ( ) /*COS-II function to
initiate the defined number of ticks per
second after the beginning of the first task
and creating all the tasks to which the
context will be switched by the OS on the
tick. It initiates SysClkIntr interrupts every
10 ms when number of ticks = 100 per s*/.

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 14


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OSTimeDelay (n
(n )

• OSTimeDelay (n ) by period equal to


period of n clock ticks
 OSTimeDelay (100);
/* Code for finding the coins amount after
every 100 clock ticks, which means every
1 s*/
;}

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 15


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OSTimeSet (Count) and OSTimeGet ()
 count =0; OSTimeSet (Count); while (count < =
6000) /* While loop waits for the coins amount
upto 60000 ms = 1 minute */
{Count = OSTimeGet ( );
 /*Calling OSTimeGet ( ), finding the count,
count1, running a section of codes and then calling
OSTimeGet ( ), finding the new count, count2
give us the interval, T spent by the system in
between the two function calls of OSTimeGet ( ).
T = (count2 – count1)  interval between two
clock ticks gives the interval*/
2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 16
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 17


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• SysClkIntr interrupts
• OS_TICK_PER_SEC
• OSTickInit ( )
• OSTimeDelay ( )
• OSTimeDelay-Resume ( )

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 18


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• OSTimeSet ( )
• OSTimeGet ( )
• OSSemPend (semVal, twait, *semErr)
• OSMboxPend (semVal, twait,
*mboxErr)

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 19


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 3 of Chapter 8

2008 Chapter-8 L3: "Embedded Systems - Architecture, Programming 20


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-4:
Event Functions

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. Event Functions

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Event Functions

 Wait for only one event (semaphore or


mailbox-message posting event)
 Event related OS functions can wait for
number of events before initiating an
action or wait for any of the predefined
set of events
 Events for wait can be from different
tasks or the ISRs

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 3


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Event functions at OS
 Some OSes support and some don’t support
event functions for a group of event

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 4


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OSEventCreate ( )
 Creates an event register, which has a
set of 8 or 16 or 32 flag-bits
 Event register is for defining the
information of occurrences of the
events in an event register
 Event register is for clearing the
defined occurrences of the events in an
event register
 Each bit corresponds to an event flag

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OS Event Register flag-
flag-bits
 Flag = 1 means occurrence of an event in an
event register
 Flag = 0 means an event in an event group
has not occurred
 A subset of flags defines an event-group
 For example, a 16-bit register can be
divided into four groups. Group 0 is from
bit 0 to bit 3, group 1 from bit 4 to bit 7,
Group 2 is from bit 8 to bit 11 and group 3
from bit 12 to bit 15
2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 6
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Event functions at OS…
 OSEventQuery ( )─ to query an event
register to find the event register existence
and its contents.
 OSEventDelete ( ) ─ delete the event
register
 SET (event_flag) to set one of the bits at the
event register
 Event flag in the register can be set by an
ISR or Task using SET (event_flag)
function
2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 7
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Event functions at OS…
 CLEAR (eventFlag) clears the flag in the
event register.
 An event flag can be cleared by an ISR or
Task
 WAIT_ALL function─ task waits for the
occurrences of setting all the event flags in
a group. [Wait till AND operation between
all flags in the group equals true.]

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 8


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Event functions at OS…
 WAIT_ANY function─ task waits for the
occurrences of setting at least one event flag
setting in a group. [Wait till OR operation
between all flags in the group equals true.]

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 10


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• Defining the occurrences of the events
in an event register
• Setting and clearing the event flags by
tasks and ISRs
• OSEventCreate ( ) and OSEventDelete
()
• OSEventQuery ( )
• SET ( ) and CLEAR ( )
• WAIT_ALL ( ) and WAIT_ANY ( )
2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 11
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 4 of Chapter 8

2008 Chapter-8 L4: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-5:
Memory Management Functions

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. Memory allocation

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory allocation

 When a process is created, the memory


manager allocates the memory addresses
(blocks) to it by mapping the process-
address space.
 Threads of a process share the memory
space of the process

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 3


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory Management after Initial
Allocation
 Memory manager of the OS─ secure, robust
and well protected.
 No memory leaks and stack overflows
 Memory leaks means attempts to write in
the memory block not allocated to a process
or data structure.
 Stack overflow means that the stack
exceeding the allocated memory block(s)

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 4


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory Managing Strategy for a system
 Fixed-blocks allocation

 Dynamic -blocks Allocation

 Dynamic Page-Allocation

 Dynamic Data memory Allocation

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory Managing Strategy for a system
 Dynamic address-relocation

 Multiprocessor Memory Allocation

 Memory Protection to OS functions

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. Memory allocation in RTOSes

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 7


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory allocation in RTOSes

 RTOS may disable the support to the


dynamic block allocation, MMU support to
dynamic page allocation and dynamic
binding as this increases the latency of
servicing the tasks and ISRs.

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 8


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory allocation in RTOSes

 RTOS may not support to memory


protection of the OS functions, as this
increases the latency of servicing the tasks
and ISRs.
 User functions are then can run in kernel
space and run like kernel functions

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory allocation in RTOSes

 RTOS may provide for disabling of the


support to memory protection among the
tasks as this increases the memory
requirement for each task

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 10


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3. Memory manager functions

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 11


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory Manager functions

 (i) use of memory address space by a


process,
 (ii) specific mechanisms to share the
memory space and
 (iii) specific mechanisms to restrict sharing
of a given memory space

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Memory Manager functions

 (iv) optimization of the access periods of a


memory by using an hierarchy of memory
(caches, primary and external secondary
magnetic and optical memories).
 Remember that the access periods are in the
following increasing order: caches, primary
and external secondary magnetic and then
or optical.

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 13


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4. Fragmentation Memory Allocation
Problems

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 14


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Fragmented not continuous memory
addresses in two blocks of a process
 Time is spent in first locating next free
memory address before allocating that to
the process.
 A standard memory allocation scheme is to
scan a linked list of indeterminate length to
find a suitable free memory block.
 When one allotted block of memory is de-
allocated, the time is spent in first locating
next allocated memory block before de-
allocating that to the process.
2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 15
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Fragmented not continuous memory
addresses in two blocks of a process
 The time for allocation and de-allocation of
the memory and blocks are variable (not
deterministic) when the block sizes are
variable and when the memory is
fragmented.
 In RTOS, this leads to unpredicatble task
performance

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 16


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
5. Memory management Example

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 17


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS COS
COS--II

 Memory partitioning
 A task must create a memory partition or
several memory partitions by using function
OSMemCreate ( )
 Then the task is permitted to use the
partition or partitions.
 A partition has several memory blocks.

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 18


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS COS
COS--II

 Task consists of several fixed size memory


blocks.
 The fixed size memory blocks allocation
and de-allocation time takes fixed time
(deterministic).
 OSMemGet ( )─ to provide a task a
memory block or blocks from the partition
 OSMemPut ( ) ─ to release a memory block
or blocks to the partition
2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 19
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 20


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• Memory manager allocates memory to the
processes and manages it with appropriate
protection.
• Static and dynamic allocations of memory.
• Manager optimizes the memory needs and
memory utilization

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 21


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• An RTOS may disable the support to the
dynamic block allocation, MMU support to
dynamic page allocation and dynamic
binding as this increases the latency of
servicing the tasks and ISRs.
• An RTOS may or may not support memory
protection in order to reduce the latency and
memory needs of the processes.
• An RTOS may provide of running user
functions in kernel space
2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 22
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 5 of Chapter 8

2008 Chapter-8 L5: "Embedded Systems - Architecture, Programming 23


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-6:
Device Management Functions

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. Device manager functions

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device Driver ISRs

 Number of device driver ISRs in a


system,
 Each device or device function having s a
separate driver, which is as per its
hardware

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 3


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device manager
 Software that manages the device drivers of
each device
 Provides and executes the modules for
managing the devices and their drivers
ISRs.
 Effectively operates and adopts appropriate
strategy for obtaining optimal performance
for the devices.
 Coordinates between application-process,
driver and device-controller.
2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 4
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device manager
 Process sends a request to the driver by an
interrupt; and the driver provides the actions
by executing an ISR.
 Device manager polls the requests at the
devices and the actions occur as per their
priorities.
 Manages IO Interrupts (requests) queues.

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device manager
 Creates an appropriate kernel interface and
API and that activates the control register
specific actions of the device. [Activates
device controller through the API and
kernel interface.]

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device manager

 Manages the physical as well as virtual


devices like the pipes and sockets through a
common strategy.

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 7


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device management has three standard
approaches
 Three types of device drivers:
 (i) Programmed I/Os by polling from each
device its the service need from each
device.
 Interrupt(s) from the device drivers device-
ISR and
 (iii) Device uses DMA operation used by
the devices to access the memory.
 Most common is the use of device driver
ISRs
2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 8
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device Manager Functions

 Device Detection and Addition


 Device Deletion
 Device Allocation and
 Registration
 Detaching and Deregistration

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device Manager Functions

 Restricting Device to a specific process


 Device Sharing
 Device control
 Device Access Management
 Device Buffer Management
 Device Queue, Circular-queue or blocks of
queues Management

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 10


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device Manager Functions

 Device drivers updating and upload of new


device-functions
 Backup and restoration

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 11


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Device Types

 char devices and


 block devices

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. Set of Command Functions for the
Device Management

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 13


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Commands for Device

 create
 open
 write
 read
 ioctl
 close and
 delete
2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 14
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
ioctl Command for Device

 (i) Accessing specific partition information


 (ii) Defining commands and control
functions of device registers
 (iii) IO channel control

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 15


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Three arguments in ioctl ( )
 First Argument: Defines the chosen device and
its function by passing as argument the device
descriptor (a number), for example, fd or sfd
Example is fd = 1 for read, fd = 2 for write.
 Second Argument: Defines the control option or
uses option for the IO device, for example, baud
rate or other parameter optional function
 Third Argument: Values needed by the defined
function are at the third argument

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 16


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Example

 Status = ioctl (fd, FIOBAUDRATE, 19200)


is an instruction in RTOS VxWorks.
 fd is the device descriptor (an integer
returned when the device is opened)
 FIOBAUDRATE is the function that takes
value = 19200 from the argument.
 This at configures the device for operation
at 19200-baud rate.

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 17


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3. Device Driver ISR functions

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 18


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
ISR functions

 intlock ( ) to disable device-interrupts


systems,
 intUnlock ( ) to enable device-interrupts,
 intConnect ( ) to connect a C function to an
interrupt vector
 Interrupt vector address for a device ISR
points to its specified C function.
 intContext ( ) finds whether interrupt is
called when an ISR was in execution
2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 19
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4. Unix OS functions

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 20


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
UNIX Device driver functions

 Facilitates that for devices and files have an


analogous implementation as far as
possible.
 open ( ),
 close ( ),
 read ( ),
 write ( ) functions analogous to a file open,
close, read and write functions.

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 21


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
APIs and kernel interfaces in BSD (Berkley
sockets for devices)
 open,

 close,

 read

 write

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 22


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
in--kernel commands
in
 (i) select ( ) to check whther read/write will
succeed and then select
 (ii) ioctl ( )
 (iii) stop ( ) to cancel the output activity
from the device.
 (iv) strategy ( ) to permit a block read or
write or character read or write

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 23


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 24


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• Device Manager initializes, controls, and
drives the physical devices and virtual
devices of the system.
• Main classes of devices are char devices
and block devices.
• Device driver functions may be similar to
file functions, open, read, lseek, write and
close

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 25


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 6 of Chapter 8

2008 Chapter-8 L6: "Embedded Systems - Architecture, Programming 26


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-7:
File System Organization and
Implementation

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. File

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
File

 a named entity on a magnetic disc, or


optical disc, or system memory or
memory stick
 contains has the data, characters and text

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 3


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OSes differing abstractions of a file
 i) A file may be a named entity that is a
structured record named entity as on a disk,
which has having random access in the
system.
 ii) May be a structured record on a RAM
analogous to a disk and may also be either
called separately called as ‘RAM disk’ or
simply ,simply, as a ‘file’ itself (virtual
device).
2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 4
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OSes differing abstractions of a file
 (iii) May be an unstructured record of bits
or bytes.
 (iv) A file device may be a pipe -like device

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. File organization in a file system

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
File organization in a system
• File is organized in a way according to a file
system, which has set of command
functions for operations on the file

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 7


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Standard set of interfaces

 Should a file having integers differ from a


file having bytes?
 Should a file having bytes differ from file
having characters?
 Due to the differing approaches to device
and file management interfaces, the
development of a set of standard interfaces
must.
 Then only then can systems can be portable.
2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 8
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Two type of file systems─
systems─ Block File
System
 Block File System. Its application generates
records to be saved into the memory.
 First structured into a suitable format and
then translated into block-streams.
 A file pointer (record) points to a block
from the start to the end of the file.

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Two type of file systems ─ Byte Stream File
System
 Byte Stream File-System. Its application
generates record stream.
 Stream are to be saved into the memory.
 These are first structured into a suitable
format and then translated into byte-stream.
 A file pointer (byte index) points to a byte
from the start index = 0 to N-1 in a file of N
bytes

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 10


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
POSIX

 A standard set of interfaces.


 File operations are as the operations on a
linear sequence of bytes

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 11


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
POSIX

 POSIX stands for Portable Operating


System Interface IEEE standard for coding
the programs when using the multiple
threads.
 The X after I is because of the interfaces
being similar to ones in UNIX like.
 It is according to the definitions at A T & T
UNIX System V Interface.

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Set of Command Functions in the
POSIX File System
 open ( )
 write ( ) in the byte stream at the present
pointer position
 read ( ) from the byte stream present pointer
position
 lseek (List seek) or set the file pointer in the
byte stream
 fcntl ( )
 close ( ) Chapter-8 L7: "Embedded Systems - Architecture, Programming
2008 13
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
UNIX and LINUX File Systems

 Unix has a structured file system with an


unstructured hardware interface.
 Linux supports different standard file-
systems for the system

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 14


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
File Device

 File devices are block devices in UNIX.


 Linux permits the use of a block devices as
a char devices also. This is because between
block-device to char-device, Linux has an
additional interface.
 Kernel interface is identical for the char and
block devices in Linux and not in UNIX.

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 15


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RAM disk

 File on the RAM that is hierarchically


organized is is known as RAM disk.
 RAM memory storage is analogous to that
on the disk and accessing is also analogous
to a disk.
 For example, path for accessing a file is
directory, then subdirectory, then folder and
then subfolder. There is hierarchical tree
like filing organisation
2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 16
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3. File descriptor

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 17


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
File descriptor─
descriptor─ fd

 An integer, which returns on opening a file


and which points to a data structure.

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 18


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
File describing data structure

 Identity
 Creator or Owner
 State
 Locks and Protection fields
 file Info
 Sharing Permission
 Count
 Storing Media Details
2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 19
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 20


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• File Manager has functions to create, open,
reads, seek a record, write, and close a file.
• A file has a file descriptor.

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 21


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 7 of Chapter 8

2008 Chapter-8 L7: "Embedded Systems - Architecture, Programming 22


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-8:
I/O Subsystems

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. I/O Subsystem

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
I/O ports

 Subsystems of OS device management


system─ UART access sub system and the
parallel port access sub system .
 They are used by drivers to communicate
with the many devices that use them
 I/O instructions depend on the hardware
platform.
 I/O systems differ in different OSes.

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 3


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
I/O Subsystem in a Typical I/O System
at in an OS
 Application
 IO Basic functions
 IO device driver functions
 Device Hardware or Port or IO Interface
card

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 4


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. I/O Operations

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
OSes differing IO operations
 Traditional OSes Synchronous IO
operations─ at a certain fixed data transfer
rates.
 RTOSes Asynchronous IO operations ─ at
the variable data transfer rates.

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Synchronous IO operations
 Are at a certain fixed data transfer rates.
 Therefore, a task (process) blocks tills till
completion of the IO.
 For example, a write function, write ( ) for 1
kB data transfer to a buffer.

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 7


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Synchronous IO

 Synchronous IO operation means once


synchronous IO initiates, the data transfer
will block the task till 1 kB data gets
transferred to the buffer.
 Similarly, read ( ) once initiated blocks the
task till 1 kB is read

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 8


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Asynchronous IO operations
 Variable data transfer rates.
 Permits that a process of high priority to run
and should not block during the IOs.

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. POSIX asynchronous functions for
IOs

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 10


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
POSIX asynchronous functions for
IOs
• aio_read ( )
• aio_write ( )
• aio_list ( )
• aio_error ( )
• aio_cancel,
• aio_suspend ( )─ Suspension is till the next
port-device interruption or till a timed out.
• aio_return ( ) returns the status of completed
operations.
2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 11
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• I/O subsystems are part of OS services.
• Examples are UART access and parallel
port access.
• Synchronous and asynchronous IOs.
• A task gets blocked during the synchronous
IOs, for example, fread ( ) or write ( ).
• RTOSes support asynchronous IOs, for
example, aio_read ( ) and aio_write also in
order to not to block a task during the IOs

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 13


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 8 of Chapter 8

2008 Chapter-8 L8: "Embedded Systems - Architecture, Programming 14


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-9:
INTERRUPT ROUTINES IN RTOS
ENVIRONMENT AND HANDLING
OF INTERRUPT SOURCE CALLS

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. INTERRUPT ROUTINES IN
RTOS

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
ISRs in RTOSes

 ISRs have the higher priorities over the RTOS


functions and the tasks.
 An ISR should not wait for a semaphore, mailbox
message or queue message
 An ISR should not also wait for mutex else it has
to wait for other critical section code to finish
before the critical codes in the ISR can run.
 Only the IPC accept function for these events
(semaphore, mailbox, queue) can be used, not the
post function
2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 3
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOSes

 Three alternative ways systems to respond


to hardware source calls from the interrupts

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 4


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
2. Direct Call to an ISR by an Interrupting
Source and ISR sending an ISR enter
message to OS

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 5


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Direct Call to an ISR

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Direct Call to an ISR and ISR Enter
message
 On an interrupt, the process running at
the CPU is interrupted
 ISR corresponding to that source starts
executing.
 A hardware source calls an ISR directly.
 The ISR just sends an ISR enter message
to the RTOS. ISR enter message is to
inform the RTOS that an ISR has taken
control of the CPU
2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 7
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
ISR IPC messages and Exit message
 ISR code can send into a mailbox or message
queue but the task waiting for a mailbox or
message queue does not start before the return
from the ISR
 When ISR finishes, it send s Exit message to OS
 On return from ISR by retrieving saved context,
The RTOS later on returns to the interrupted
process (task) or reschedules the processes (tasks).
 RTOS action depends on the event-messages,
whether the task waiting for the event message
from the ISR is a task of higher priority than the
interrupted task on the interrupt

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 8


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Event Message (IPC) from the ISR

 On certain RTOSes, there may be a function


OSISRSemPost ( ).

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Example in RTOS COS
COS--II

 Assume that a microcontroller timer


programmed to interrupt every 10 ms.
 Timer interrupt calls and program counter
changes to an ISR vector address,
ISR_Timer_Addr. At ISR_Timer_Addr,
there is a routine ISR_Timer for the
servicing the timer interrupt.
 ISR_Timer first executes OSIntEnter ( ) just
after the start of ISR_Timer
2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 10
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Example in RTOS COS
COS--II
 ISR_Timer then executes OSIntExit ( ) before the
return code.
 OSIntEnter ( ) sends the message to RTOS that
there should be return to the ISR only after any
system call is made by the ISR until the OSIntExit
( ) executes in the ISR code.
 Any task waiting for the post of semaphore or
mailbox message or queue message should not
start on execution of the post function within the
ISR or in any other task or ISR

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 11


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Multiple ISRs Nesting
 Each ISR low priority sends on high priority
interrupt the ISR interrupt message (ISM) to the
OS to facilitate return to it on the finishing and
return from the higher priority interrupt.
 Nesting means when an interrupt source call of
higher priority, for example, SysClkIntr occurs,
then the control is passed to higher priority and on
return from the higher priority the lower priority
ISR starts executing.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 12


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Multiple ISRs Nesting…

 Each ISR on letting a higher priority


interrupt call sends the ISM to the RTOS.
 Common stack for the ISR nested calls,
similar to the nested function calls.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 13


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
3. RTOS first interrupting on an interrupt,
then RTOS calling the corresponding ISR

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 14


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS first interrupting on an interrupt

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 15


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS interrupting on an interrupt
 On interrupt of a task, say, k-th task, the
RTOS first gets itself the hardware source
call and initiates the corresponding ISR
after saving the present processor status (or
context)
 Then the ISR during execution then can
post one or more outputs for the events and
messages into the mailboxes or queues.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 16


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS first interrupting on an interrupt
 The ISR must be short and it must simply
puts post the messages for another task.
 This task runs the remaining codes
whenever it is scheduled.
 RTOS schedules only the tasks (processes)
and switches the contexts between the tasks
only.
 ISR executes only during a temporary
suspension of a task.
2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 17
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Mobile PDA device example
 Each device event has the codes for an ISR, which
executes only on scheduling it by the RTOS and
provided an interrupt is pending for its service.
 Assume that using an RTOS, the touch screen
ISR, ISR_TouchScreen has been created using a
function OS_ISR_Create ( ).
 The ISR can share the memory heap with other
ISRs.
 A function, IntConnect connects the touch screen
event with the event identifier in an interrupt
handler, ISR_handler.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 18


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Mobile PDA device example…
 When a touch screen event occur on tap at
the screen to select icon or menu the OS
sends signal on behalf of the ISR_handler to
the waiting ISR_TouchScreen.
 ISR_TouchScreen runs on an interrupt call
message
 ISR_TouchScreen executes as per its
priority, IST_TouchScreenPriority among
the other pending ISRs before it starts
executing
2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 19
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Mobile PDA device example…

 Before return from the ISR_TouchScreen, it


sends a message to kernel using a function
OS_eventPost ( ) or OS_ISR_Exit ( ) just
before the end of the codes in the
ISR_TouchScreen

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 20


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
4. RTOS first interrupting on interrupt,
then RTOS calling the corresponding ISR,
the ISR sending messages to priority queue
of Interrupt Service threads by Temporary
Suspension of a scheduled Task

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 21


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS calling the corresponding ISR, the ISR
sending messages to priority queue of
Interrupt Service threads

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 22


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS calling the corresponding ISR, the
ISR sending messages to IST
 An RTOS can provide for two levels of
interrupt service routines, a fast level ISR,
FLISR and a slow level ISR (SLISR).
 The FLISR can also be called hardware
interrupt ISR and the SLISR as software
interrupt ISR.
 FLISR is called just the ISR in RTOS
Windows CE The SLISR is called interrupt
service thread (IST) in Windows CE.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 23


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
ISR sending messages to priority queue
of Interrupt Service threads
 The use of FLISR reduces the interrupt
latency (waiting period) for an interrupt
service and jitter (worst case and best case
latencies difference) for an interrupt service.
 An IST functions as deferred procedure call
(DPC) of the ISR. An i-th interrupt service
thread (IST) is a thread to service an i-th
interrupt source call.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 24


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Windows NT

 Priority level of interrupts always higher


than the user-thread priorities. [Threads are
also assigned priorities.]
 Only very critical operations of ISR are
performed and the remaining processing
passed to a DPC or IST.
 DPCs executes ass priority FIFOs.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 25


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Window NT

 Even very low priority task ISR and DPC


can preempt a real time process.
 DPCs run in FIFO mode. therefore, a DPC
of the very low priority ISR may not release
a resource. [For example, DPC of network
IO or keypad interrupt]

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 26


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Windows CE

 In Window CE provides for priority


inheritance by the IST.
 Interrupt service threads (equivalent of
DPCs) of high priority ISR will inherit the
priority of the ISR.
 Window CE provides hard real time
support.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 27


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Mac OS X─
X─ RTOS for iPod

 An interrupt handler first receives the


primary interrupt and then it then generates
a software interrupt known as a secondary
interrupt. The secondary software interrupt
using SWI is sent to initiate an IST
 There is three level strategy

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 28


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Mac OS X

 OS does not receive the actual interrupt but


the low level LISR intercepts the interrupt
directly.
 When LISR is called, it resets the pending
interrupt bit in the device interrupt
controller and calls a device-specific ISR,
say, DISRi.
 The DISRi posts a message to an ISTi
specific to the device.
2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 29
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Mac OS X

 The message notifies to ISTi that an


interrupt has occurred, and then the DISRi
returns to LISR.
 LISR resets another pending interrupt bit in
the device interrupt controller and calls the
another device-specific ISR, say, DISRj.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 30


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Mac OS X…

 When no further interrupts are pending, the


OS control returns to the currently
executing thread, which was interrupted and
when the OS passed control to the LISR.
 The IST i are scheduled by the OS, the IST
i finds that the SWI interrupt has occurred,
it starts and run the codes.
 ISTs run as if a thread is running

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 31


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 32


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• RTOS uses one of three strategies on
interrupt source calls
• (i) An ISR directly servicing directly , after
and ISR just merely informing the RTOS on
enter and sends exit information before
return.

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 33


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• (ii) Kernel intercepting the call and calling
the corresponding ISR and tasks.
• RTOS kernel schedules only the tasks
(processes) and ISR executes only during a
temporary suspension of the task by the
RTOS

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 34


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• (iii) Kernel intercepting the call and calling
the ISR, which initiates and queues the ISR
call into a priority FIFO. The RTOS kernel
schedules the ISTs and tasks (processes) as
per priorities
• In MAC OS X, there is hardware level
LISR, which calls device specific DISR,
which posts messages to ISTs

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 35


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 9 of Chapter 8

2008 Chapter-8 L9: "Embedded Systems - Architecture, Programming 36


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-10:
Real Time Operating System

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 1
Inc.
1. Real Time Operating System
Definition

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 2
Inc.
Real Time

 A real time is the time which continuously


increments at regular intervals after the start
of the system and time for all the activities
at difference instances take that time as a
reference in the system.

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 3
Inc.
RTOS

 A real time operating system (RTOS) is


multitasking operation system for the
applications with hard or soft real time
constraints

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 4
Inc.
RTOS

 Real-time constraint means constraint on


occurance of an event and system expected
response and latency to the event.

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 5
Inc.
Basic OS Functions

 Process Management,
 Resources Management,
 Device Management,
 I/O Devices subsystems
 Network Devices and subsystems
management

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 6
Inc.
Process Priorities Allocation
 User level priorities allocation, called static
priority allocation or real-time priority
allocation is permitted.
 The real time priorities are higher than the
dynamically allocated priorities to the OS
functions and the idle priority allotted to
low priority threads.
 The idle priority thread or task is one which
runs when no other high priority ones
running.
Chapter-8 L10: "Embedded Systems - Architecture,
2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 7
Inc.
Process Management by Preemption
 RTOS kernel preempts a lower priority
process when a message or event for that
was waiting is obtained for the higher
priority process.
 The RTOS kernel has the preemption points
at the end of the critical code and therefore
the RTOS can be preempted at those points
by a real time high priority task.
 Only small sections in the RTOS functions
are non-preemptive
Chapter-8 L10: "Embedded Systems - Architecture,
2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 8
Inc.
Process Priorities Management by priority
Inheritance
 Priorities Inheritance enables a shared
resource in low priority task, for example,
LCD display, be used by high priority task
first.
 An intermediate priority task will not
preempt the low priority task when it is
locked to run the critical shared resource or
code for the high priority task.

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 9
Inc.
Process Priorities Management by priority
Inheritance
 Priority sealing in place of priority
inheritance option can also be used for a
specific system.

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 10
Inc.
Process Predictability

 A predictable timing behavior of the system


and a predictable task-synchronization with
minimum jitter (difference between best
case latency and worst case latency)

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 11
Inc.
Memory Management by Protection

 In RTOS threads can run in kernel space.


 The real time performance becomes high.
However, then a thread can access the
kernel codes, stack and data memory space,
and this could lead to unprotected kernel
code

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 12
Inc.
Memory Management: Disabling MMU

 Either disabling use of MMU and virtual


memory or using memory locks.
 Memory locking stops page swapping
between physical memory and disk
disabled. This makes RTOS task latencies
predictable and reduces jitter (time between
worst case latency and best case latencies
for a task or thread).

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 13
Inc.
Memory Allocation

 In RTOS, the memory allocation is fast and


there are , fixed length memory block
allocationt and system takes predictable
time for allocation

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 14
Inc.
Scheduling and Interrupt-
Interrupt-latency control
functions
 Real time Task-Scheduling and Interrupt-
latency control and uses of the timers and
system clocks

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 15
Inc.
Timer Functions and Time Management

 Provides for timer functions. There is time


allocation and de-allocation to attain best
utilization efficiency in given timing
constraints

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 16
Inc.
Asynchronous IO Functions

 Permits asynchronous IOs, which means


IOs without blocking a task

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 17
Inc.
IPC Synchronization

 Synchronization of tasks with IPCs


(Semaphores, mailboxes, message queues,
pipes, sockets and RPCs)

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 18
Inc.
Spin Locks

 Spin locks for critical section handling

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 19
Inc.
Hard and soft real time operability

 Hard real-time and soft real-time operations

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 20
Inc.
Time Slicing

 Time-slicing of the processes execution of


those processes which have equal priority

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 21
Inc.
Summary

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 22
Inc.
We learnt
• RTOS is an OS for response time controlled
and event controlled processes. The
processes have predicable latencies and
execute by preemptive scheduling
• An RTOS is an OS for the systems having
the hard or soft real timing constraints and
deadline on the tasks, ISTs and ISRs

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 23
Inc.
End of Lesson 10 of Chapter 8

Chapter-8 L10: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 24
Inc.
REAL TIME OPERATING SYSTEMS

Lesson-11:
Basic Design using an Real Time
Operating System

2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 1


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
1. Basic Design

2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 2


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS

 An embedded system with a single CPU can


run only one process at an instance.
 The process at any instance may either be
an ISR, or kernel function or task

2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 3


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS
 Provides running the user threads in kernel
space so that they execute fast.
 Provides effective handling of the ISRs,
device drivers, ISTs, tasks or threads
 Disabling and enabling of interrupts in user
mode critical section code
 Provides memory allocation and de-
allocation functions in fixed time and
blocks of memory

2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 4


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS

 Provides for effectively scheduling and


running and blocking of the tasks in cases
of number of many tasks
 I/O Management with devices, files,
mailboxes, pipes and sockets becomes
simple using an RTOS
 provides for the uses of Semaphore (s) by
the tasks or for the shared resources
(cCritical sections) in a task or OS functions
2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 5
and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
RTOS

 Effective management of the multiple


states of the CPU and, internal and
external physical or virtual devices

2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 6


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
Summary

2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 7


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
We learnt
• RTOS is an OS for effective real time
scheduling of the concurrent processes
• User priority allocations
• Preemptive scheduling
• Fixed memory blocks allocation and de-
allocation
• IPCs

2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 8


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
End of Lesson 11 of Chapter 8

2008 Chapter-8 L11: "Embedded Systems - Architecture, Programming 9


and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc.
REAL TIME OPERATING SYSTEMS

Lesson-12:
Real Time Operating System based
System design Principles

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 1
Inc.
Design with the ISRs and Tasks
 The embedded system hardware source
calls generates interrupts
 ISR ─ only post (send) the messages
 Provides of nesting of ISRs, while tasks run
concurrently
 A task─ wait and take the messages (IPCs)
and post (send) the messages using the
system calls.
 A task or ISR should not call another task or
ISR Chapter-8 L12: "Embedded Systems - Architecture,
2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 2
Inc.
Each ISR design consisting of shorter code

 Since ISRs have higher priorities over the


tasks, the ISR code should be made short so
that the tasks don’t wait longer to execute.
 A design principle is that the ISR code
should be optimally short and the detailed
computations be given to an IST or task by
posting a message or parameters for that.
 The frequent posting of the messages by the
IPC functions from the ISRs be avoided
Chapter-8 L12: "Embedded Systems - Architecture,
2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 3
Inc.
Design with using Interrupt Service
Threads or Interrupt Service tasks
 In certain RTOSes, for servicing the
interrupts, there are two levels, fast level
ISRs and slow level ISTs, the priorities are
first for the ISRs, then for the ISTs and then
the task
 In certain RTOSes, three levels

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 4
Inc.
Design with using Interrupt Service
Threads
 ISRs post the messages for the ISTs and
do the detailed computations.
 If RTOS is providing for only one level,
then use the tasks as interrupt service
threads

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 5
Inc.
Design Each Task with an infinite loop
 Each task has a while loop which never
terminates.
 A task waits for an IPC or signal to start.
 The task, which gets the signal or takes the
IPC for which it is waiting, runs from the
point where it was blocked or preempted.
 In preemptive scheduler, the high priority
task can be delayed for some period to let
the low priority task execute
Chapter-8 L12: "Embedded Systems - Architecture,
2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 6
Inc.
Design in the form of tasks for the Better
and Predictable Response Time Control
 provide the control over the response time
of the different tasks.
 The different tasks are assigned different
priorities and those tasks which system
needs to execute with faster response are
separated out.

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 7
Inc.
Response Time Control

 For example, in mobile phone device there


is need for faster response to the phone call
receiving task then the user key input.
 In digital camera, the task for recording the
image needs faster response than the task
for down loading the image on computer
through USB port

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 8
Inc.
Design in the form of tasks Modular Design

 System of multiple tasks makes the design


modular.
 The tasks provide modular design

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 9
Inc.
Design in the form of tasks for Data
Encapsulation
 System of multiple tasks encapsulates the
code and data of one task from the other by
use of global variables in critical sections
getting exclusive access by mutex

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 10
Inc.
Design with taking care of the time spent in
the system calls
 expected time in general depends on the
specific target processor of the embedded
system and the memory access times.
 Some IPCs takes longer than the other

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 11
Inc.
Design with Limited number of tasks

 Limit the number of tasks and select the


appropriate number of tasks to increase
the response time to the tasks, better
control over shared resource and reduced
memory requirement for stacks
 The tasks, which share the data with
number of tasks, can be designed as one
single task.

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 12
Inc.
Use appropriate precedence assignment
strategy and Use Preemption
 Use appropriate precedence assignment
strategy and Use Preemption in place of
Time Slicing

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 13
Inc.
Avoid Task Deletion

 Create tasks at start-up only and avoid


creating and then deleting tasks later at the
later times.
 Deleting only advantage─ availability of
additional memory space

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 14
Inc.
Use CPU idle CPU time for internal
functions
 Read the internal queue.
 Manage the memory.
 Search for a free block of memory.

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 15
Inc.
Design with Memory Allocation and De-
De-
Allocation By the Task
 If memory allocation and de-allocation are done
by the task then the number of functions required
as the RTOS functions is reduced.
 This reduces the interrupt-latency periods. As
execution of these functions takes significant time
by the RTOS whenever the RTOS preempts a
task.
 Further, if fixed sized memory blocks are
allocated, then the predictability of time taken in
memory allocation is there
Chapter-8 L12: "Embedded Systems - Architecture,
2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 16
Inc.
Design with taking care of the Shared
Resource or Data among the Tasks
 The ISR coding should be as like a reentrant
function or should take care of problems
from the shared resources or data such as
buffer or global variables
 if possible, instead of disabling the
interrupts only the task-switching flag
changes should only be prevented. [It is by
using the semaphore.]

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 17
Inc.
Design with limited RTOS functions

 Use an RTOS, which can be configured.


RTOS provides during execution of the
codes enabling the limited RTOS functions.
For example, if queue and pipe functions
are not used during execution, then disable
these during run
 Scalable RTOS
 Hierarchical RTOS

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 18
Inc.
Use an RTOS, which provides for creation
of scalable code
 Only the needed functions include in the
executable files, and those functions of
kernel and RTOS not needed do not include
in the executable files

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 19
Inc.
Use an RTOS, which is hierarchical

 The needed functions extended and


interfaced with the functionalities
 Configured with specific processor and
devices

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 20
Inc.
Encapsulation Using the Semaphores

 Semaphores, queues, and messages should


not be not globally shared variables, and let
each should one be shared between a set of
tasks only and encapsulated from the rest
 A semaphore encapsulates the data during a
critical section or encapsulates a buffer
from reading task or writing into the buffer
by multiple tasks concurrently

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 21
Inc.
Encapsulation Using Queues
 A queue can be used to encapsulate the messages
to a task at an instance from the multiple tasks.
 Assume that a display task is posted a menu for
display on a touch screen in a PDA
 Multiple tasks can post the messages into the
queue for display.
 When one task is posting the messages and these
messages are displayed, another task should be
blocked from posting the messages.
 We can write a task, which takes the input
messages from other tasks and posts these
messages to the display task only after querying
whether the queue is empty
Chapter-8 L12: "Embedded Systems - Architecture,
2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 22
Inc.
Summary

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 23
Inc.
We learnt
• There are certain design principles observed
when using the RTOS

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 24
Inc.
End of Lesson 12 of Chapter 8

Chapter-8 L12: "Embedded Systems - Architecture,


2008 Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, 25
Inc.

Вам также может понравиться