You are on page 1of 20

Concept of Process & Thread

Process (or Task) :


 Embedded application software can be thought of as consisting of many processes
 Process is an action of execution of the program.
 Process is defined by its own program code, data and stack
Process & Thread
 When process is created, RTOS allocates memory space and instantiates data
structure called Process Control Block (PCB)
 PCB :- Process Context (Program status word, SP, PC & other CPU registers value),
process ID, Process state, Process priority, pointers to parent and daughter process,
pointer to system resources, pointer to access permission descriptor for sharing
resources globally and with other process
 Context switching
 User mode (application) and Kernel mode (RTOS) processes
 a process can create one or more other processes (referred to as child processes) and
these processes in turn can create child processes to form process tree.

Threads:
 Threads are subunits of a process which may execute simultaneously
 Process may contain multiple threads. All threads of a process run in address space of
same process.
 Have their own PC, stack and registers which are private and never shared
 Advantages:- reduces context switching overhead, multiple control points in a
process, improves responsiveness
Process & Thread
Concept of Process & Thread
 Embedded OS schedules execution of processes
 Switching processes at fast rate gives feeling of multi-processing
 Process Status :- ready, running, blocked, terminated, sleeping
Scheduling Algorithms
Timing parameters:
 Release time : an instant the process is ready for execution
 Execution (run) time : time period to complete process execution
 Absolute deadline (d) : an instant by which process must finish
 Slack time = (d-t) – remaining run time of process at time t
= deadline – estimated completion time (negative slack means missed
deadline)
[A] Non Preemptive Task(Process) scheduling:
(1)Round Robin (Circular Queue):
 Processes are executed sequentially from ready linked list
 RTOS keeps track of currently executing process. It updates linked list upon
insertion and removal of a process. New process is inserted at the end of
linked list
 Circular FIFO buffer can be used to form ready list of processes
 New process is inserted before the currently running one (last in circular list)

 Simple architecture :– no interrupts, no shared data, no latency concerns.


Attractive option for some jobs
(2) Round Robin with Priority based Ready List
 Processes in ready queue are ordered as per their priority but executed in turn
(circularly) from ordered list
 Scheduling is still non-preemptive as no process is preempted by any other

 Time to execute all processes in ready queue is termed as one cycle


 The processes are ordered based on priority at the beginning of every cycle.
 Process is allowed to run till the end of execution
(3) Round Robin Scheduling with Time Slicing:
 RTOS defines a time slot for each process for its execution Tslot
 In a single schedule cycle ( time slice Tslice) processes are taken sequentially
 If a process doesn’t finish in Tslot it is blocked until it get its turn in the next time slice

 Tslice is fixed whereas Tslot is variable


 If process finishes before Tslot there is waiting period
Disadv. Of Non-Preemptive:
 Long execution of low priority tasks make high priority task to wait long
 Difficult to meet deadlines
Scheduling Algorithms
[B] Preemptive Scheduling:
 Process may be blocked at any instant to allow higher priority task to run.
This is called preemption.
 In preemptive scheduling, Priorities are assigned to processes using various
criteria.
 Following scheduling algorithms are commonly used

 Shortest Remaining Runtime First


 Priorities are assigned such that shorter the remaining execution
time, higher the priority
 Earliest Deadline First (EDF)
 Earlier the deadline, higher the priority

 Least Slack time First (LSF) or Least Laxity First (LLF)


 Smaller the slack, higher the priority
 Example :- A scheduler is invoked every 1 unit of time and selects process from ready
queue to run. (a) How processes are scheduled with Least Slack Time first policy ? (b)
Find total time spent in wait by all process, total no. of context switches and no. of deadline
missed for each scheduling.
In case of tie, following criteria would be used in that order (i) process already running
should be preferred over others (ii) process with earlier deadline should be selected (iii)
processes should be scheduled in Round robin fashion

Ans:

Slacktime(t) = deadline - t - time remaining to finish process at t


The slack time of processes when they became ready (released) is given by
P0=18 (at t=0), P1=4 (at t=10), P2=19 (at t=4) and for P3=5 (at t=5).

>slack time of running process remains constant whereas for waiting process it
decreases
Resource Access Control
 Global variables are often shared between ISR process and main process. As shown
sharing of variable ‘Temp’ causes race condition and leads to hazards. Without proper
locking, the code cannot be race free
 Solution : (i) disable all interrupts (ii) writing sensitive part of process in critical section
(region) of memory which is protected by lock

 Lock (semaphore) ensures that shared resource is acquired by only one process at a
time. It makes critical region atomic (uninterruptible). Each share resource has its own
lock (semaphore)
Semaphores
Semaphores :- acts as lock to protect critical region (piece of software where
a process accesses shared resource), process acquires semaphore from
before executing critical section codes

[1] Boolean Semaphore:


 facilitates mutually exclusive access to shared resources using boolean
Semaphore flag ‘sem_f’
 Each process does “test before set” action

 What if process is preempted


between “test and set” ? =>
multiple process may enter in
critical region (race condition)
 Testing again and again before
setting sem_f =0 would not help
much
Semaphores
[2] Taking Turn:
 Turn variable keeps track of which process can enter in critical region
 Strict alteration in execution
 Not good if one process takes too much time in non-critical section. Other
process has to wait long (busy waiting problem or starvation)

 What will be algorithm for n processes in general?


Semaphores
[3] Peterson’s Algorithm:
 Uses two variables: (1)flag => who is interested (2)turn => whose turn it is
 Process enters to Critical Section only if other processes are not interested
and if it is its turn
 Avoids waiting time even if some process takes to much time in non-critical
section. For example, long delay in non-critical section P1 will not hinder
the progress of P0
 Priority Inversion Problem
 Priority Inheritance Protocol
 Deadlock and its Prevention
Commercial RTOS
 Study of features of RTOSs gives idea of which is suitable for specific system
 Many commercial RTOS comes with development tools that facilitate real time
application development

 uC/OS
 RTOS from Micrium Corporation that support over 100 different processor
architectures ranging from 8-bit to 64-bit
 The Real-Time Kernel is a highly portable, ROMable, very scalable, fully
preemptive, deterministic
 Allows integration with other software packages such as μC/TCP-IP, μC/GUI,
μC/File System, μC/USB, μC/CAN, μC/Modbus, μC/Bluetooth for obtaining greater
scalability and performance
 Certified by FAA to be used in avionics

 VxWorks
 Multitasking RTOS from Wind River. It is hard real time.

 High accuracy, predictable response and less context switch time

 Symmetric Multi-Processing (SMP) to facilitate multi-core processors

 Spacecraft, robotics and programmable controller, Networking and Communication


components (routers, cellular base stations)
Commercial RTOS
 QNX
 It is Microkernel based Unix like RTOS.

 Kernel include only basic services such as CPU scheduling, interprocess


communication, interrupt redirection and timers
 It has unique feature of tight integration of message passing and CPU scheduling

 Intended for mission critical applications => Medical instrumentation, Process


control, Air traffic control, Internet routers & telematics devices,
 New version Neutrino (compact, fast and fault tolerant) is used as platform for
many portable embedded systems
Commercial RTOS
 FreeRTOS:
 Portable, open source, light weight Kernel (binary image is 6k to 12k bytes)

 FreeRTOS provides the core real time scheduling functionality, inter-task


communication, timing and synchronisation primitives only
 Additional features (command consol, networking stack etc.) can then be
included with add ons
 Free RTOS scheduler - preemptive, cooperative and hybrid configuration options
with optional time slicing.
 Supports MCUs from ARM, Atmel AVR, TI (Texas Instruments), NXP, Infineon,
ST Micro, Renesas
 You can use free RTOS with tools such as keil, arduino, STM32 IDE, GCC