Ch6 CPU Scheduling
Ch6 CPU Scheduling
CPU scheduling is a process that 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.
7. Non-Preemptive Scheduling
8. 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.
9. This scheduling method is used by the Microsoft Windows 3.1 and by the Apple
Macintosh operating systems.
10. 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.
11. In non-preemptive scheduling, it does not interrupt a process running CPU in the middle
of the execution. Instead, it waits till the process completes its CPU burst time, and then
after that it can allocate the CPU to any other process.
12. Some Algorithms based on non-preemptive scheduling are: Shortest Job First (SJF
basically non-preemptive) Scheduling and Priority (non- preemptive version) Scheduling,
etc.
Some Algorithms based on non-preemptive scheduling are: Shortest Job First (SJF basically non-
preemptive) Scheduling etc.
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.
Thus this type of scheduling is used mainly when a process switches either from running state to
ready state or from waiting state to ready state. The resources (that is CPU cycles) are mainly
allocated to the process for a limited amount of time and then are taken away, and after that, the
process is again placed back in the ready queue in the case if that process still has a CPU burst
time remaining. That process stays in the ready queue until it gets the next chance to execute.
Some Algorithms that are based on preemptive scheduling are Round Robin Scheduling (RR),
Shortest Remaining Time First (SRTF).etc.
CPU Scheduling: Scheduling Criteria
There are many different criteria to check when considering the "best" scheduling algorithm,
they are:
CPU Utilization
To make out the best use of the CPU and not to waste any CPU cycle, the CPU would be
working most of the time(Ideally 100% of the time). Considering a real system, CPU usage
should range from 40% (lightly loaded) to 90% (heavily loaded.)
Throughput
It is the total number of processes completed per unit of time or rather says the total amount of
work done in a unit of time. This may range from 10/second to 1/hour depending on the specific
processes.
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from the time of
submission of the process to the time of completion of the process(Wall clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process has been
waiting in the ready queue to acquire get control on the CPU.
Load Average
It is the average number of processes residing in the ready queue waiting for their turn to get into
the CPU.
Response Time
Amount of time it takes from when a request was submitted until the first response is produced.
Remember, it is the time till the first response and not the completion of process execution(final
response).
In general CPU utilization and Throughput are maximized and other factors are reduced for
proper optimization.
Scheduling Algorithms
To decide which process to execute first and which process to execute last to achieve maximum
CPU utilization, computer scientists have defined some algorithms, they are:
1. First Come First Serve(FCFS) Scheduling
2. Shortest-Job-First(SJF) Scheduling
3. Round Robin(RR) Scheduling
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.
This is used in Batch Systems.
It's easy to understand and implement programmatically, using a Queue data structure,
where a new process enters through the tail of the queue, and the scheduler selects
process from the head of the queue.
A perfect real life example of FCFS scheduling is buying tickets at ticket counter.
Calculating Average Waiting Time
For every scheduling algorithm, Average waiting time is a crucial parameter to judge it's
performance.
AWT or Average waiting time is the average of the waiting times of the processes in the queue,
waiting for the scheduler to pick them for execution.
Lower the Average Waiting Time, better the scheduling algorithm.
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.
The GANTT chart above perfectly represents the waiting time for each process.
1. It is Non Pre-emptive algorithm, which means the process priority doesn't matter.
If a process with very least priority is being executed, more like daily routine
backup process, which takes more time, and all of a sudden some other high priority
process arrives, like interrupt to avoid system crash, the high priority process will have
to wait, and hence in this case, the system will crash, just because of improper process
scheduling.
1. A
t
(
t
=
0
At ( t =0ms ), P1 arrives. It’s the only process so CPU starts executing it.
At ( t = 2ms ), P3 process has arrived. At this time, P1(remaining time) = 5ms, P2(remaining
time ) = 3 ms , P3 = 2ms. Since P3 is having least burst time, P3 is executed .
At ( t = 3ms ), P4 comes , At this time, P1 = 5ms, P2 = 3ms, P3 = 1ms, P4 = 3ms. Since P4 does
not have short burst time, so P3 continues to execute.
At ( t= 4ms ),P3 is finished . Now, remaining tasks are P1 = 5ms, P2 = 3ms, P4 = 3ms. As ,P2
and P4 have same time, so the task which came first will be executed first. So, P2 gets executed
first.
P1 = (15-0) = 15ms
P2 = (7-1) = 6ms
P3 = (4-2 )=2ms
P4 = (10-3)= 7ms
Important terms
1. Completion Time
It is the time at which any process completes its execution.
2. Turn Around Time
This mainly indicates the time Difference between completion time and arrival time. The
Formula to calculate the same is: Turn Around Time = Completion Time – Arrival
Time
3. Waiting Time(W.T):
It Indicates the time Difference between turn around time and burst time.
And is calculated as Waiting Time = Turn Around Time – Burst Time
Let us now cover an example for the same:
Here, we have taken quantum as 3ms. So,each process is assigned 3ms before switching to next
process.
P1 is executed for 3ms. The, P2 is executed. As, P2 is only 2ms long, So after 2 ms , next
process occupies the CPU.
P3 then is executed for 3ms. So, P3 remaining = 2ms. Then , P4 is executed for 3ms. P4
remaining time = 3ms
Now remaining process is P1 only . It executed for 3 ms. After that, Since P2,P3 and P4 are
already finished, P1 is executed again for remaining time (1ms).
P1 = 23-0 = 23ms
P2 = 5-0=5ms
P3 = 16-0 = 16ms
P4 = 19-0 = 19ms
P1 = 23-10 = 13ms
P2 =5-2= 3ms
P3 = 16-5 = 11ms
P4 = 19-6 = 13ms