Numerical
Numerical
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
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
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 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.