[go: up one dir, main page]

0% found this document useful (0 votes)
146 views34 pages

RTOS Multitasking

The document discusses various concepts related to real-time operating systems including multiprocessing, multitasking, context switching, preemptive and non-preemptive multitasking, task scheduling, queues used in scheduling, and non-preemptive scheduling algorithms like first come first served and last come first served.

Uploaded by

Bhuvana Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views34 pages

RTOS Multitasking

The document discusses various concepts related to real-time operating systems including multiprocessing, multitasking, context switching, preemptive and non-preemptive multitasking, task scheduling, queues used in scheduling, and non-preemptive scheduling algorithms like first come first served and last come first served.

Uploaded by

Bhuvana Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Designing with RTOS

✔ Till slide 18- Scheduling Problems for test2


..
Designing with RTOS
Multiprocessing & Multitasking
✔ The ability to execute multiple processes simultaneously is referred as multiprocessing
✔ Systems which are capable of performing multiprocessing are known as multiprocessor
systems
✔ Multiprocessor systems possess multiple CPUs and can execute multiple processes
simultaneously
✔ The ability of the Operating System to have multiple programs in memory, which are ready
for execution, is referred as multiprogramming
✔ Multitasking refers to the ability of an operating system to hold multiple processes in memory
and switch the processor (CPU) from executing one process to another process
✔ Multitasking involves ‘Context switching’, ‘Context saving’ and ‘Context retrieval’
✔ Context switching refers to the switching of execution context from task to other
✔ When a task/process switching happens, the current context of execution should be saved to
(Context saving) retrieve it at a later point of time when the CPU executes the process, which
is interrupted currently due to execution switching
✔ During context switching, the context of the task to be executed is retrieved from the saved
context list. This is known as Context retrieval
Designing with RTOS
Multitasking – Context Switching
Designing with RTOS
Types of Multitasking
Depending on how the task/process execution switching act is implemented, multitasking
can is classified into
• Co-operative Multitasking: Co-operative multitasking is the most primitive form of
multitasking in which a task/process gets a chance to execute only when the currently executing
task/process voluntarily relinquishes the CPU. In this method, any task/process can avail the CPU
as much time as it wants. Since this type of implementation involves the mercy of the tasks each
other for getting the CPU time for execution, it is known as co-operative multitasking. If the
currently executing task is non-cooperative, the other tasks may have to wait for a long time to get
the CPU
• Preemptive Multitasking: 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 the preemptive scheduling. As the name indicates, in preemptive multitasking, the currently
running task/process is preempted to give a chance to other tasks/process to execute. The
preemption of task may be based on time slots or task/process priority
•Non-preemptive Multitasking: The process/task, which is currently given the CPU time, is
allowed to execute until it terminates (enters the ‘Completed’ state) or enters the ‘Blocked/Wait’ state,
waiting for an I/O. The co-operative and non-preemptive multitasking differs in their behavior when
they are in the ‘Blocked/Wait’ state. In co-operative multitasking, the currently executing process/task
need not relinquish the CPU when it enters the ‘Blocked/Wait’ sate, waiting for an I/O, or a shared
resource access or an event to occur whereas in non-preemptive multitasking the currently executing
task relinquishes the CPU when it waits for an I/O.
Designing with RTOS
Task Scheduling
✔ In a multitasking system, there should be some mechanism in place to share the
CPU among the different tasks and to decide which process/task is to be
executed at a given point of time
✔ Determining which task/process is to be executed at a given point of time is
known as task/process scheduling
✔ Task scheduling forms the basis of multitasking
✔ Scheduling policies forms the guidelines for determining which task is to be
executed when
✔ The scheduling policies are implemented in an algorithm and it is run by the
kernel as a service
✔ The kernel service/application, which implements the scheduling algorithm, is
known as ‘Scheduler’
✔ The task scheduling policy can be pre-emptive, non-preemptive or co-operative
Designing with RTOS
Task Scheduling
✔ Depending on the scheduling policy the process scheduling decision may take
place when a process switches its state to
✔ ‘Ready’ state from ‘Running’ state
✔ ‘Blocked/Wait’ state from ‘Running’ state
✔ ‘Ready’ state from ‘Blocked/Wait’ state
✔ ‘Completed’ state
Designing with RTOS
Task Scheduling - Scheduler Selection
The selection of a scheduling criteria/algorithm should consider
•CPU Utilization: The scheduling algorithm should always make the CPU utilization high.
CPU utilization is a direct measure of how much percentage of the CPU is being utilized.
•Throughput: This gives an indication of the number of processes executed per unit of
time. The throughput for a good scheduler should always be higher.
•Turnaround Time: It is the amount of time taken by a process for completing its
execution. It includes the time spent by the process for waiting for the main memory, time
spent in the ready queue, time spent on completing the I/O operations, and the time spent
in execution. The turnaround time should be a minimum for a good scheduling algorithm.
•Waiting Time: It is the amount of time spent by a process in the ‘Ready’ queue waiting to
get the CPU time for execution. The waiting time should be minimal for a good scheduling
algorithm.
•Response Time: It is the time elapsed between the submission of a process and the first
response. For a good scheduling algorithm, the response time should be as least as
possible.
Designing with RTOS
Task Scheduling - Queues
The various queues maintained by OS in association with CPU scheduling are
• Job Queue: Job queue contains all the processes in the system
• Ready Queue: Contains all the processes, which are ready for execution and
waiting for CPU to get their turn for execution. The Ready queue is empty
when there is no process ready for running.
• Device Queue: Contains the set of processes, which are waiting for an I/O
device
Designing with RTOS
Task Scheduling – Task transition through various Queues
Designing with RTOS
Non-preemptive scheduling – First Come First Served (FCFS)/
First In First Out (FIFO) Scheduling

✔ Allocates CPU time to the processes based on the order in which they enters the
‘Ready’ queue
✔ The first entered process is serviced first
✔ It is same as any real world application where queue systems are used; E.g.
Ticketing
Drawbacks:
✔ Favors monopoly of process. A process, which does not contain any I/O
operation, continues its execution until it finishes its task
✔ In general, FCFS favors CPU bound processes and I/O bound processes may have to
wait until the completion of CPU bound process, if the currently executing process is a
CPU bound process. This leads to poor device utilization.
✔ The average waiting time is not minimal for FCFS scheduling algorithm
Designing with RTOS
Non-preemptive scheduling – First Come First Served (FCFS)/
First In First Out (FIFO) Scheduling
• Three processes with process IDs P1, P2, P3 with estimated completion time
10, 5, 7 milliseconds respectively enters the ready queue together in the order
P1, P2, P3. Calculate the waiting time and Turn Around Time (TAT) for each
process and the Average waiting time and Turn Around Time (Assuming there
is no I/O waiting for the processes).
The sequence of execution of the processes by the CPU is represented as

Assuming the CPU is readily available at the time of arrival of P1, P1 starts
executing without any waiting in the ‘Ready’ queue. Hence the waiting time for
P1 is zero.
Designing with RTOS
Non-preemptive scheduling – First Come First Served (FCFS)/
First In First Out (FIFO) Scheduling
Waiting Time for P1 = 0 ms (P1 starts executing first)
Waiting Time for P2 = 10 ms (P2 starts executing after completing P1)
Waiting Time for P3 = 15 ms (P3 starts executing after completing P1 and P2)
Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P1+P2+P3)) / 3
= (0+10+15)/3 = 25/3 = 8.33 milliseconds
Turn Around Time (TAT) for P1 = 10 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P2 = 15 ms (-Do-)
Turn Around Time (TAT) for P3 = 22 ms (-Do-)
Average Turn Around Time= (Turn Around Time for all processes) / No. of Processes
= (Turn Around Time for (P1+P2+P3)) / 3
= (10+15+22)/3 = 47/3
= 15.66 milliseconds
Average Turn Around Time (TAT) is the sum of average waiting time and average execution time.
The average Execution time = (Execution time for all processes)/No. of processes
= (Execution time for (P1+P2+P3))/3
= (10+5+7)/3 = 22/3 = 7.33 milliseconds
Average Turn Around Time = Average Waiting time + Average execution time
= 8.33 + 7.33
= 15.66 milliseconds
Designing with RTOS
Non-preemptive scheduling – Last Come First Served (LCFS)/
Last In First Out (LIFO) Scheduling

✔ Allocates CPU time to the processes based on the order in which they are
entered in the ‘Ready’ queue
✔ The last entered process is serviced first
✔ LCFS scheduling is also known as Last In First Out (LIFO) where the process,
which is put last into the ‘Ready’ queue, is serviced first

Drawbacks:
✔ Favors monopoly of process. A process, which does not contain any I/O
operation, continues its execution until it finishes its task
✔ In general, LCFS favors CPU bound processes and I/O bound processes may
have to wait until the completion of CPU bound process, if the currently
executing process is a CPU bound process. This leads to poor device utilization.
✔ The average waiting time is not minimal for LCFS scheduling algorithm
Designing with RTOS
Non-preemptive scheduling – Last Come First Served (LCFS)/
Last In First Out (LIFO) Scheduling
• Three processes with process IDs P1, P2, P3 with estimated completion time 10, 5, 7 milliseconds
respectively enters the ready queue together in the order P1, P2, P3 (Assume only P1 is present in
the ‘Ready’ queue when the scheduler picks up it and P2, P3 entered ‘Ready’ queue after that). Now
a new process P4 with estimated completion time 6ms enters the ‘Ready’ queue after 5ms of
scheduling P1. Calculate the waiting time and Turn Around Time (TAT) for each process and the
Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the
processes).Assume all the processes contain only CPU operation and no I/O operations are involved.

Initially there is only P1 available in the Ready queue and the scheduling sequence will be P1, P3, P2.
P4 enters the queue during the execution of P1 and becomes the last process entered the ‘Ready’
queue. Now the order of execution changes to P1, P4, P3, and P2 as given below.
Designing with RTOS
Non-preemptive scheduling – Last Come First Served (LCFS)/
Last In First Out (LIFO) Scheduling
The waiting time for all the processes are given as

Waiting Time for P1 = 0 ms (P1 starts executing first)


Waiting Time for P4 = 5 ms (P4 starts executing after completing P1. But P4 arrived
after 5ms of execution of P1. Hence its waiting time = Execution start time – Arrival Time = 10-5 = 5)
Waiting Time for P3 = 16 ms (P3 starts executing after completing P1 and P4)
Waiting Time for P2 = 23 ms (P2 starts executing after completing P1, P4 and P3)
Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P1+P4+P3+P2)) / 4
= (0 + 5 + 16 + 23)/4 = 44/4
= 11 milliseconds

Turn Around Time (TAT) for P1 = 10 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P4 = 11 ms (Time spent in Ready Queue + Execution Time = (Execution Start Time – Arrival
Time) + Estimated Execution Time = (10-5) + 6 = 5 + 6)
Turn Around Time (TAT) for P3 = 23 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P2 = 28 ms (Time spent in Ready Queue + Execution Time)

Average Turn Around Time = (Turn Around Time for all processes) / No. of Processes
= (Turn Around Time for (P1+P4+P3+P2)) / 4
= (10+11+23+28)/4 = 72/4
= 18 milliseconds
Designing with RTOS
Non-preemptive scheduling – Shortest Job First (SJF) Scheduling

✔ Allocates CPU time to the processes based on the execution completion time
for tasks
✔ The average waiting time for a given set of processes is minimal in SJF
scheduling
✔ Optimal compared to other non-preemptive scheduling like FCFS
Drawbacks:
✔ A process whose estimated execution completion time is high may not get a
chance to execute if more and more processes with least estimated execution
time enters the ‘Ready’ queue before the process with longest estimated
execution time starts its execution
✔ May lead to the ‘Starvation’ of processes with high estimated completion time
✔ Difficult to know in advance the next shortest process in the ‘Ready’ queue for
scheduling since new processes with different estimated execution time keep
entering the ‘Ready’ queue at any point of time.
Designing with RTOS
Non-preemptive scheduling – Shortest Job First (SJF) Scheduling

• Three processes with process IDs P1, P2, P3 with estimated completion time 10, 5, 7 milliseconds
respectively enters the ready queue together in the order P1, P2, P3 (Assume only P1 is present in
the ‘Ready’ queue when the scheduler picks up it and P2, P3 entered ‘Ready’ queue after that). Now
a new process P4 with estimated completion time 6ms enters the ‘Ready’ queue after 5ms of
scheduling P1. Calculate the waiting time and Turn Around Time (TAT) for each process and the
Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the
processes).Assume all the processes contain only CPU operation and no I/O operations are involved.

Initially there is only P1 available in the Ready queue and the scheduling sequence will be P1, P3, P2.
P4 enters the queue during the execution of P1 and becomes the last process entered the ‘Ready’
queue. Now the order of execution changes to P1, P4, P3, and P2 as given below.
Designing with RTOS
Non-preemptive scheduling – Shortest Job First (SJF) Scheduling

The waiting time for all the processes are given as

Waiting Time for P1 = 0 ms (P1 starts executing first)


Waiting Time for P4 = 5 ms (P4 starts executing after completing P1. But P4 arrived
after 5ms of execution of P1. Hence its waiting time = Execution start time – Arrival Time = 10-5 = 5)
Waiting Time for P3 = 16 ms (P3 starts executing after completing P1 and P4)
Waiting Time for P2 = 23 ms (P2 starts executing after completing P1, P4 and P3)
Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P1+P4+P3+P2)) / 4
= (0 + 5 + 16 + 23)/4 = 44/4
= 11 milliseconds

Turn Around Time (TAT) for P1 = 10 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P4 = 11 ms (Time spent in Ready Queue + Execution Time = (Execution Start Time – Arrival
Time) + Estimated Execution Time = (10-5) + 6 = 5 + 6)
Turn Around Time (TAT) for P3 = 23 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P2 = 28 ms (Time spent in Ready Queue + Execution Time)

Average Turn Around Time = (Turn Around Time for all processes) / No. of Processes
= (Turn Around Time for (P1+P4+P3+P2)) / 4
= (10+11+23+28)/4 = 72/4
= 18 milliseconds
Designing with RTOS
Non-preemptive scheduling – Priority based Scheduling

✔ A priority, which is unique or same is associated with each task


✔ The priority of a task is expressed in different ways, like a priority number, the
time required to complete the execution etc.
✔ In number based priority assignment the priority is a number ranging from 0 to
the maximum priority supported by the OS. The maximum level of priority is
OS dependent.
✔ Windows CE supports 256 levels of priority (0 to 255 priority numbers, with 0
being the highest priority)
✔ The priority is assigned to the task on creating it. It can also be changed
dynamically (If the Operating System supports this feature)
✔ The non-preemptive priority based scheduler sorts the ‘Ready’ queue based on
the priority and picks the process with the highest level of priority for execution
Designing with RTOS
Non-preemptive scheduling – Priority based Scheduling

• Three processes with process IDs P1, P2, P3 with estimated completion time 10, 5, 7 milliseconds
and priorities 0, 3, 2 (0- highest priority, 3 lowest priority) respectively enters the ready queue
together. Calculate the waiting time and Turn Around Time (TAT) for each process and the
Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the processes)
in priority based scheduling algorithm.

• The scheduler sorts the ‘Ready’ queue based on the priority and schedules the process with the
highest priority (P1 with priority number 0) first and the next high priority process (P3 with
priority number 2) as second and so on. The order in which the processes are scheduled for
execution is represented as
Designing with RTOS
Non-preemptive scheduling – Priority based Scheduling
The waiting time for all the processes are given as

Waiting Time for P1 = 0 ms (P1 starts executing first)


Waiting Time for P3 = 10 ms (P3 starts executing after completing P1)
Waiting Time for P2 = 17 ms (P2 starts executing after completing P1 and P3)

Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P1+P3+P2)) / 3
= (0+10+17)/3 = 27/3
= 9 milliseconds

Turn Around Time (TAT) for P1 = 10 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P3 = 17 ms (-Do-)
Turn Around Time (TAT) for P2 = 22 ms (-Do-)

Average Turn Around Time = (Turn Around Time for all processes) / No. of Processes
= (Turn Around Time for (P1+P3+P2)) / 3
= (10+17+22)/3 = 49/3
= 16.33 milliseconds
Designing with RTOS
Non-preemptive scheduling – Priority based Scheduling
Drawbacks:
✔ Similar to SJF scheduling algorithm, non-preemptive priority based algorithm
also possess the drawback of ‘Starvation’ where a process whose priority is low
may not get a chance to execute if more and more processes with higher
priorities enter the ‘Ready’ queue before the process with lower priority starts
its execution.
✔ ‘Starvation’ can be effectively tackled in priority based non-preemptive
scheduling by dynamically raising the priority of the low priority task/process
which is under starvation (waiting in the ready queue for a longer time for
getting the CPU time)
✔ The technique of gradually raising the priority of processes which are waiting in
the ‘Ready’ queue as time progresses, for preventing ‘Starvation’, is known as
‘Aging’.
Designing with RTOS
Preemptive scheduling

✔ Employed in systems, which implements preemptive multitasking model


✔ Every task in the ‘Ready’ queue gets a chance to execute. When and how often
each process gets a chance to execute (gets the CPU time) is dependent on the
type of preemptive scheduling algorithm used for scheduling the processes
✔ The scheduler can preempt (stop temporarily) the currently executing
task/process and select another task from the ‘Ready’ queue for execution
✔ When to pre-empt a task and which task is to be picked up from the ‘Ready’
queue for execution after preempting the current task is purely dependent on the
scheduling algorithm
✔ A task which is preempted by the scheduler is moved to the ‘Ready’ queue. The
act of moving a ‘Running’ process/task into the ‘Ready’ queue by the scheduler,
without the processes requesting for it is known as ‘Preemption’
✔ Time-based preemption and priority-based preemption are the two important
approaches adopted in preemptive scheduling
Designing with RTOS
Preemptive scheduling – Preemptive SJF Scheduling/
Shortest Remaining Time (SRT)
✔ The non preemptive SJF scheduling algorithm sorts the ‘Ready’ queue only
after the current process completes execution or enters wait state, whereas the
preemptive SJF scheduling algorithm 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 execution time
of the currently executing process
✔ If the execution time of the new process is less, the currently executing process
is preempted and the new process is scheduled for execution
✔ Always compares the execution completion time (ie the remaining execution
time for the new process) of a new process entered the ‘Ready’ queue with the
remaining time for completion of the currently executing process and schedules
the process with shortest remaining time for execution
Designing with RTOS
Preemptive scheduling – Preemptive SJF Scheduling

• Three processes with process IDs P1, P2, P3 with estimated completion time 10, 5, 7 milliseconds
respectively enters the ready queue together. A new process P4 with estimated completion time
2ms enters the ‘Ready’ queue after 2ms. Assume all the processes contain only CPU operation and
no I/O operations are involved.

• At the beginning, there are only three processes (P1, P2 and P3) available in the ‘Ready’ queue
and the SRT scheduler picks up the process with the Shortest remaining time for execution
completion (In this example P2 with remaining time 5ms) for scheduling. Now process P4 with
estimated execution completion time 2ms enters the ‘Ready’ queue after 2ms of start of execution
of P2. The processes are re-scheduled for execution in the following order
Designing with RTOS
Non-preemptive scheduling – Preemptive SJF Scheduling
The waiting time for all the processes are given as

Waiting Time for P2 = 0 ms + (4 -2) ms = 2ms (P2 starts executing first and is interrupted by P4 and has to wait till the completion of
P4 to get the next CPU slot)
Waiting Time for P4 = 0 ms (P4 starts executing by preempting P2 since the execution time for completion of P4 (2ms) is less than
that of the Remaining time for execution completion of P2 (Here it is 3ms))
Waiting Time for P3 = 7 ms (P3 starts executing after completing P4 and P2)
Waiting Time for P1 = 14 ms (P1 starts executing after completing P4, P2 and P3)
Average waiting time = (Waiting time for all the processes) / No. of Processes
= (Waiting time for (P4+P2+P3+P1)) / 4
= (0 + 2 + 7 + 14)/4 = 23/4
= 5.75 milliseconds

Turn Around Time (TAT) for P2 = 7 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P4 = 2 ms
(Time spent in Ready Queue + Execution Time = (Execution Start Time – Arrival Time) + Estimated Execution Time = (2-2) + 2)
Turn Around Time (TAT) for P3 = 14 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P1 = 24 ms (Time spent in Ready Queue + Execution Time)

Average Turn Around Time = (Turn Around Time for all the processes) / No. of Processes
= (Turn Around Time for (P2+P4+P3+P1)) / 4
= (7+2+14+24)/4 = 47/4
= 11.75 milliseconds
Designing with RTOS
Preemptive scheduling – Round Robin (RR) Scheduling

✔ Each process in the ‘Ready’ queue is executed for a


pre-defined time slot.
✔ The execution starts with picking up the first process
in the ‘Ready’ queue. It is executed for a pre-defined
time
✔ When the pre-defined time elapses or the process
completes (before the pre-defined time slice), the next
process in the ‘Ready’ queue is selected for execution.
✔ This is repeated for all the processes in the ‘Ready’
queue
✔ Once each process in the ‘Ready’ queue is executed
for the pre-defined time period, the scheduler comes
back and picks the first process in the ‘Ready’ queue
again for execution
✔ Round Robin scheduling is similar to the FCFS
scheduling and the only difference is that a time slice
based preemption is added to switch the execution
between the processes in the ‘Ready’ queue
Designing with RTOS
Preemptive scheduling – Round Robin Scheduling

• Three processes with process IDs P1, P2, P3 with estimated completion time 6, 4, 2 milliseconds
respectively, enters the ready queue together in the order P1, P2, P3. Calculate the waiting time and
Turn Around Time (TAT) for each process and the Average waiting time and Turn Around Time
(Assuming there is no I/O waiting for the processes) in RR algorithm with Time slice= 2ms.

• The scheduler sorts the ‘Ready’ queue based on the FCFS policy and picks up the first process P1
from the ‘Ready’ queue and executes it for the time slice 2ms. When the time slice is expired, P1 is
preempted and P2 is scheduled for execution. The Time slice expires after 2ms of execution of P2.
Now P2 is preempted and P3 is picked up for execution. P3 completes its execution within the time
slice and the scheduler picks P1 again for execution for the next time slice. This procedure is
repeated till all the processes are serviced. The order in which the processes are scheduled for
execution is represented as
Designing with RTOS
Non-preemptive scheduling – Round Robin Scheduling
The waiting time for all the processes are given as

Waiting Time for P1 = 0 + (6-2) + (10-8) = 0+4+2= 6ms (P1 starts executing first and waits for two time slices to get
execution back and again 1 time slice for getting CPU time)
Waiting Time for P2 = (2-0) + (8-4) = 2+4 = 6ms (P2 starts executing after P1 executes for 1 time slice and waits for two
time slices to get the CPU time)
Waiting Time for P3 = (4 -0) = 4ms (P3 starts executing after completing the first time slices for P1 and P2 and completes its
execution in a single time slice.)
Average waiting time = (Waiting time for all the processes) / No. of Processes
= (Waiting time for (P1+P2+P3)) / 3
= (6+6+4)/3 = 16/3
= 5.33 milliseconds

Turn Around Time (TAT) for P1 = 12 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P2 = 10 ms (-Do-)
Turn Around Time (TAT) for P3 = 6 ms (-Do-)

Average Turn Around Time= (Turn Around Time for all the processes) / No. of Processes
= (Turn Around Time for (P1+P2+P3)) / 3
= (12+10+6)/3 = 28/3
= 9.33 milliseconds
Designing with RTOS
Preemptive scheduling – Priority based Scheduling

✔ Same as that of the non-preemptive priority based scheduling except for the
switching of execution between tasks
✔ In preemptive priority based scheduling, any high priority process entering the
‘Ready’ queue is immediately scheduled for execution whereas in the
non-preemptive scheduling any high priority process entering the ‘Ready’
queue is scheduled only after the currently executing process completes its
execution or only when it voluntarily releases the CPU
✔ The priority of a task/process in preemptive priority based scheduling is
indicated in the same way as that of the mechanisms adopted for
non-preemptive multitasking
Designing with RTOS
Preemptive scheduling – Priority based Scheduling

• Three processes with process IDs P1, P2, P3 with estimated completion time 10, 5, 7 milliseconds
and priorities 1, 3, 2 (0- highest priority, 3 lowest priority) respectively enters the ready queue
together. A new process P4 with estimated completion time 6ms and priority 0 enters the ‘Ready’
queue after 5ms of start of execution of P1. Assume all the processes contain only CPU operation
and no I/O operations are involved.

• At the beginning, there are only three processes (P1, P2 and P3) available in the ‘Ready’ queue
and the scheduler picks up the process with the highest priority (In this example P1 with priority
1) for scheduling. Now process P4 with estimated execution completion time 6ms and priority 0
enters the ‘Ready’ queue after 5ms of start of execution of P1. The processes are re-scheduled for
execution in the following order
Designing with RTOS
Preemptive scheduling – Priority based Scheduling
The waiting time for all the processes are given as

Waiting Time for P1 = 0 + (11-5) = 0+6 =6 ms (P1 starts executing first and gets preempted by P4 after 5ms and
again gets the CPU time after completion of P4)
Waiting Time for P4 = 0 ms (P4 starts executing immediately on entering the ‘Ready’ queue, by preempting P1)
Waiting Time for P3 = 16 ms (P3 starts executing after completing P1 and P4)
Waiting Time for P2 = 23 ms (P2 starts executing after completing P1, P4 and P3)
Average waiting time = (Waiting time for all the processes) / No. of Processes
= (Waiting time for (P1+P4+P3+P2)) / 4
= (6 + 0 + 16 + 23)/4 = 45/4
= 11.25 milliseconds
Turn Around Time (TAT) for P1 = 16 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P4 = 6ms (Time spent in Ready Queue + Execution Time = (Execution Start Time –
Arrival Time) + Estimated Execution Time = (5-5) + 6 = 0 + 6)
Turn Around Time (TAT) for P3 = 23 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P2 = 28 ms (Time spent in Ready Queue + Execution Time)
Average Turn Around Time = (Turn Around Time for all the processes) / No. of Processes
= (Turn Around Time for (P2+P4+P3+P1)) / 4
= (16+6+23+28)/4 = 73/4
= 18.25 milliseconds
Designing with RTOS
Preemptive scheduling – Priority based Scheduling
Summary:
✔ Priority based preemptive scheduling gives real time attention to high priority
tasks
✔ Priority based preemptive scheduling is adopted in systems which demands
‘Real Time’ behavior
✔ Most of the RTOSs implements the preemptive priority based scheduling
algorithm for process/task scheduling
✔ Preemptive priority based scheduling also possess the same drawback of
non-preemptive priority based scheduling – ‘Starvation’
✔ This can be eliminated by the ‘Aging’ technique (Temporarily boosting the
priority of a task which is ‘starving’ for a long time
Multitasking and Task Scheduling
Learning Summary
✔ The ability of a system to execute multiple processes/tasks simultaneously is known as multiprocessing,
whereas the ability of an operating system to hold multiple processes in memory and switch the
processor (CPU) from executing one process to another process is known as multitasking.
✔ Multitasking involves Context Switching, Context Saving and Context Retrieval
✔ Co-operative multitasking, Preemptive multitasking and Non-preemptive multitasking are the three
important types of multitasking which exist in the Operating system context
✔ CPU utilization, Throughput, Turn Around Time (TAT), Waiting Time and Response Time are the
important criterions that need to be considered for the selection of a scheduling algorithm for task
scheduling
✔ A good scheduling algorithm provides high CPU utilization, minimum Turn Around Time (TAT),
maximum throughput and least response time.
✔ Job queue, Ready queue and Device queue are the important queues maintained by an operating system
in association with CPU scheduling
✔ First Come First Served (FCFS)/First in First Out (FIFO), Last Come First Served (LCFS)/Last in First
Out (LIFO), Shortest Job First (SJF), priority based scheduling etc are examples for Non-preemptive
scheduling,
✔ Preemptive SJF Scheduling/ Shortest Remaining Time (SRT), Round Robin (RR) scheduling and
priority based scheduling are examples for preemptive scheduling

You might also like