RTOS For Embedded System Design: Mr. Anand H. D
RTOS For Embedded System Design: Mr. Anand H. D
Mr. Anand H. D.
SYLLABUS:
Operating System basics
Task scheduling
Operating System (OS) acts as a bridge between user applications/tasks and the
underlying system resources through a set of system functionalities and services.
The OS manages the system resources and makes them available to the user
applications/tasks on a need basis .
The Kernel:
➢ Is the core of OS and is responsible for managing the system resources and the
communication among the hardware and other system services.
➢ Acts as the abstraction layer between system resources and user applications.
Process Management:
•Deals with managing the processes/tasks.
•By setting up the memory space for the process, loading the
process’s code into the memory space, allocating system
resources, scheduling and managing execution of process,
setting up and managing the Process Control Block (PCB),
Inter-process Communication and synchronization, process
termination/deletion etc.
Time Management:
Deals with at what time which process needs to be scheduled.
Till what duration processes needs to be executed.
After what time a process needs to be preempted.
Time Management:
Deals with at what time which process needs to be scheduled.
Till what duration processes needs to be executed.
After what time a process needs to be preempted.
Interrupt Handler:
➢Kernel provides handler mechanism for all external/internal interrupts generated by the system.
Microkernel
➢Incorporates only essential set of OS services into the
kernel and rest of OS services are implemented in programs
known as Servers which run in user space.
➢Provides a highly modular design and OS-neutral
abstraction to the kernel.
➢Memory management, process management, time system
and interrupt handlers are essential services which forms the
part of microkernel.
➢Mach, QNX, MINIX3 are examples of microkernel OS.
➢Offers 2 benefits:
•Robustness: server programs can be reconfigured &
restarted, chances of corruption are ideally zero.
•Configurability: any server application can be changed
without restarting the entire system—dynamically
The Microkernel Model
configurable
RTOS for Embedded System Design
Depending on kernel and kernel services, purpose and type of computing systems where OS is deployed and
responsiveness to applications, OSs are classified into 2 types:
General Purpose Operating System (GPOS)
Real-Time Operating System (RTOS)
for running the user applications/tasks. •Task context pointer •Task memory pointers
•Task system resource pointers •Task pointers.
➢The basic functions of Real Time Kernel
➢Implementation of TCB is kernel dependent.
are: ➢Task management service uses TCB of a task in following way:
•Creates a TCB for a task on creating a task.
•Task/Process management.
•Delete/remove TCB of task when task is terminated/deleted
•Task/Process scheduling. •Reads TCB to get the state of task.
•Update TCB with updated parameters on need basis.
•Task/Process synchronization.
•Modify TCB to change task priority dynamically.
•Error/Exception Handling.
•Memory management.
•Interrupt Handling.
•Time Management
for running the user applications/tasks. •Task context pointer •Task memory pointers
•Task system resource pointers •Task pointers.
➢The basic functions of Real Time Kernel
➢Implementation of TCB is kernel dependent.
are: ➢Task management service uses TCB of a task in following way:
•Creates a TCB for a task on creating a task.
•Task/Process management.
•Delete/remove TCB of task when task is terminated/deleted
•Task/Process scheduling. •Reads TCB to get the state of task.
•Update TCB with updated parameters on need basis.
•Task/Process synchronization.
•Modify TCB to change task priority dynamically.
•Error/Exception Handling.
•Memory management. Task/Process scheduling:
➢Deals with sharing CPU among various tasks/processes.
•Interrupt Handling. ➢A kernel service called ‘Scheduler’ handles task scheduling.
•Time Management ➢Scheduler is nothing but an algorithm implementation which performs the
efficient and optimal scheduling of task to provide a deterministic behavior.
➢The basic functions of Real Time Kernel ➢If timer tick interval is 1 millisecond, system time register will reset in
are:
•Task/Process management.
➢This timer tick can be utilized for:
•Task/Process scheduling. •Save the current context.
•Task/Process synchronization. •Increment the timer register by one.
•Update the timers implemented in kernel.
•Error/Exception Handling. •Activate the periodic tasks, which are in idle state.
•Memory management. •Invoke the scheduler and schedule the tasks again based on scheduling
algorithm.
•Interrupt Handling.
•Delete all terminated tasks and associated data structures (TCBs).
•Time Management •Load the context for first task in the ready queue.
Hard Real Time: ➢Ideally speaking the response time of air bag system
➢RTOSs that strictly adhere to the timing constraints must be zero and bags should be deployed exactly
for a task are referred as ‘Hard Real Time’ systems. within the time frame which is predefined.
➢Hard real time system must meet the deadlines for a ➢Any delay in deployment of air-bags makes the life
task without any slippage. Missing any deadlines may of passengers under threat.
produce catastrophic results for Hard real time systems ➢When the airbag deployment task is triggered, the
including permanent data lose and irrecoverable currently executing task must be preempted, airbag
damages to the system/users. deployment task must be brought into execution and
➢Emphasizes on ‘ a late answer is a wrong answer’ . necessary I/O systems should be made readily available
➢Air bag control system and Anti-lock Braking ➢As a Thumb rule hard real time systems doesn’t
System (ABS) are typical examples. implement virtual memory model. This eliminates the
➢The airbag control system must be into action and delay in swapping in and out the code.
deploy the airbags when vehicles meet severe ➢Most of the Hard Real Time systems are automatic
accident. and does not contain a ‘human in the loop (HITL)’.
➢In OS context a ‘task’ is defined as the program in execution and the related information maintained by the OS
for the program.
➢A task is also known as ‘job’ in OS context.
➢A program or part of it in execution is also called a ‘Process’
➢The terms ‘task’, ‘job’ and ‘process’ refer to the same entity in the OS context and most often they are used
interchangeably.
Process:
➢Also known as an instance of program in execution.
➢Multiple instances of the same program can execute simultaneously.
➢A process requires various system resources like CPU for executing the process, memory for storing the code
corresponding to the process and associated variables, I/O devices for information exchange etc.
➢A process is sequential in execution.
Thread Standard:
➢Thread standard deals with different standards available for thread creation and management.
➢These standards utilize OS for thread creation and management. It is a set of thread class libraries.
➢The commonly available thread class libraries are POSIX threads, Win32 Threads, Java Threads etc.
POSIX Threads:
➢Stands for Portable Operating System Interface.
➢POSIX. 4 standard deals with real time extensions and POSIX.4a standard deals with thread extensions.
➢The POSIX standard library for thread creation and management is ‘Pthreads’.
➢The Pthreads library defines the set of POSIX thread creation and management functions in ‘C’ language.
➢The above primitive is used for creating new thread for running the function start_function.
➢Here pthread_t is the handle to newly created thread and pthread_attr_t is data type for holding thread attributes.
➢Start_function is the function, the thread is going to execute, arguments is the arguments for start_function
➢On successful creation of a Pthread, Pthread_create() associates the TCB corresponding to the newly created thread to
variable of type Pthread_t
RTOS for Embedded System Design
➢The above primitive blocks the current thread and waits until the completion of the thread pointed by it.
➢All POSIX thread calls return an integer.
➢A return value of zero indicates the success of the call. It is always good to check the return value of each call.
Example:
Write a multithread appliction to print “Hello I’m in main thread” from the main thread and “Hello I’m in new
thread” 5 times each, using pthread_create() and pthread_join() POSIX primitives.
Thread Process
Thread is a single unit of execution and is part of Process is a program in execution and contains one or
process. more threads.
Thread does not have its own data memory and Process has its own code memory, data memory and stack
heap memory. It shares the data memory and memory
heap memory with other threads of same process.
Thread cannot live independently: it lives within Process contains at least one thread
the process.
There can be multiple threads in a process. The Threads within a Process shares data, code and heap
first thread calls the main function and occupies memory. Each thread holds separate memory area for
the start of stack memory of the process. stack.(Share the total stack memory of process)
Threads are very inexpensive to create. Process are very expensive to create as they involve many
OS overhead.
Context switching is inexpensive. and fast Context switching is complex and involves lot of OS
overhead and comparatively slower.
If a Thread expires, its stack is reclaimed by the If a process dies, the resources allocated to it reclaimed by
process. OS and all associated threads of the process also dies.
RTOS for Embedded System Design
Time
RTOS for Embedded System Design
running
idle
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
running
idle
Process 2
Process 1
Time
RTOS for Embedded System Design
Types of Multitasking:
Co-operative Multitasking:
➢Most primitive form of multitasking, in which a task/process gets a chance to execute only when currently
executing voluntarily relinquishes the CPU.
➢Any task/process can hold CPU as much time as it wants.
➢This name because, this type of implementation involves mercy of tasks for each other to get CPU time for
execution.
➢If tasks are non-cooperative, the other tasks have to wait for a long time to get the CPU.
Preemptive Multitasking:
➢Ensures that every task/process gets a chance to execute.
➢When and how much time a process gets is dependent on the implementation of preemptive scheduling.
➢The currently running task/process is preempted to give a chance to other tasks/processes to execute.
➢Preemption of task may be based on time slots or task/process priority.
Non-Preemptive Multitasking:
➢The task/process to which CPU is currently allocated is allocated to execute until it terminates or enters
‘Blocked/wait’ state, waiting for I/O or system resource.
➢In cooperative multitasking process/task will not relinquish CPU when it enters s ‘Blocked/wait’ state.
➢Where as in Non-preemptive multitasking currently executing process/task relinquish CPU when it waits for an
I/O or system resource or an event to occur.
➢Determining which task/process is to be executed at given point if time is known as Task Scheduling.
➢Scheduling polices form the guidelines for determining which task is to be executed when.
➢The process scheduling decision may take place under 4 scenarios, when a process switches its state to:
•Ready’ State from ‘Running’ State — Scenario 1
•‘Blocked/Wait’ State from ‘Running’ State — Scenario 2
•‘Ready’ State from ‘Blocked/Wait’ State — Scenario 3
•‘Completed’ State — Scenario 4
Scenario 1: A process switches to ‘Ready’ state from ‘Running’ state when its preempted.— Preemptive Scheduling.
Scheduling in Scenario 2 can be either preemptive or non-preemptive.
Scenario 3: When a high priority process in Blocked/Wait state , the scheduler picks it for execution if scheduling
policy is priority based preemptive.
Scheduling under Scenario 4 can be preemptive, non-preemptive or cooperative scheduling.
RTOS for Embedded System Design
Therefore a good scheduling algorithm should have high CPU utilization, minimum Turn Around Time
(TAT), maximum throughput and least response time.
➢The OS maintains various queues in connection with CPU scheduling and a process passes through these queues
during the course of admittance to execution completion.
•Job queue: contains all processes in the system.
•Ready queue: contains all processes which are ready for execution and waiting for CPU. Empty when no
process is ready for running.
•Device queue contains processes, which are waiting for an I/O device
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Now let us consider an diagrammatic representation that shows transition of Process through various queues:
Non-Preemptive Scheduling:
➢Non-Preemptive Scheduling is employed in systems, which implement non-preemptive multitasking model.
➢The currently executing task/process is allowed to run until it terminates or enters the Block/Wait state waiting for
an I/O or system resource.
➢The various types of non-preemptive scheduling adopted in task/process scheduling are:
First Come First Served (FCFS)/FIFO Scheduling
Last Come First Served (LCFS)/LIFO Scheduling
Shortest Job First (SJF) Scheduling
Priority Based Scheduling
➢Allocates CPU time to the process based on order in which they enter the ‘Ready’ queue.
➢Example is Ticket reservation system where people need to stand in a queue and the first person in the queue is
serviced first.
➢The first entered process is serviced first, also called as First In First Out (FIFO) scheduling.
Turn Around Time of Process, P1 = 10ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P2 = 15ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P3 = 22ms (time spent in Ready Queue + Execution Time)
Therefore, Average Turn Around Time = (Turn Around Time of (P1+P2+P3) / 3)
(10 + 15 +22) / 3 = 47/3 = 15.66 milliseconds
Turn Around Time of Process, P2 = 5ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P1 = 15ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P3 = 22ms (time spent in Ready Queue + Execution Time)
Therefore, Average Turn Around Time = (Turn Around Time of (P2+P1+P3) / 3)
(5 + 15 +22) / 3 = 42/3 = 14 milliseconds
Analysis:
The Average Waiting Time and Average Turn Around Time depends upon order in which the processes enter
the ‘Ready’ queue.
The Average Waiting Time and Average Turn Around Time improves if the process with shortest execution
time is scheduled first.
Major Drawbacks are:
➢It favors monopoly of process. A process, which does not contain any I/O operation continues it execution till it
finishes. If the task/process contains I/O operation, the CPU is relinquished by the process.
➢In general FCFS algorithm favors CPU bound processes and I/O bound processes must wait until completion of
CPU bound processes, if currently executing process is a CPU bound Process.
➢This leads to poor device utilization. Average waiting Time is not minimal for FCFS scheduling algorithm.
➢Also allocates CPU time to the process based on order in which they enter the ‘Ready’ queue.
➢The Last entered process is serviced first, also called as Last In First Out (LIFO) scheduling where the the process,
which is put last into ‘Ready’ queue is serviced first.
Therefore, Average Turn Around Time = (Turn Around Time of (P1+P4+P3 + P2) / 4)
(10 + 11+23+28) / 4 = 72/4 = 18 milliseconds
➢Sorts the ‘Ready’ queue each time the ready queue relinquishes the CPU to pick the process with the shortest
estimated completion / run time.
➢The process with shortest estimated run time is scheduled first, followed by next shortest Process and so on.
Turn Around Time of Process, P2= 5ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P3 = 12ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P1 = 22ms (time spent in Ready Queue + Execution Time)
From this example, it is clear that Average waiting time and Average Turn Around Time is much improved for SJF scheduling
for same processes compare to FCFS scheduling.
RTOS for Embedded System Design
Therefore, Average Turn Around Time = (Turn Around Time of (P2+P4+P3 + P1) / 4)
(5+ 5+14+24) / 4 = 48/4 = 12 milliseconds
RTOS for Embedded System Design
Analysis:
The Average Waiting Time and Average Turn Around Time minimal for SJF scheduling and so it is optimal
compared to other non-preemptive scheduling.
➢Priority based non-preemptive scheduling ensures that a process with high priority is serviced at the earliest
compared to other low priority processes in the ‘Ready’ queue.
➢Priority of a task/process can be indicated through various mechanisms.
➢SJF algorithm can be viewed as a priority based scheduling where process is prioritized in the order of time required
to complete the task.
➢Priority can be 0 to maximum priority supported by the OS.
RTOS for Embedded System Design
Turn Around Time of Process, P1= 10ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P3 = 17ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P2 = 22ms (time spent in Ready Queue + Execution Time)
Therefore, Average Turn Around Time = (Turn Around Time of (P1+P4+P3 + P2) / 4)
(10+11+23+28) / 4 = 72/4 = 18 milliseconds
RTOS for Embedded System Design
Preemptive Scheduling:
➢Preemptive Scheduling is employed in systems, which implement preemptive multitasking model.
➢Every task/process in the ‘Ready’ queue gets a chance to execute.
➢When and how often each process gets a chance to execute is dependent on type of preemptive scheduling
algorithm used for scheduling tasks/processes.
➢The scheduler can preempt the currently executing task/process and select another task/process from ‘Ready’ queue
for execution.
➢The task which is preempted by the scheduler goes to ‘Ready’ queue, this act without the process requesting for it –
‘Preemption’
➢The Preemptive scheduling can be implemented in different approaches. The two important approaches being Time-
Based Preemption and Priority Based Preemption.
➢The various types of Preemptive scheduling adopted in task/process scheduling are:
Preemptive Shortest Job First (SJF)/shortest Remaining Time(SRT) Scheduling
Round Robin (RR) Scheduling
Priority Based Scheduling
➢Sorts the ‘Ready’ queue when a new process enters the ‘Ready’ queue and checks whether the execution time of the
new process is shorter than the remaining of the total estimated time for the currently executing process.
➢If the execution time of the new process is less, the currently executed process is preempted and new process is
scheduled for execution.
➢Thus preemptive SJF scheduling always compares the execution time of the new process entering the ‘Ready’
queue with the remaining time for completion of the currently executing process and schedules the process with
shortest remaining time for execution. – Shortest Remaining First Scheduling.
waiting time of Process, P2 = 0ms + (4-2)ms = 2ms (P2 starts execution first but interrupted by P4 and has to
wait till P4 completes execution )
waiting time of Process,P4 = 0ms (P4 is scheduled as soon as it enters the ‘Ready’ queue
waiting time of Process, P3 = 7ms (P3 starts execution after completion of P2 and P4)
waiting time of Process, P1 = 14ms (P1 starts execution after completion of P2, P4 & P3)
Turn Around Time of Process, P1= 12ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P2 = 10ms (time spent in Ready Queue + Execution Time)
Turn Around Time of Process, P3 = 6ms (time spent in Ready Queue + Execution Time)
RR scheduling involves lot of overhead in maintaining the time slice information for every process which is currently being executed.
➢In Priority based Preemptive scheduling, any high priority process entering the ‘Ready’ queue is immediately
scheduled for execution whereas in non-preemptive scheduling any high priority process entering the ‘Ready’ queue
is scheduled only after completion of currently executing task or voluntarily relinquishes the CPU.
➢Priority of a task/process is indicated is same way as that of the mechanisms adopted for non-preemptive
multitasking.
Therefore, Average Turn Around Time = (Turn Around Time of (P1+P4+P3 + P2) / 4)
(16+6+23+28) / 4 = 73/4 = 18.25 milliseconds
Analysis:
Preemptive Priority based Scheduling is adopted in systems which demand ‘Real time’ behavior.
Functional Requirements:
1. Processor Support:
➢it is necessary that all RTOS’s support all kinds of Processor architecture.
2. Memory Requirements:
➢OS requires ROM memory for holding OS files and it is normally stored in non-volatile memory like FLASH.
➢OS also requires working memory RAM for loading the OS services.
➢Since embedded systems are memory constrained, it is essential to evaluate the minimal ROM and RAM
requirements for the OS under consideration.
3. Real time capabilities:
➢it is mandatory that OS for all embedded system are to be Real-time as all Embedded Systems are Real time in
behavior.
4. Kernel and interrupt latency:
➢The kernel of OS may disable interrupts while executing certain services and it may lead to interrupt latency.
➢For embedded system whose response requirement are high, this latency must be minimal.
2. Cost:
➢the total cost for developing or buying OS and maintaining it in terms of commercial product and custom build needs to be evaluated
before taking decision on selection of OS
➢Availability of development and debugging tools is a critical decision making factor in the selection of an OS for embedded design
➢Certain OS may be superior in performance, but availability of tools for supporting development may be limited.
➢Explore the different tools available for OS under consideration.
4. Ease of use:
➢How easy it is to use a commercial RTOS
5. After sales:
➢For commercial embedded RTOS, in the form of e-mail, on-call services etc. for bug fixes, critical patch updates and support for
production issues etc. should be thoroughly analyzed.
Reference