Module 9 Principles of CPU Scheduling
Module 9 Principles of CPU Scheduling
CPU scheduling is a process which allows one process to use the CPU while the execution of another process is on
hold (in waiting state) due to unavailability of any resource like I/O etc, thereby making full use of CPU. The aim of
CPU scheduling is to make the system efficient, fast and fair.
Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be
executed. The selection process is carried out by the short-term scheduler (or CPU scheduler). The scheduler selects
from among the processes in memory that are ready to execute, and allocates the CPU to one of them.
Maximize:
CPU utilization: CPU utilization is the main task in which the operating system needs to make sure that CPU
remains as busy as possible. It can range from 0 to 100 percent. However, for the RTOS, it can be range from 40
percent for low-level and 90 percent for the high-level system.
Throughput: The number of processes that finish their execution per unit time is known Throughput. So, when the
CPU is busy executing the process, at that time, work is being done, and the work completed per unit time is called
Throughput.
Minimize:
Waiting time: Waiting time is an amount that specific process needs to wait in the ready queue.
Response time: It is an amount to time in which the request was submitted until the first response is produced.
Turnaround Time: Turnaround time is an amount of time to execute a specific process. It is the calculation of the
total time spent waiting to get into the memory, waiting in the queue and, executing on the CPU. The period
between the time of process submission to the completion time is the turnaround time.
Interval Timer
Timer interruption is a method that is closely related to preemption. When a certain process gets the CPU allocation,
a timer may be set to a specified interval. Both timer interruption and preemption force a process to return the CPU
before its CPU burst is complete.
Most of the multi-programmed operating system uses some form of a timer to prevent a process from tying up the
system forever.
CPU Scheduling: Dispatcher
Another component involved in the CPU scheduling function is the Dispatcher. The dispatcher is the module that
gives control of the CPU to the process selected by the short-term scheduler. This function involves:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that program from where it left last time.
The dispatcher should be as fast as possible, given that it is invoked during every process switch. The time taken by
the dispatcher to stop one process and start another process is known as the Dispatch Latency. Dispatch Latency can
be explained using the below figure:
Non-Preemptive Scheduling
Under non-preemptive scheduling, once the CPU has been allocated to a process, the process keeps the
CPU until it releases the CPU either by terminating or by switching to the waiting state.
This scheduling method is used by the Microsoft Windows 3.1 and by the Apple Macintosh operating
systems.
It is the only method that can be used on certain hardware platforms, because It does not require the special
hardware (for example: a timer) needed for preemptive scheduling.
Preemptive Scheduling
In this type of Scheduling, the tasks are usually assigned with priorities. At times it is necessary to run a
certain task that has a higher priority before another task although it is running. Therefore, the running task
is interrupted for some time and resumed later when the priority task has finished its execution.
• Turnaround time – the interval from the time a process is submitted until it is completed; compute for the
time the process is in the ready queue, executing & doing I/O;
• Response time – for an interactive process, this refers to the time between the submission of the process
until it starts responding.
• Waiting time – the total amount of time a process spends in the ready queue;
In the "First come first serve" scheduling algorithm, as the name suggests, the process which arrives first,
gets executed first, or we can say that the process which requests the CPU first, gets the CPU allocated
first.
First Come First Serve, is just like FIFO (First in First out) Queue data structure, where the data element
which is added to the queue first, is the one who leaves the queue first.
A perfect real life example of FCFS scheduling is buying tickets at ticket counter.
Example 1:
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same order,
with Arrival Time 0, and given Burst Time, let's find the average waiting time using the FCFS scheduling
algorithm.
Gantt Chart:
P1 = 21 – 21 = 0ms
P2 = 24 – 3 = 21ms
P3 = 30 – 6 = 24ms
P4 = 32 – 2 = 30ms
• For the above given processes, first P1 will be provided with the CPU resources,
• Hence, waiting time for P1 will be 0
• P1 requires 21ms for completion, hence waiting time for P2 will be 21ms
• Similarly, waiting time for process P3 will be execution time of P1 + execution time for P2, which will
be (21 + 3)ms = 24ms.
• For process P4 it will be the sum of execution times of P1, P2 and P3.
• The GANTT chart above perfectly represents the waiting time for each process.
• Here we have simple formula for calculating various times for given processes:
• Completion Time: Time taken for the execution to complete, starting from arrival time.
• Turn Around Time: Time taken to complete after arrival. In simple words, it is the difference
between the Completion time and the Arrival time.
• Waiting Time: Total time the process has to wait before it's execution begins. It is the difference
between the Turn Around time and the Burst time of the process.
Example 2:
Gantt Chart:
P1 = 8 – 0 = 8ms
P2 = 12 – 3 = 9ms
P3 = 17 – 4 = 13ms
P4 = 20 – 6 = 14ms
P5 = 22 – 10 = 12ms
P1 = 8 – 8 = 0ms
P2 = 9 – 4 = 5ms
P3 = 13 – 5 = 8ms
P4 = 14 – 3 = 11ms
P5 = 12 – 2 = 10ms
Example 1:
Consider the below processes available in the ready queue for execution, with arrival time as 0 for all
and given burst times.
Gantt Chart:
P1 = 32 – 0 = 32ms
P2 = 5 – 0 = 5ms
P3 = 11 – 0 = 11ms
P4 = 2 – 0 = 2ms
P1 = 32 – 21 = 11ms
P2 = 5 – 3 = 2ms
P3 = 11 – 6 = 5ms
P4 = 2 – 2 = 0ms
Example 2:
Gantt Chart
P1 P4 P5 P2 P3
0 8 11 13 17 22
P1 = 8 – 0 = 8ms
P2 = 17 – 3 = 14ms
P3 = 22 – 4 = 18ms
P4 = 11 – 6 = 5ms
P5 = 13 – 10 = 3ms
P1 = 8 – 8 = 0ms
P2 = 14 – 4 = 10ms
P3 = 18 – 5 = 13ms
P4 = 5 – 3 = 2ms
P5 = 3 – 2 = 1ms
Example 1
Gantt Chart
P1 P2 P4 P5 P1 P3
0 3 7 10 12 17 22
P1 = 17 – 0 = 17ms
P2 = 7 – 3 = 4ms
P3 = 22 – 4 = 18ms
P4 = 10 – 6 = 4ms
P5 = 12 – 10 = 2ms
P1 = 17 – 8 = 9ms
P2 = 4 – 4 = 0ms
P3 = 18 – 5 = 13ms
P4 = 4 – 3 = 1ms
P5 = 2 – 2 = 0ms
P1 P3 P4 P5 P2
0 8 13 16 18 22
P1 = 8 – 0 = 8ms
P2 = 22 – 3 = 19ms
P3 = 13 – 4 = 9ms
P4 = 16 – 6 = 10ms
P5 = 18 – 10 = 8ms
P1 = 8 – 8 = 0ms
P2 = 19 – 4 = 15ms
P3 = 9 – 5 = 4ms
P4 = 10 – 3 = 7ms
P5 = 8– 2 = 6ms
P1 P2 P3 P4 P5 P2 P1
0 3 4 9 12 14 17 22
P1 = 22 – 0 = 22ms
P2 = 17 – 3 = 14ms
P3 = 9 – 4 = 5ms
P4 = 12 – 6 = 6ms
P5 = 14 – 10 = 4ms
P1 = 22 – 8 = 14ms
P2 = 14 – 4 = 10ms
P3 = 5 – 5 = 0ms
P4 = 6 – 3 = 3ms
P5 = 4 – 2 = 2ms
Gantt chart
P1 P2 P1 P3 P4 P2 P1 P5 P3
0 3 6 9 12 15 16 18 20 22
P1 = 18 – 8 = 10ms
P2 = 13 – 4 = 9ms
P3 = 18 – 5 = 13ms
P4 = 9 – 3 = 6ms
P5 = 10– 2 = 8ms