0% found this document useful (0 votes)
349 views10 pages

Numerical

Uploaded by

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

Numerical

Uploaded by

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

1.

Non-preemptive Scheduling
First-Come-First-Served (FCFS)/ FIFO Scheduling

Example 1
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. The waiting time for all
processes are given as
Waiting Time for P1 = 0 ms (P1 starts executing fi rst)
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.
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
Average Turn Around Time = Average waiting time + Average execution time
= 8.33 + 7.33
= 15.66 milliseconds

Example 2
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) for the above
example if the process
enters the ‘Ready’ queue together in the order P2, P1, P3.
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 P2, P2 starts executing
without any waiting in
the ‘Ready’ queue. Hence the waiting time for P2 is zero. The waiting time for all
processes is given as
Waiting Time for P2 = 0 ms (P2 starts executing fi rst)
Waiting Time for P1 = 5 ms (P1 starts executing after completing P2)
Waiting Time for P3 = 15 ms (P3 starts executing after completing P2 and P1)
Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P2+P1+P3)) / 3
= (0+5+15)/3 = 20/3
= 6.66 milliseconds
Turn Around Time (TAT) for P2 = 5 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P1 = 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 (P2+P1+P3)) / 3
= (5+15+22)/3 = 42/3
= 14 milliseconds
The Average waiting time and Turn Around Time (TAT) depends on the order in which the
processes
enter the ‘Ready’ queue, regardless there estimated completion time.

The major drawback of FCFS algorithm is that it favours monopoly of process. A process,
which does
not contain any I/O operation, continues its execution until it fi nishes its task. If the
process contains any
I/O operation, the CPU is relinquished by the process. In general, FCFS favours 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 utilisation. The average
waiting time is not
minimal for FCFS scheduling algorithm.
10.5.1.2 Last-Come-First Served (LCFS)

Example 1
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 whenthe scheduler picks it up and P2, P3 entered ‘Ready’ queue after
that). Now a new process P4 with estimated
completion time 6 ms enters the ‘Ready’ queue after 5 ms 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.
The waiting time for all the processes is given as
Waiting Time for P1 = 0 ms (P1 starts executing fi rst)
Waiting Time for P4 = 5 ms ( P4 starts executing after completing P1. But P4 arrived after
5 ms 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

Shortest Job First (SJF) Scheduling


Example 1
Three processes with process IDs P1, P2, P3 with estimated completion time 10, 5, 7
milliseconds 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
SJF algorithm.
The scheduler sorts the ‘Ready’ queue based on the shortest estimated completion time
and schedules the
process with the least estimated completion time fi rst and the next least one as second,
and so on. The order
in which the processes are scheduled for execution is represented as

The estimated execution time of P2 is the least (5 ms) followed by P3 (7 ms) and P1 (10
ms).
The waiting time for all processes are given as
Waiting Time for P2 = 0 ms (P2 starts executing fi rst)
Waiting Time for P3 = 5 ms (P3 starts executing after completing P2)
Waiting Time for P1 = 12 ms (P1 starts executing after completing P2 and P3)
Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P2+P3+P1)) / 3
= (0+5+12)/3 = 17/3
= 5.66 milliseconds
Turn Around Time (TAT) for P2 = 5 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P3 = 12 ms (-Do-)
Turn Around Time (TAT) for P1 = 22 ms (-Do-)
Average Turn Around Time = (Turn Around Time for all processes) / No. of Processes
= (Turn Around Time for (P2+P3+P1)) / 3
= (5+12+22)/3 = 39/3
= 13 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
Average Turn Around Time = Average Waiting time + Average Execution time
= 5.66 + 7.33
= 13 milliseconds
From this example, it is clear that the average waiting time and turn around time is much
improved with the
SJF scheduling for the same processes when compared to the FCFS algorithm.

Example 2
Calculate the waiting time and Turn Around Time (TAT) for each process and the Average
waiting time and
Turn Around Time for the above example if a new process P4 with estimated completion
time 2 ms enters
the ‘Ready’ queue after 2 ms of execution of P2. 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 SJF scheduler picks up the process with the least execution completion time (In this
example P2 with execution completion time 5 ms) for scheduling. The execution
sequence diagram for this is same as that of
Example 1.
Now process P4 with estimated execution completion time 2 ms enters the ‘Ready’
queue after 2 ms of
start of execution of P2. Since the SJF algorithm is non-preemptive and process P2 does
not contain any I/O
operations, P2 continues its execution. After 5 ms of scheduling, P2 terminates and now
the scheduler again
sorts the ‘Ready’ queue for process with least execution completion time. Since the
execution completion
time for P4 (2 ms) is less than that of P3 (7 ms), which was supposed to be run after the
completion of P2
as per the ‘Ready’ queue available at the beginning of execution scheduling, P4 is picked
up for executing.
Due to the arrival of the process P4 with execution time 2 ms, the ‘Ready’ queue is re-
sorted in the order P2,
P4, P3, P1. At the beginning it was P2, P3, P1. The execution sequence now changes as
per the following
Diagram

The waiting time for all the processes are given as


Waiting time for P2 = 0 ms (P2 starts executing fi rst)
Waiting time for P4 = 3 ms (P4 starts executing after completing P2. But P4 arrived after
2 ms of execution
of P2. Hence its waiting time = Execution start time – Arrival Time = 5 – 2 = 3)
Waiting time for P3 = 7 ms (P3 starts executing after completing P2 and P4)
Waiting time for P1 = 14 ms (P1 starts executing after completing P2, P4 and P3)
Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P2+P4+P3+P1)) / 4
= (0 + 3 + 7 + 14)/4 = 24/4
= 6 milliseconds
Turn Around Time (TAT) for P2 = 5 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P4 = 5 ms ( Time spent in Ready Queue + Execution Time =
(Execution Start
Time – Arrival Time) + Estimated Execution Time = (5 – 2) + 2
= 3 + 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 Processes) / No. of Processes
= (Turn Around Time for (P2+P4+P3+P1)) / 4
= (5+5+14+24)/4 = 48/4
= 12 milliseconds
The average waiting time for a given set of process is minimal in SJF scheduling and so it
is optimal
compared to other non-preemptive scheduling like FCFS. The major drawback of SJF
algorithm is that
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 started its execution (In non-preemptive SJF ). This
condition is known as
‘Starvation’. Another drawback of SJF is that it is diffi cult 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.

Priority Based Scheduling


Example 1
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) fi rst 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
The waiting time for all the processes are given as
Waiting time for P1 = 0 ms (P1 starts executing fi rst)
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
Example 2
Calculate the waiting time and Turn Around Time (TAT) for each process and the Average
waiting time and
Turn Around Time for the above example if a new process P4 with estimated completion
time 6 ms and
priority 1 enters the ‘Ready’ queue after 5 ms 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
0) for scheduling.
The execution sequence diagram for this is same as that of Example 1. Now process P4
with estimated
execution completion time 6 ms and priority 1 enters the ‘Ready’ queue after 5 ms of
execution of P1. Since
the scheduling algorithm is non-preemptive and process P1 does not contain any I/O
operations, P1 continues
its execution. After 10 ms of scheduling, P1 terminates and now the scheduler again
sorts the ‘Ready’ queue
for process with highest priority. Since the priority for P4 (priority 1) is higher than that of
P3 (priority 2),
which was supposed to be run after the completion of P1 as per the ‘Ready’ queue
available at the beginning
of execution scheduling, P4 is picked up for executing. Due to the arrival of the process
P4 with priority 1,
the ‘Ready’ queue is resorted in the order P1, P4, P3, P2. At the beginning it was P1, P3,
P2. The execution
sequence now changes as per the following diagram

The waiting time for all the processes are given as


Waiting time for P1 = 0 ms (P1 starts executing fi rst)
Waiting time for P4 = 5 ms (P4 starts executing after completing P1. But P4 arrived after
5 ms 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 (P2 + P4 + P3 + P1)) / 4
= (10 + 11 + 23 + 28)/4 = 72/4
= 18 milliseconds

Preemptive Scheduling
In
preemptive scheduling, 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. In this kind of
scheduling, the scheduler can preempt (stop temporarily)
the currently executing task/process and select another task from the ‘Ready’ queue for
execution
Preemp ve SJF Scheduling/ Shortest Remaining Time (SRT)
Example 1
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 2 ms
enters the ‘Ready’
queue after 2 ms. 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 5 ms) for scheduling. The execution sequence diagram
for this is same as
that of example 1 under non-preemptive SJF scheduling.
Now process P4 with estimated execution completion time 2 ms enters the ‘Ready’
queue after 2 ms of start of execution of P2. Since the SRT algorithm is preemptive, the
remaining time for completion of process
P2 is checked with the remaining time for completion of process P4. The remaining time
for completion of
P2 is 3 ms which is greater than that of the remaining time for completion of the newly
entered process P4
(2 ms). Hence P2 is preempted and P4 is scheduled for execution. P4 continues its
execution to fi nish since
there is no new process entered in the ‘Ready’ queue during its execution. After 2 ms of
scheduling P4
terminates and now the scheduler again sorts the ‘Ready’ queue based on the remaining
time for completion
of the processes present in the ‘Ready’ queue. Since the remaining time for P2 (3 ms),
which is preempted
by P4 is less than that of the remaining time for other processes in the ‘Ready’ queue, P2
is scheduled for
execution. Due to the arrival of the process P4 with execution time 2 ms, the ‘Ready’
queue is re-sorted in
the order P2, P4, P2, P3, P1. At the beginning it was P2, P3, P1. The execution sequence
now changes as per
the following diagram

The waiting time for all the processes are given as


Waiting time for P2 = 0 ms + (4 – 2) ms = 2 ms (P2 starts executing fi rst 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 (2 ms) is less than that of the Remaining time for execution completion of P2
(Here it is 3 ms))
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
Now let’s compare the Average Waiting time and Average Turn Around Time with that of
the Average
waiting time and Average Turn Around Time for non-preemptive SJF scheduling (Refer to
Example 2 given
under the section Non-preemptive SJF scheduling)
Average Waiting Time in non-preemptive SJF scheduling = 6 ms
Average Waiting Time in preemptive SJF scheduling = 5.75 ms
Average Turn Around Time in non-preemptive SJF scheduling = 12 ms
Average Turn Around Time in preemptive SJF scheduling = 11.75 ms

10.5.2.2 Round Robin (RR) Scheduling


The term Round Robin is very popular among the sports and games activities. You might
have heard about
‘Round Robin’ league or ‘Knock out’ league associated with any football or cricket
tournament. In the ‘Round
Robin’ league each team in a group gets an equal chance to play against the rest of the
teams in the same
group whereas in the ‘Knock out’ league the losing team in a match moves out of the
tournament ☺.
In the process scheduling context also, ‘Round Robin’ brings the same message “Equal
chance to all”.
In Round Robin scheduling, each process in the ‘Ready’ queue is executed for a pre-defi
ned time slot. The
execution starts with picking up the fi rst process in the ‘Ready’ queue (see Fig. 10.13). It
is executed for a
pre-defi ned time and when the pre-defi ned time elapses or the process completes
(before the pre-defi ned 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-
defi ned time period,
the scheduler comes back and picks the fi rst process in the ‘Ready’ queue again for
execution. The sequence
is repeated. This reveals that the 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. The ‘Ready’ queue can be considered as a circular queue in which the
scheduler picks
up the fi rst process for execution and moves to the next till the end of the queue and
then comes back to the
beginning of the queue to pick up the fi rst process.

The time slice is provided by the timer tick feature of the time management unit of the
OS kernel (Refer the
Time management section under the subtopic ‘The Real-Time kernel’ for more details on
Timer tick). Time
slice is kernel dependent and it varies in the order of a few microseconds to milliseconds.
Certain OS kernels
may allow the time slice as user confi gurable. Round Robin scheduling ensures that
every process gets a fi xed
amount of CPU time for execution. When the process gets its fi xed time for execution is
determined by the FCFS policy (That is, a process entering the Ready queue fi rst gets its
fi xed execution time fi rst and so on…).
If a process terminates before the elapse of the time slice, the process releases the CPU
voluntarily and the
next process in the queue is scheduled for execution by the scheduler. The
implementation of RR scheduling
is kernel dependent. The following code snippet illustrates the RR scheduling
implementation for RTX51
Tiny OS, an 8bit OS for 8051 microcontroller from Keil Software (www.keil.com), an
ARM® Company.

You might also like