RTOS Material
RTOS Material
OPERATING SYSTEM
M.E I-I Semester
Arbaaz Khan
161020744001
NSAKCET
UNIT I
1. Unix Kernel:
The UNIX operating system is made up of three parts; the kernel, the shell, the Commands and Utilities, the Files and Directories
• Kernel: The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles
the files management and communications in response to system calls.
• Shell: The shell is the utility that processes your requests. When you type in a command at your terminal, the shell
interprets the command and calls the program that you want. The shell uses standard syntax for all commands. C Shell,
Bourne Shell and Korn Shell are the most famous shells which are available with most of the Unix variants.
• Commands and Utilities: There are various commands and utilities which you can make use of in your day to day
activities. cp, mv, cat and grep, etc. are few examples of commands and utilities. There are over 250 standard
commands plus numerous others provided through 3 rd party software. All the commands come along with various
options.
• Files and Directories: All the data of Unix is organized into files. All files are then organized into directories. These
directories are further organized into a tree-like structure called the filesystem.
2. File system:
A Linux file system is a structured collection of files on a disk drive or a partition. A partition is a segment of memory and
contains some specific data. In our machine, there can be various partitions of the memory. Generally, every partition contains
a file system.
• The file system requires an API (Application programming interface) to access the function calls to interact with file
system components like files and directories. API facilitates tasks such as creating, deleting, and copying the files. It
facilitates an algorithm that defines the arrangement of files on a file system.
• The first two parts of the given file system together called a Linux virtual file system. It provides a single set of
commands for the kernel and developers to access the file system. This virtual file system requires the specific system
driver to give an interface to the file system.
3. Process:
A process is defined as an entity which represents the basic unit of work to be implemented in the system.
An instance of a running program is called a process. Every time you run a shell command, a program is run and a process is
created for it. Each process in Linux has a process id (PID) and it is associated with a particular user and group account.
Linux is a multiprocessing operating system, its objective is to have a process running on each CPU in the system at all times,
to maximize CPU utilization. If there are more processes than CPUs (and there usually are), the rest of the processes must wait
before a CPU becomes free until they can be run.
The kernel itself is not a process but a process manager. The process/kernel model assumes that processes that require a kernel
service use specific programming constructs called system calls. Each system call sets up the group of parameters that identifies
the process request and then executes the hardware-dependent CPU instruction to switch from User Mode to Kernel Mode.
"Real time" (for a process) refers to the scheduling algorithm, or the thinking the kernel does when it decides which process
gets to run. A real time process will preempt all other processes (of lesser scheduling weight) when an interrupt is received and
it needs to run.
Principles of Concurrency:
Both interleaved and overlapped processes can be viewed as examples of concurrent processes, they both present the same
problems.
The relative speed of execution cannot be predicted. It depends on the following:
• The activities of other processes
• The way operating system handles interrupts
• The scheduling policies of the operating system
Problems in Concurrency:
• Sharing global resources
Sharing of global resources safely is difficult. If two processes both make use of a global variable and both perform
read and write on that variable, then the order iin which various read and write are executed is critical.
• Optimal allocation of resources
It is difficult for the operating system to manage the allocation of resources optimally.
• Locating programming errors
It is very difficult to locate a programming error because reports are usually not reproducible.
• Locking the channel
It may be inefficient for the operating system to simply lock the channel and prevents its use by other processes.
Advantages of Concurrency:
• Running of multiple applications
It enable to run multiple applications at the same time.
• Better resource utilization
It enables that the resources that are unused by one application can be used for other applications.
• Better average response time
Without concurrency, each application has to be run to completion before the next one can be run.
• Better performance
It enables the better performance by the operating system. When one application uses only the processor and another
application uses only the disk drive then the time to run both applications concurrently to completion will be shorter
than the time to run each application consecutively.
Drawbacks of Concurrency:
• It is required to protect multiple applications from one another.
• It is required to coordinate multiple applications through additional mechanisms.
• Additional performance overheads and complexities in operating systems are required for switching among
applications.
• Sometimes running too many applications concurrently leads to severely degraded performance.
Issues of Concurrency:
• Non-atomic –
Operations that are non-atomic but interruptible by multiple processes can cause problems.
• Race conditions
A race condition occurs of the outcome depends on which of several processes gets to a point first.
• Blocking –
Processes can block waiting for resources. A process could be blocked for long period of time waiting for input from
a terminal. If the process is required to periodically update some data, this would be very undesirable.
• Starvation
It occurs when a process does not obtain service to progress.
• Deadlock
It occurs when two processes are blocked and hence neither can proceed to execute.
Interrupts:
An interrupt is an event that changes the sequence of instructions executed by the processor.
There are two different kinds of interrupts:
1. Synchronous interrupt (Exception) produced by the CPU while processing instructions
2. Asynchronous interrupt (Interrupt) issued by other hardware devices
Exceptions are caused by programming errors (i.e. Divide error, Page Fault, Overflow) that must be handled by the kernel. He
sends a signal to the program and tries to recover from the error.
The following two exceptions are classified:
• Processor-detected exception generated by the CPU while detecting a anomalous condition; divided into three
groups: Faults can generally be corrected, Traps report an execution, Aborts are serious errors.
• Programmed exception requested by the programmer, handled like a trap.
Interrupts can be issued by I/O devices (keyboard, network adapter, ...), interval timers and (on multiprocessor systems) other
CPUs. When an interrupt occurs, the CPU must stop his current instruction and execute the newly arrived interrupt. He needs
to save the old interrupted process state to (probably) resume it after the interrupt is handled.
Handling interrupts is a sensitive task:
• Interrupts can occur at any time; the kernel tries to get it out of the way as soon as possible
• An interrupt can be interrupted by another interrupt
• There are regions in the kernel which must not be interrupted at all
Every hardware device has its own Interrupt Request (IRQ) line. The IRQs are numbered starting from 0. All IRQ lines are
connected to a Programmable Interrupt Controller (PIC). The PIC listens on IRQs and assigns them to the CPU. It is also
possible to disable a specific IRQ line.
Modern multiprocessing Linux systems generally include the newer Advanced PIC (APIC), which distributes IRQ requests
equally between the CPUs.
The mid-step between an interrupt or exception and the handling of it is the Interrupt Descriptor Table (IDT). This table
associates each interrupt or exception vector (a number) with a specified handler (i.e. Divide error gets handled by the
function divide error ()). Through the IDT, the kernel knows exactly how to handle the occurred interrupt or exception.
5. Process Management:
• A process is usually defined as the instance of the running program. Process management is an integral part of any
modern day operating system (OS). Process management describes how the operating systems manage the multiple
processes running at a particular instance of time.
• The OS must allocate resources to processes, enable processes to share and exchange information, protect the resources
of each process from other processes and enable synchronization among processes. To meet these requirements, the
OS must maintain a data structure for each process, which describes the state and resource ownership of that process,
and which enables the OS to exert control over each process.
• Several processes can run at the same time and must all be handled by the operating system. The kernel needs to
maintain data for each process it is running. The data tells the OS in what state the process is, what les are open, which
user is running it, etc. The information is maintained in a process structure is also called Process Control Block (PCB).
• The operating system tracks processes through a five-digit ID number known as the pid or the process ID. Each
process in the system has a unique pid.
• Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any point
of time, no two processes with the same pid exist in the system because it is the pid that Unix uses to track each process.
Every application(program) comes into execution through means of process, process is a running instance of a program.
Processes are created through different system calls, most popular are fork() and exec().
Fork creates a new process by duplicating the calling process, The new process, referred to as child, is an exact duplicate of
the calling process, referred to as parent, except for the following:
1. The child has its own unique process ID, and this PID does not match the ID of any existing process group.
2. The child’s parent process ID is the same as the parent’s process ID.
3. The child does not inherit its parent’s memory locks and semaphore adjustments.
4. The child does not inherit outstanding asynchronous I/O operations from its parent nor does it inherit any asynchronous
I/O contexts from its parent.
Exection family of functions replaces the current process image with a new process image. It loads the program into the current
process space and runs it from the entry point.
ReadFile() Data is read from the file using this system call.
WriteFile() Data is written into the file using this system call.
SetTimer() This system call sets the alarm or the timer of a process
SetFileSecurity() This system call sets the security for a particular process
SetConsoleMode() This sets the input mode or output mode of the console’s
input buffer or output screen buffer respectively.
ReadConsole() This reads the characters from the console input buffer.
WriteConsole() This writes the characters into the console output buffer.
The process scheduling is the activity of the process manager that handles the removal of the running process from the CPU
and the selection of another process on the basis of a particular strategy.
Process scheduling is an essential part of a Multiprogramming operating systems. Such operating systems allow more than one
process to be loaded into the executable memory at a time and the loaded process shares the CPU using time multiplexing.
7. Process Scheduling:
The process scheduling is the activity of the process manager that handles the removal of the running process from the CPU
and the selection of another process on the basis of a particular strategy.
Process scheduling is an essential part of a Multiprogramming operating systems. Such operating systems allow more than
one process to be loaded into the executable memory at a time and the loaded process shares the CPU using time multiplexing.
The scheduler is the component of the kernel that selects which process to run next. The scheduler (or process scheduler,
as it is sometimes called) can be viewed as the code that divides the finite resource of processor time between the runnable
processes on a system.
Scheduling fell into one of the two general categories:
• Pre-emptive Scheduling: In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it
is important to run a task with a higher priority before another lower priority task, even if the lower priority task is still
running. The lower priority task holds for some time and resumes when the higher priority task finishes its execution.
• Non Pre-emptive Scheduling: In this type of scheduling method, the CPU has been allocated to a specific process.
The process that keeps the CPU busy will release the CPU either by switching context or terminating. It is the only
method that can be used for various hardware platforms. That's because it doesn't need special hardware (for example,
a timer) like preemptive scheduling. When the currently executing process gives up the CPU voluntarily.
Shell, command interpreter, is a program started up after opening of the user session by the login process. The shell is active
till the occurrence of the character, which signals requests for termination of the execution and for informing about that fact the
operating system kernel.
Each user obtains their own separate instance of the sh. Program sh prints out the monitor on the screen showing its readiness
to read a next command.
A filter is a small and (usually) specialized program in Unix-like operating systems that transforms plain text (i.e., human
readable) data in some meaningful way and that can be used together with other filters and pipes to form a series of operations
that produces highly specific results.
As is generally the case with command line (i.e., all-text mode) programs in Unix-like operating systems, filters read data
from standard input and write to standard output. Standard input is the source of data for a program, and by default it is text
typed in at the keyboard. However, it can be redirected to come from a file or from the output of another program. Standard
output is the destination of output from a program, and by default it the display screen. This means that if the output of a
command is not redirected to a file or another device (such as a printer) or piped to another filter for further processing, it will
be sent to the monitor where it will be displayed.
The ls command lists the contents of /sbin and pipes its output (using the pipe operator, which is designated by the vertical bar
character) to the filter grep, which searches for all files and directories that contain the letter sequence mk in their names. grep
then pipes its output to the sort filter, which, with its -r option, sorts it in reverse alphabetical order. sort, in turn, pipes its output
to the head filter.
There are more than ten parts to the POSIX standard, but two are widely available.
POSIX.1 defines C programming interfaces (that is, a library of system calls) for files, processes, and terminal I/O. To support
the library, a POSIX system must implement a Hierarchical File System (HFS).
POSIX.2 defines a "shell" command interpreter and utilities (e.g., ls to list files).
Certain important standards are not covered by POSIX (for example, spooling, batch processing, and NLS -- Native Language
Support). NLS is defined by the X/Open standard.
A hard real-time system also referred to as immediate real-time system is a hardware or software that must operate within the
confines of a stringent predefined deadline. Usually, the application is considered to have failed if it does not complete its
function within the specified timeline. The response predefined time of hard real time systems is in order of milliseconds and
therefore, missing of the deadline results in complete or massive system failure, and therefore this system should not miss the
deadline.
Soft Real Time System:
Soft real-time system is an operating system in which a critical real-time task gets priority over other task and retains that
priority over other tasks until it completes. The response predefined time of soft real time systems are not very stringent,
therefore missing of the deadline only affects the performance and not the entire system.
Response Time The response predefined time of hard real time The response predefined time of soft real time
systems is in order of milliseconds and therefore, systems are not very stringent, therefore missing
missing of the deadline results in complete or of the deadline only affects the performance and
massive system failure, and therefore this system not the entire system. Soft real time systems miss
should not miss the deadline. deadline occasionally.
Peak Load Peak load performance should be predictable and In a soft real-time system, a degraded operation in
should not violate the predefined deadlines. a rarely occurring peak load can be tolerated.
Conditional A hard real-time system must remain synchronous Soft real time system will slow down their
Requirement with the state of the environment at all time. response time if the load is very high.
Database Size & Most of the hard real time systems have small Most of the soft real time systems have larger
Integrity databases and occasionally require short-term databases and require long-term integrity of the
integrity of the system. system.
Error Handling In case of an error in a hard real time system, the In case of an error in a soft real time system, the
computation is rolled back or recovery is of computation is rolled back to previously
limited use. established checkpoint to initiate a recovery
action.
Completion Of Completion of the task or activity by hard real Completion of task or activity by soft real time
Task/Activity time systems is predefined or deterministic. system probabilistic.
Validation The users of hard real time systems obtain Users of soft real time systems not always obtain
validation when required. the validation.
Examples Examples of hard real time system are inkjet Examples of soft real time systems are DVD
printer system, railway signalling system, air player, electronic games, multimedia system, web
traffic control systems, nuclear reactor control browsing, online transaction systems, telephone
systems, anti-missile system. switches, virtual reality, weather station, mobile
communication etc.
2. Jobs & Processors:
▪ Each unit of work that is scheduled and executed by the system job is a job.
▪ Set of related jobs which jointly provides some system function is called task.
▪ Computation of a control law is a job, so is the computation of FFT (Fast Fourier Transform) of sensor data or
transmission of data packet or file retrieval and so on.
▪ Rather than using different verbs (E.g. Compute, transmit etc.) for different types of jobs, we say job executes or is
executed by system.
▪ The jobs mentioned above executes on CPU, network or disk. These all resources are called processor, except in the
case where we wanted to be specific.
▪ Processors have attributes (pre-emptivity, context switch time, speed). Two processors are of the same type, if they
are functionally identical and can be used interchangeably.
▪ According to the model the basic component of any real-time application are jobs. The operating system treats jobs as
a unit of work and allocates processors and resources.
3. Hard and Soft Timing Constraints:
3.1. Deadline:
A deadline is a timing milestone. If a deadline is missed by a computer-controller, the controlled system may transit to an
undesirable state.
In hard real-time systems, according to the usual definition, a deadline that is not met can lead to a catastrophic failure. This
means that the criteria used to establish deadlines are safety based. Control system engineers, on the other hand, use performance
criteria to establish the desired response time of a controlling computer.
The deadlines suggested by these scientific communities are not mutually exclusive but only different entities perceived in
particular and equally important contexts. Moreover, they show the disassociation of controllers’ timing constraints into those
related to safety – hard deadlines – and those related to performance – performance deadlines.
Performance deadlines are usually more confining than hard deadlines. Therefore, a computer controller, designed to meet
performance deadlines, does not drive the controlled system to an unsafe state as soon as one of them is missed, but only later,
when a hard deadline is disrespected. Performance and hard deadlines are thus separated by a grace-time. This notion can help
in the design of low-cost, yet highly reliable control systems.
3.2. Time Constraints:
Real-time systems are usually classified into soft and hard. Classically, in a soft real-time system, missing a deadline is
inconvenient but not damaging to the environment; in hard real-time system, missing a deadline can be catastrophic, and thus
unacceptable.
The traditional view of the temporal merit of a hard real-time computation (i.e., the relationship between the computation
completion time and the resulting temporal merit of that computation) is usually modelled by a step time-value function: if a
controller service is completed before a given deadline it yields a constant positive value while completing it any time later may
incur in a catastrophic failure. From this point of view, hard deadlines are established in a safety based context.
This means that when a computer is part of a hard real-time system, all the software running on it has to be tuned to satisfy all
controlled system deadlines.
It is commonly accepted that a controlled process can sporadically tolerate a missed deadline, if not by much. This notion
presupposes a controller not tuned to meet hard deadlines but some other kind of time limit.
However, the characterisation of a deadline is by itself a relatively unexplored problem in the real-time community. Most of
the literature seems to consider that deadlines are somehow provided by others, possibly by control system engineers. Moreover,
techniques to calculate systems’ deadlines are very seldom presented.
Nevertheless, soft and hard deadlines are universally used, and many suggestions appear in the literature reasoning about the
existence of other kinds of deadlines, besides these classical ones. Moreover, there is a growing tendency to classify real-time
services according to their associated benefit/cost functions, and to establish on them a set of pertinent points of time concerning
application goals. This means that there is a growing feeling that traditional definitions and interpretations of deadlines are poor
since they cannot describe reality in a satisfactory way nor can be explicitly employed for a best-effort scheduling.
3.3. Hard and Soft Timing Constraint:
According to a commonly used definition, a timing constraint or deadline is hard if the failure to meet it is considered to be a
fatal fault. A hard deadline is imposed on a job because a late result produced by the job after the deadline may have disastrous
consequences. (As examples, a late command to stop a train may cause a collision, and a bomb dropped too late may hit a
civilian population instead of the intended military target.)
In contrast, the late completion of a job that has a soft deadline is undesirable. However, a few misses of soft deadlines do no
serious harm; only the system’s overall performance becomes poorer and poorer when more and more jobs with soft deadlines
complete late.
This definition of hard and soft deadlines invariably leads to the question of whether the consequence of a missed deadline is
indeed serious enough. The question of whether a timing constraint is hard or soft degenerates to that of how serious is serious.
In real-time systems literature, the distinction between hard and soft timing constraints is sometimes stated quantitatively in
terms of the usefulness of results (and therefore the overall system performance) as functions of the tardinesses of jobs.
The tardiness of a job measures how late it completes respective to its deadline. Its tardiness is zero if the job completes at or
before its deadline; otherwise, if the job is late, its tardiness is equal to the difference between its completion time (i.e., the time
instant at which it completes execution) and its deadline.
The usefulness of a result produced by a soft real-time job (i.e., a job with a soft deadline) decreases gradually as the tardiness
of the job increases, but the usefulness of a result produced by a hard real-time job (i.e., a job with a hard deadline) falls off
abruptly and may even become negative when the tardiness of the job becomes larger than zero.
The deadline of a job is softer if the usefulness of its result decreases at a lower rate. By this means, we can define a spectrum
of hard/soft timing constraints. This quantitative measure of hardness and softness of deadlines is sometimes useful.
It is certainly more appealing to computer scientists and engineers who have been trained not to rely on handwaving, qualitative
measures. However, there is often no natural choice of usefulness functions. When choices are made, it is difficult to validate
that the choices are sound and that different measures of the overall system performance as functions of tardinesses indeed
behave as specified by the usefulness functions.
Consequently, this kind of quantitative measure is not as rigorous as it appears to be. Sometimes, we see this distinction made
on the basis of whether the timing constraint is expressed in deterministic or probabilistic terms. If a job must never miss its
deadline, then the deadline is hard. On the other hand, if its deadline can be missed occasionally with some acceptably low
probability, then its timing constraint is soft.
An example is that the system recovery job or a point-of-sales transaction completes within one minute 99.999 percent of the
time. In other words, the probability of failure to meet the one-minute relative deadline is 10−5. This definition ignores
completely the consequence of a timing failure. In our example, if the failure of an on-time recovery could cause loss of life
and property, we would require a rigorous demonstration that the timing failure probability is indeed never more than 10−5.
However, we would not require a demonstration of nearly the same rigor for a credit validation.
4. SCHEDULING:
When a computer is multiprogrammed, it frequently has multiple processes or threads competing for the CPU at the same time.
This situation occurs whenever two or more of them are simultaneously in the ready state. If only one CPU is available, a choice
has to be made which process to run next. The part of the operating system that makes the choice is called the scheduler, and
the algorithm it uses is called the scheduling algorithm. These topics form the subject matter of the following sections. Many
of the same issues that apply to process scheduling also apply to thread scheduling, although some are different. When the
kernel manages threads, scheduling is usually done per thread, with little or no regard to which process the thread belongs.
Initially we will focus on scheduling issues that apply to both processes and threads. Later on, we will explicitly look at thread
scheduling and some of the unique issues it raises
Rate monotonic scheduling Algorithm works on the principle of preemption. Preemption occurs on a given processor when
higher priority task blocked lower priority task from execution. This blocking occurs due to priority level of different tasks in
a given task set. rate monotonic is a preemptive algorithm which means if a task with shorter period comes during execution it
will gain a higher priority and can block or preemptive currently running tasks. In RM priorities are assigned according to time
period. Priority of a task is inversely proportional to its timer period. Task with lowest time period has highest priority and the
task with highest period will have lowest priority. A given task is scheduled under rate monotonic scheduling Algorithm, if its
satisfies the following equation:
Where n = The number of processes in the process set,
Ci = The computation time of the process,
Ti = The Time period for the process to run and
U = The processor utilization
For example, we have a task set that consists of three tasks as follows
A task set given in above table its RM scheduling is given in above figure. The explanation of above is as follows
1. According to RM scheduling algorithm task with shorter period has higher priority so T1 has high priority, T2 has
intermediate priority and T3 has lowest priority. At t=0 all the tasks are released. Now T1 has highest priority so it
executes first till t=0.5.
2. At t=0.5 task T2 has higher priority than T3 so it executes first for one-time units till t=1.5. After its completion only one
task is remained in the system that is T3, so it starts its execution and executes till t=3.
3. At t=3 T1 releases, as it has higher priority than T3 so it pre-empts or blocks T3 and starts it execution till t=3.5. After
that the remaining part of T3 executes.
4. At t=4 T2 releases and completes it execution as there is no task running in the system at this time.
5. At t=6 both T1 and T3 are released at the same time but T1 has higher priority due to shorter period so it preempts T3
and executes till t=6.5, after that T3 starts running and executes till t=8.
6. At t=8 T2 with higher priority than T3 releases so it preempts T3 and starts its execution.
7. At t=9 T1 is released again and it preempts T3 and executes first and at t=9.5 T3 executes its remaining part. Similarly,
the execution goes on.
It is an optimal dynamic priority scheduling algorithm used in real-time systems. It can be used for both static and dynamic
real-time scheduling.
EDF uses priorities to the jobs for scheduling. It assigns priorities to the task according to the absolute deadline. The task
whose deadline is closest gets the highest priority. The priorities are assigned and changed in a dynamic fashion. EDF is very
efficient as compared to other scheduling algorithms in real-time systems. It can make the CPU utilization to about 100%
while still guaranteeing the deadlines of all the tasks.
EDF includes the kernel overload. In EDF, if the CPU usage is less than 100%, then it means that all the tasks have met the
deadline. EDF finds an optimal feasible schedule. The feasible schedule is one in which all the tasks in the system are
executed within the deadline. If EDF is not able to find a feasible schedule for all the tasks in the real-time system, then it
means that no other task scheduling algorithms in real-time systems can give a feasible schedule. All the tasks which are
ready for execution should announce their deadline to EDF when the task becomes runnable.
EDF scheduling algorithm does not need the tasks or processes to be periodic and also the tasks or processes require a fixed
CPU burst time. In EDF, any executing task can be preempted if any other periodic instance with an earlier deadline is ready
for execution and becomes active. Preemption is allowed in the Earliest Deadline First scheduling algorithm.
Example:
Consider two processes P1 and P2.
The difference between an OS (Operating System) such as Windows or Unix and an RTOS (Real Time
Operating System) found in embedded systems, is the response time to external events. OS’s typically
provide a non-deterministic, soft real time response, where there are no guarantees as to when each task will
complete, but they will try to stay responsive to the user. An RTOS differs in that it typically provides a hard
real time response, providing a fast, highly deterministic reaction to external events.
A Traditional OS (or) General Purpose OS(GPOS) is used for systems/applications that are not time
critical. Example: - Windows, Linux, Unix etc.
A RTOS is used for time critical systems. Example: - VxWorks, uCos etc.
RTOS GPOS
RTOS has unfair scheduling i.e. scheduling is GPOS has fair scheduling i.e. it can be adjusted
based on priority. dynamically for optimized throughput.
It works under worst case assumptions. It optimizes for the average case.
RTOS focuses on very fast response time A regular OS focuses on computing throughput.
RTOSes are generally embedded in devices that OSes are used in a wide variety of applications
require real time response
RTOSes either use a time sharing design or an OSes use a time sharing design to allow for multi-
even driven design tasking
The coding of an RTOS is stricter. The coding of an RTOS is not much stricter.
3. Real time System Concepts:
Real-time systems are characterized by the fact that severe consequences will result if logical as well as timing correctness properties
of the system are not met. There are two types of real-time systems: SOFT and HARD.
In a SOFT real-time system, tasks are performed by the system as fast as possible, but the tasks don't have to finish by specific
times. In HARD real-time systems, tasks have to be performed not only correctly but on time. Most realtime systems have a
combination of SOFT and HARD requirements.
Real-time applications cover a wide range. Most applications for real-time systems are embedded. This means that the computer
is built into a system and is not seen by the user as being a computer. Examples of embedded systems are:
Process control:
• Food processing
• Chemical plants
Automotive:
• Engine controls
• Anti-lock braking systems
Office automation:
• FAX machines
• Copiers
Computer peripherals:
• Printers
• Terminals
• Scanners
• Modems
Robots Aerospace:
• Flight management systems
• Weapons systems
• Jet engine controls
Domestic:
• Microwave ovens
• Dishwashers
• Washing machines
• Thermostats
Real-time software applications are typically more difficult to design than non-real-time applications. This chapter describes real-
time concepts.
1. Foreground/Background Systems:
These systems are called foreground/background or super-loops. An application consists of an infinite loop that calls
modules (that is, functions) to perform the desired operations (background). Interrupt Service Routines (ISRs) handle
asynchronous events (foreground). Foreground is also called interrupt level while background is called task level. Critical
operations must be performed by the ISRs to ensure that they are dealt with in a timely fashion.
3. Resource:
A resource is any entity used by a task. A resource can thus be an I/O device such as a printer, a keyboard, a display, etc.
or a variable, a structure, an array, etc.
4. Shared Resource:
A shared resource is a resource that can be used by more than one task. Each task should gain exclusive access to the shared
resource to prevent data corruption. This is called Mutual Exclusion.
5. Multitasking:
Multitasking is the process of scheduling and switching the CPU (Central Processing Unit) between several tasks; a single
CPU switches its attention between several sequential tasks. Multitasking is like foreground/background with multiple
backgrounds. Multitasking maximizes the utilization of the CPU and also provides for modular construction of
applications.
One of the most important aspects of multitasking is that it allows the application programmer to manage
complexity inherent in real-time applications. Application programs are typically easier to design and maintain if
multitasking is used.
6. Task:
A task, also called a thread, is a simple program that thinks it has the CPU all to itself. The design process for a realtime
application involves splitting the work to be done into tasks which are responsible for a portion of the problem. Each task
is assigned a priority, its own set of CPU registers, and its own stack area.
Each task typically is an infinite loop that can be in any one of five states: DORMANT, READY, RUNNING, WAITING
FOR AN EVENT, or INTERRUPTED (see Figure 2-3). The DORMANT state corresponds to a task which resides in
memory but has not been made available to the multitasking kernel. A task is READY when it can execute but its priority
is less than the currently running task. A task is RUNNING when it has control of the CPU. A task is WAITING FOR AN
EVENT when it requires the occurrence of an event (waiting for an I/O operation to complete, a shared resource to be
available, a timing pulse to occur, time to expire etc.). Finally, a task is INTERRUPTED when an interrupt has occurred
and the CPU is in the process of servicing the interrupt. Figure 2-3 also shows the functions provided by µC/OS-II to make
a task switch from one state to another.
8. Kernel: The kernel is the part of a multitasking system responsible for the management of tasks (that is, for managing
the CPU's time) and communication between tasks. The fundamental service provided by the kernel is context switching.
The use of a real-time kernel will generally simplify the design of systems by allowing the application to be divided into
multiple tasks managed by the kernel. A kernel will add overhead to your system because it requires extra ROM (code
space), additional RAM for the kernel data structures but most importantly, each task requires its own stack space which
has a tendency to eat up RAM quite quickly. A kernel will also consume CPU time (typically between 2 and 5%).
Single chip microcontrollers are generally not able to run a real-time kernel because they have very little RAM.
A kernel can allow you to make better use of your CPU by providing you with indispensible services such as semaphore
management, mailboxes, queues, time delays, etc. Once you design a system using a real-time kernel, you will not want
to go back to a foreground/background system.
9. Scheduler: The scheduler, also called the dispatcher, is the part of the kernel responsible for determining which task will
run next. Most real-time kernels are priority based. Each task is assigned a priority based on its importance. The priority
for each task is application specific. In a priority-based kernel, control of the CPU will always be given to the highest
priority task ready-to-run. When the highest-priority task gets the CPU, however, is determined by the type of kernel
used. There are two types of priority-based kernels: non-preemptive and preemptive.
One of the advantages of a non-preemptive kernel is that interrupt latency is typically low (see the later
discussion on interrupts). At the task level, non-preemptive kernels can also use non-reentrant functions (discussed later).
Nonreentrant functions can be used by each task without fear of corruption by another task. This is because each task can
run to completion before it relinquishes the CPU. Non-reentrant functions, however, should not be allowed to give up
control of the CPU.
Task-level response using a non-preemptive kernel can be much lower than with foreground/background
systems because task-level response is now given by the time of the longest task.
Another advantage of non-preemptive kernels is the lesser need to guard shared data through the use of
semaphores. Each task owns the CPU and you don't have to fear that a task will be preempted. This is not an absolute
rule, and in some instances, semaphores should still be used. Shared I/O devices may still require the use of mutual
exclusion semaphores; for example, a task might still need exclusive access to a printer.
NOTE: For more concepts refer MicroC OS II_ The Real Time Kernel Book from Pg. No. 50
4. RTOS Kernel
RTOS Kernel RTOS Kernel provides an Abstraction layer that hides from application software the hardware details of the processor
/ set of processors upon which the application software shall run.
1. The central component of most Operating systems is called Kernel.
2. The kernel manages system's resources and the communication.
3. The kernel provides the most basic interface between the computer itself and the rest of the operating system.
4. The kernel is responsible for the management of the central processor.
5. The kernel includes the dispatcher to allocate the central processor, to determine the cause of an interrupt and initiate its
processing, and some provision for communication among the various system and user tasks currently active in the system.
6. Kernel is the core of an operating system.
Basic function of RTOS Kernel:
• Task Management
• Task Scheduling
• Task Synchronization
• Memory Management
• Time Management
• Interrupt Handling
• Device I/O Management
4.1. Task Assignment:
The application is divided into small, schedulable, and sequential program units known as ‘Thread‘ or ‘Task‘. This is done to achieve
concurrency in Real Time Application. Task Management by Kernel includes Real Time Task Creation, termination, changing
priorities etc. Task creation involves creating a Task Control Block (TCB) which has information about Task id, priority, Task states
i.e. if the task is in (idle, running, ready, terminated) state etc.
DORMANT
• The DORMANT state corresponds to a task that resides in memory but has not been made available to the multitasking
kernel.
READY
• A task is READY when it can be executed but its priority is less than that of the task currently being run.
• In this state, the task actively competes with all other ready tasks for the processor’s execution time.
• The kernel’s scheduler uses the priority of each task to determine which task to move to the running state.
RUNNING
• A task is RUNNING when it has control of the CPU and it's currently being executed.
• On a single-processor system, only one task can run at a time.
• When a task is preempted by a higher priority task, it moves to the ready state.
• It also can move to the blocked state.
–Making a call that requests an unavailable resource
–Making a call that requests to wait for an event to occur
–Making a call to delay the task for some duration
WAITING
•A task is WAITING when it requires the occurrence of an event (waiting for an I/O operation to complete, a shared resource
to be available, a timing pulse to occur, time to expire, etc.).
BLOCKED
• CPU starvation occurs when higher priority tasks use all of the CPU execution time and lower priority tasks do not get to
run.
The cases when blocking conditions are met
–A semaphore token for which a task is waiting is released
–A message, on which the task is waiting, arrives in a message queue
–A time delay imposed on the task expires
ISR(Interrupt Service Routine)
• A task is in ISR state when an interrupt has occurred and the CPU is in the process of servicing the interrupt.
4.2. Task Priorities:
Deciding Task priority based on
1. Assign high priority to Interrupt processing tasks - particularly if they are in user interaction flow.
2. Can assign high priority to short duration tasks.
3. I/O bound tasks may end up having low priority.
4.3. Task Synchronization:
Synchronization and messaging provide the necessary communication between tasks in one system to tasks in another system. The
event flag is used to synchronize internal activities while message queues and mailboxes are used to send text messages between
systems. Common data areas utilize semaphores.
4.4. Scheduling:
1. Scheduling is the process of deciding how to commit resources between a variety of possible tasks. Time can be specified
(scheduling a flight to leave at 8:00) or floating as part of a sequence of events.
2. Scheduling is a key concept in computer multitasking, multiprocessing operating system and real-time operating system designs.
3. Scheduling refers to the way processes are assigned to run on the available CPUs, since there are typically many more processes
running than there are available CPUs. This assignment is carried out by software's known as a scheduler and dispatcher.
The scheduler is concerned mainly with
CPU utilization - to keep the CPU as busy as possible.
Throughput - number of processes that complete their execution per time unit.
Turnaround - total time between submission of a process and its completion.
Waiting time - amount of time a process has been waiting in the ready queue.
Response time - amount of time it takes from when a request was submitted until the first response is produced.
Fairness - Equal CPU time to each thread.
4.5. Task Scheduling:
1. Schedulers are parts of the kernel responsible for determining which task runs next
2. Most real-time kernels use priority-based scheduling
1. Each task is assigned a priority based on its importance
2. The priority is application-specific
3. The scheduling can be handled automatically.
4. Many kernels also provide a set of API calls that allows developers to control the state changes.
5. Manual scheduling
6. Non-Real -time systems usually use Non-preemptive Scheduling
Once a task starts executing, it completes its full execution
7. Most RTOS perform priority-based preemptive task scheduling.
8. Basic rules for priority based preemptive task scheduling
The Highest Priority Task that is Ready to Run, will be the Task that Must be Running.
4.6. Inter task Communication:
• Software is the basic building block of RTOS. Task is a simply subroutine. Task must be able to communicate with one
another to coordinate their activities or to share the data.
• Kernel object is used for inter task communication. Kernel objects uses message queue, mail box and pipes, Shared
Memory, Signal Function and RPC a for inter task communication.
4.7. Context switch:
The meaning of the signal is implied by the semaphore object, so you need one semaphore for each purpose. The most common
type of semaphore is a binary semaphore, that triggers activation of a task. The typical design pattern is that a task contains a main
loop with an RTOS call to “take” the semaphore. If the semaphore is not yet signalled, the RTOS blocks the task from executing
further until some task or interrupt routine “gives” the semaphore, i.e., signals it.
Mutex: -
Mutexes are used to protect access to a shared resource. A mutex is created and then passed between the threads (they can acquire
and release the mutex).
A binary semaphore for mutual exclusion between tasks, to protect a critical section. Internally it works much the same way as a
binary semaphore, but it is used in a different way. It is “taken” before the critical section and “given” right after, i.e., in the same
task. A mutex typically stores the current “owner” task and may boost its scheduling priority to avoid a problem called “priority
inversion”