Module 4
EM BED D ED FIRM WARE & OPERATING SYSTEM CONCEPTS
Introduction to embedded firmware & operating systems: Boot loader -Realtime kernel-Embedded OS-
Tasks, Processes and Threads, Multiprocessing and Multitasking, Task scheduling, Task communication and
synchronisation, Device Drivers.
Slides courtesy:
Dr Lyla Das
A PC wakes up…. warm /cold Boot
2
2
Functional Layers in a computing system
3
• Application programs like
games- developed by User
• Utilities are programs
API
present to support ancillary
activities [like compilers,
linkers..]-Developed by
system programmer
• OS – program that shields the
Device Drivers user from having to know
anything about the underlying
hardware – developed by an
expert system programmer
• STANDALONE
• NETWORK OS
• EMBEDDED os
3
Functions of an OS
4
4
Functions of an OS
5
5
Memory Management System
6
Memory management system -
take care of this heirarchy
6
Boot Loader
Once POST ( power on self test) and other
hardware initialization steps are done by BIOS,
system is ready to run the SOFTWARE BOOTLOADERS in Embedded
OS is in secondary memory ( due to ?) System?
Software responsible for loading OS core (kernel) They also do the same thing
onto main memory
A simple boot loader could be just few “Assembly
But
instructions” to copy OS code from disk to RAM Loads Application program
complex ones allow us to select an OS
Boot loaders are hardware platform dependant
They may allow code to be
transferred via interfaces like serial
port or JTAG
8
And ….
User interface Portable OS interface
GUI POSIX
Command line IEEE std (UNIX originally)
Important in Embedded
applications
API • large variety of
Simplify the process of building applications
• Many variants of OSes in
software applications
filed
One can get services of OS
9
What is an OS?
10
OS is the manager of the system
It manages
physical resources ( mouse, memory etc)
&
logical resources ( file, variables)
User friendly environment is offered for the end user
It “Hides” the complex hardware from the end user /
application program --- “Abstraction – at each level”
10
What is an OS?
11
General Purpose OS
It is software on top of
which the application Network OS
software runs
Embedded OS
→ System Software
Hence: compatibility issues ☺
Like DataBit length issues
11
Functions Performed By An OS {the manager}
12
Process Management
Memory Management
IO Management
File Management
Multiprogramming
Protection and Security
Network Management
12
DEVICE DRIVERS
13
13
Firmware? { Ascher Opler}
14
Firmware is the
Responsible for controlling
Software which is
the program sent the various peripherals and
/set/that sit inside for generating the desired
ROM in the response in accordance with
embedded module
the functional specification
A code that provides a low
level (?) control for the given by the customer /
device’s specific hardware
market demands
14
Firmware?
15
Embedded Firmware is the “master brain/
intelligence” of the embedded system
Once intelligence is imparted to Hardware end
user cannot alter it
[ but bug fixing and updating are cases where
firm ware is altered]
A new emergence :firmware security
15
Firmware?
16
Firmware developer need to understand the
application desired, components used in
hardware, memory map and a programming
language {assembly /C/ Java }
16
Firmware vs ?
17
Hardware Is Physical: It's "Real," Sometimes Breaks, and
Eventually Wears Out
Software Is Virtual: It Can Be Copied, Changed, and Destroyed
Firmware Is Virtual: It's Software Specifically Designed for a
Piece of Hardware
## almost all electronic devices contain firmware
17
KERNEL
18
The KERNEL is the innermost part or core
of an operating system, and it is that
which provides the basic services for all
other parts of the system
The kernel is sometimes referred to as the
supervisor, core or internals of the
operating system
Level 1- less important part of system software
Level2 –software utilities
Level 3 – application pgms
18
KERNEL -- Ref LYLA DAS
19
Kernel is the first part of OS that gets loaded first into primary memory
Its stays there through out the session
It has the highest privilege status
It has full access to hardware
Kernel of ev ery OS is unique and cannot migrate from OS to OS
It has a protected memory space -
Applications have access to Kernel through “System Call” interface
Em bedded OS Kernals can be loaded only to appropriate H/W with proper configuration settings
Ty pes
Monolithic kernel
Microkernels - has servers
19
20
FIRMWARE – Super Loop based approach
22
For embedded systems that are not time critical
Tasks - Missing deadlines are acceptable –
eg : Games -- missing a keystroke
The code is executed Task by Task in a predetermined order
If any task hangs, entire code hangs – solved using s/w or h/w
Watch Dog timers
The complete code kept in infinite loop.. After nth task, task 1
taken up again.
Ref 9.1.1 of Shibu K V to see a sample Embedded C code
Do not need OS as there is no need to decide which task is to
be done first – while application is executing
Simple and straight forward as it is without the OS overhead
22
FIRMWARE – Embedded OS based approach
23
The code is executed based on Task scheduling
algorithms to maximise the resource utilization
Tasks are executed under OS supervision
OS supported applications will also have “DEVICE
DRIVER SOFTWARE” for different hardware on the
system
Types- GPOS , RTOS
23
FIRMWARE – Embedded OS based approach
24
GPOS
GPOS is used for designs similar to conventional computing machines
ex: patient monitoring systems
Kernal is general and contains all general services
API (Application Programming interface) is provided as the
interface between OS and the applications built on top of
OS
API offers functions, routines, protocols needed to simplify
the END user experience in target code development
Ex:To use ADC in the system, ADC () function is provided
as an API
Ex: Windows Embedded 8.1
24
FIRMWARE – Embedded OS based approach
25
RTOS
RT is debatable
Deterministic behavior is the key
OS must consume only known / expected amount of time regardless of
task → PREDICTABLE BEHAVIOR
Time critical applications governed by strict policies to avoid conflicts
Ex: Windows Embedded compact, VxWorks
Basic functions Process management, scheduling, synchronization,
Error/Exception handling, Interrupt handling, Time management,
Memory management
More a bit later { Ref Shibu K V chap 10 p 394}
25
RTOS – how does it handle time critical systems?
26
Assign priority to tasks when tasks enter ready Queue
Schedule execution based on the task’s priority
Dynamically change priority on a need basis
Switch task or preempt the task when it waits for external event or a
system shared resource allocation
All this done by the Real – time OS kernel called as RT kernel
RT Kernal is more sophisticated but contains only those basic functions
desired for that specific application
26
30
Tasks/Processes and Threads
30
Tasks/Processes
31
The activity of a computer/ Embedded system is to
‘run/execute’ programs.
A program in execution is formally defined as a process
Set of computations processed on a Processor under the
control of the scheduling kernel of an operating system
Tasks are generic computational blocks with
Timing constraints
Synchronization and communication relationships with other tasks
The term “task”, “job”, “ process ” refers to the same entity
31
Tasks/Processes
32
Instance of program in execution.
Multiple process can be executed simultaneously
by quickly switching from one process to the
other.
Sequential in execution within a process
Process executes concurrently
32
Structure of Process
33
Stack- local variables and code associated with
process
PC- next executable instruction
Process
Stack
Working registers
Status Registers
PC
33
Memory Organisation of Processes
34
Stack Memory
Stack Memory grows
downwards
Data memory grows upwards
Data memory
Code memory
34
The FIVE states of a Process/Task
35
The five
states are
named as
New(create)
Ready
Running
Blocked
Exit.
35
TASK/PROCESS - State Transitions
36
New/Created state – the
state at which a process is
being created.
Ready state – state where
process is incepted into
memory and awaiting the
processor time for
execution.
Process is placed in ready list
queue maintained by OS
Resources are attached
Running State – state
where source code
corresponding to the
process is executed.
36
TASK/PROCESS - State Transitions -possibilities
37
Blocked/ wait
state – state where
running process is pushed
into - thus temporarily
suspended from execution
and will not have
immediate access to
resources …… this is called
as “Pre-emption”
Exit/Completed
State – process
completes its
execution [ either
successful completion or
abnormal termination]
37
Process state and state transition
Creation to its
termination is not
single step operation.
The cycle through which Created
process changes its Incepted into
states is memory
product life cycle Blocked Ready Scheduled for
States – execution
Created
Ready Running
Running
Execution
Blocked/ wait Completion
completed Completed
38
TASK/PROCESS - State Transitions -possibilities
39
39
Process/Task Control block (PCB)
40
Data
structure
maintained by
OS to control
process states.
Architecture is
dependent on
OS
40
Process/Task Control block (PCB)
41
Data structure maintained by OS to control process states.
Architecture is dependent on OS
• Process ID-
PCB – Unique identification for each of the process in operating system
Process ID • Process states –
– Current state of process – ready ,running , waiting
State • Pointer –
– Pointer to parent process
Pointer • Program counter-
– Pointer to the address of next instruction to be executed in process.
Priority • CPU reg-
– Registers used by task and its values
Program counter
• I/O status information –
CPU registers – List of i/o resources allocated to the process.
• Accounting info-
I/O information – Amount of CPU used for process execution
• Scheduling information
Accounting info • Priority information about the task
41
Threads
42
A thread is sometimes
called a ‘lightweight
process’
A tasks needs a number of
subsidiary activities Each
of these subsidiary
activities are handled as
separate threads
Each thread owns its status
42
Threads
43
Definition→Is the single sequential Stack Memory for thread 1
flow of execution through the process
Stack Memory for thread 2
code
Each thread belongs to exactly 1 :
:
process and no thread can exist
:
outside a process
Data memory for process
Types of thread:
User level threads Code memory for process
Kernel level threads
Memory Organisation for Threads
43
Threads and Mutlithreading concept
44
If all subfunctions in a task are executed sequentially, processor utilisation may not be 100%
So instead of single sequential execution of task/process, it is split into different smaller sequential pieces
carrying out relatively independent subfunctionalities
When one thread enter a wait state another thread can be queued up and avoid processor stalling - So ??
????
Compromise?????? : Management overhead
44
Thread Pre-emption
45
Is the act of pre empting currently running thread temporarily
The procedure depends on whether thread is user level or kernel level
45
Kernal Level Threads
46
Features
If one thread is blocked , kernel can schedule another thread of same process
Slower to create and manage
Thread management done by kernel
Kernel threads are Supported directly by OS
Pre-emptive
Kernel performs
Creation
Scheduling
management
46
User Level Threads
47
Features:
Does not require kernel mode privileges
Run on any OS
Scheduling can be application specific
Fast to create and manage
User managed threads
Kernel is not aware of the existence of threads
i.e even if there are multiple threads in user task, KERNAL sees it as a single thread only
No OS intervention needed for thread switching
Thread library contains code for-
Creating, destroying
Passing message and data between threads
Scheduling thread execution
Saving and restoring thread contexts.
47
Threads.. continued
48
User level threads Kernel level threads
Faster to create and manage Slower to create and manage
Implementation is by thread library OS supports creation of kernel threads
Generic and run on any OS Specific to the OS
Multi-threaded applications cannot Kernel routines themselves can be
take advantage of multiprocessing.. As multithreaded
KERNAL is ignorant of user thread
existence in general
48
Multithreading models
49
Some OS provide combined user level thread and kernel
level thread facility.
In a combined system, multiple threads within the same
process can run in parallel on multiple processors
The binding of user level threads to Kernal level threads
follow different approaches or model
3 types of modelling
Many – to – many
Many – to – one
One – to –one
49
Multithreading models---- Many – to – many
50
User level threads allowed to be mapped to many kernel threads
This model provides Best accuracy on concurrency
Switching happens when one thread voluntarily relinquishes the processor
When one thread performs a blocking call, kernel can schedule another
thread for execution.
50
Multithreading models---- Many – to – One
51
Many user level threads are mapped to single kernel thread
Thread management is done by thread library
When thread makes a blocking system call, entire process will be
blocked.
Only one thread can access kernel at a time, so multiple threads are
unable to run in parallel.
51
Multithreading models---- One– to – One
52
One thread in user to one thread in kernel
More concurrency than many- to- one model
Allows another thread to run when a thread makes
blocking system call
Supports multiple threads to execute in parallel
52
Process & Threads
53
Process Thread
Process is a program in execution and Thread is a single unit of execution and part of
contains one or more threads process
Process has it own code memory, data Thread does not have it own code memory, data
memory and stack memory- It shares with other threads of the same
process . But usually maintains a stack of its own
A process contain at least one thread A thread cannot exist independently .. It lives
within process
Process is expensive to create .. Involves Thread is inexpensive to create….in general
OS overhead
Process switching is complex , slow and Thread context switching is fast and relatively
relatively expensive. inexpensive
If a process dies, resources go back to OS If a thread expires, resources are reclaimed by
and all associated threads also die process
53
Process & Threads
54
54
What is…Multitasking and Multiprocessing?
55
Multiprocessing refers to the ability to execute multiple processes
simultaneously
and it must have { ideally} multiple processors in its hardware
In UNI processor scenario what we claim is only pseudo parallelism
So ideal multiprocessing systems are in general multiprocessor as well
At OS level, the ability of OS to handle multiple processes by fast switching
between different processes is Multitasking
So every process has a virtual processor derived from physical processor on a time shared basis
under the supervision of OS scheduler
55
What is…
56
Multitasking?
How does multitasking affect the user?
Task (Process) scheduling?
56
What is…Multitasking?
57
One processor - but multiple tasks awaiting its service
So NO one task should be allowed to monopolize the processor
Psuedo Parallelism by time sharing – each task gets its time slice/
chunk and then task switching is done to relinquish its claim over
resources
TCB has the task context {information} to help pick up when a
relinquished task gets its next turn.
Overhead for OS – switching from one task to another – by saving
the current task’s status
57
How does multitasking affect the user?
58
Multitasking is time multiplexing effect at a high rate
User is unaware of task switching happening at
processor level
User - made to believe – that all tasks are going on
in parallel –pseudo parallelism
As number of tasks increase,
“system will slowdown” →disadv
{nothing comes free}
58
Task (Process) scheduling?
59
Role of Scheduler is to give the resources to a requesting
task, ensuring zero wastage of resources and processor
time
Policy adopted must ensure that all tasks/processes are
given proper attention
59
CPU and I/O bound tasks
60
60
61
61
Selection of a Scheduling Algorithm
62
Some aspects
• CPU utilization
• Only one processor and it should not idle
• Response time
• Interval for which a task waits to be taken up for execution by the processor
• Must be minimum
• Turnaround time (TAT)
• Interval between the instant task is presented to the system to the instant it exits
• TAT = Twait in Ready Queue +T service ( including blocked time)
• Throughput rate
• Number of tasks processed in unit time
• Inverse of TAT
62
Scheduling Algorithms -Assumptions
63
63
Scheduling Algorithms -Types
64
Scheduling
Non Pre-
Pre-emptive
emptive
Shortest Job Pre-emptive
Co operative Priority Based Round robin Pre-emptive
Next SJN Priority
SJN/Shortest
Remaining
Time (STR)
64
Non-preemptive Methods of Scheduling
65
Co-operative Scheduling
Each task is allowed to execute to its finish,
then only the next one is taken up.
While one task executes, the others are willing to
wait and this gives the name ‘co-operative’ to the
scheduling algorithm.
This is also a case of first come first serve scheduling
/ a first in first out scheme (FCFS/FIFO) or last come
first serve scheduling / a last in first out scheme
(LCFS/LIFO)
65
Non-preemptive Methods of Scheduling
66
Non-preemptive Methods of Scheduling
T1 300
FCFS
T2 125
Solution
T3 400
67
T4 150
T5 100
67
Non-preemptive Methods of Scheduling
Shortest Job Next (SJN) /Shortest Job First
68
Queue the tasks such that the one with the shortest
service time gets to execute first
Disadv:
Usually the execution times of tasks are not known in
advance.
The method then is to estimate the service times by
using the recent history of each of the tasks
Additional overhead of past history tracking
68
Non-preemptive Methods of Scheduling
69
Non-preemptive Methods of Scheduling
T1 300
T2 125
T3 400
T4 150
T5 100
70
Non-preemptive Methods of Scheduling
T1 300
T2 125
T3 400
T4 150
T5 100
T6 250
T7 200
71
Non-preemptive Methods of Scheduling
T1 300
Solution T2 125
T3 400
T4 150
T5 100
T6 250
T7 200
72
Non-preemptive Methods of Scheduling
Priority-based Scheduling
73
In such a system, tasks have priorities which are
represented in terms of numbers .
The convention is to have 0 to 255 as the numbers
representing priorities, with 0 representing the
highest priority, and higher numbers indicating
lower priorities .
73
Non-preemptive Methods of Scheduling
74
Non-preemptive Methods of Scheduling
Solution T1
T2
300
125
5
3
T3 400 2
T4 150 12
T5 100 15
75
Non-preemptive Methods of Scheduling
76
Non-preemptive Methods of Scheduling
T1 300 5
T2 125 3
T3 400 2
T4 150 12
T5 100 15
T6 120 0
T7 80 4
T8 3500 6
77
Pre-emptive Scheduling Strategies
78
In this set, when a task is running (i.e. using the
processor), if a new one arrives, it is pushed out from
the ‘running’ state to the ‘ready’ state and NEW task
from the ready queue is taken up
Task that has been removed and replaced is said to
be pre-empted
Various strategies are →…….
78
Preemptive Methods of Scheduling
1. Round Robin Scheduling
79
Here, time slices are defined.
Suppose there are n tasks in a system, each of them
is allowed to execute for a period equal to the time
slice.
After this, the next task gets its turn, but can use the
CPU only for a time equal to the defined time slice.
This is also called a time sharing or time slice system
79
Preemptive Methods of Scheduling
80
Preemptive Methods of Scheduling
T1 150
T2 100
T3 200
T4 50
81
Preemptive Methods of Scheduling
82
Preemptive Methods of Scheduling
T1 300
T2 125
T3 400
T4 150
T5 100
Proper processor time utilisation cannot be guaranteed
83
Preemptive Methods of Scheduling
2.Pre-emptive Priority
84
In this method, at any time it should be ensured that
it is the highest priority task in the ready queue that
should be running.
If, at any time, a task with a higher priority than the
one that is currently running enters the ready queue,
the current task is pre-empted and the new one
is taken up.
84
Preemptive Methods of Scheduling
85
Preemptive Methods of Scheduling
T1 300 5
Solution T2 125 3
T3 400 2
T4 150 12
T5 100 15
At time 600 →
T6* 75 1
86
Preemptive Methods of Scheduling
3.Pre-emptive SJN/Shortest Remaining
Time (SRT)
87
Normally the scheduling is in the order of increasing
service times. Thus, tasks with the shortest service times are executed earlier.
In the midst of a schedule, if a new task enters the ready queue, its service time is compared
with the remaining service time of the currently executing task
If the new task has a shorter service time than the remaining time of the current task, it (the
current task) is
pre-empted and the new one serviced until it completes.
87
Preemptive Methods of Scheduling
88
Preemptive Methods of Scheduling
Solution T1 300
T2 125
T3 400
T4 150
At time 150 → T5 100
T6 50
89
END of presentation
90