Operating System
Operating System
OPERATING SYSTEMS
UNIT II - CPU SCHEDULING
Objectives:
Describe various CPU scheduling algorithms
Assess CPU scheduling algorithms based on scheduling criteria
Explain the issues related to multiprocessor and multi core scheduling
Describe various real-time scheduling algorithms
Describe the scheduling algorithms used in the Windows, Linux, and Solaris operating systems
Apply modeling and simulations to evaluate CPU scheduling algorithms
Outcomes :
Understands the use of different process scheduling algorithm
Understand the role of the scheduler, and how its behaviour influences the performance of the
system.
Know the difference between I/O-bound and CPU-bound tasks, and how they relate to
scheduling.
Understand typical interactive and real time scheduling approaches.
Prerequisites:
Knowledge about various tasks performed by Operating System.
Knowledge about process, threads, multiprocessing and basic scheduling techniques.
CPU Scheduling is a process of determining which process will own CPU for execution while
another process is on hold.
The main task of CPU scheduling is to make sure that whenever the CPU remains idle, the OS at
least select one of the processes available in the ready queue for execution.
The selection process will be carried out by the CPU scheduler. It selects one of the processes in
memory that are ready for execution.
1
Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is important to
run a task with a higher priority before another lower priority task, even if the lower priority task is still
running. The lower priority task holds for some time and resumes when the higher priority task finishes
its execution.
Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a specific process. The process that
keeps the CPU busy will release the CPU either by switching context or terminating. It is the only method
that can be used for various hardware platforms. That's because it doesn't need special hardware (for
example, a timer) like preemptive scheduling.
Burst Time/Execution Time: It is a time required by the process to complete execution. It is also
called running time.
Arrival Time: when a process enters in a ready state
Finish Time: when process complete and exit from a system
Multiprogramming: A number of programs which can be present in memory at the same time.
Jobs: It is a type of program without any kind of user interaction.
User: It is a kind of program having user interaction.
Process: It is the reference that is used for both job and user.
CPU/IO burst cycle: Characterizes process execution, which alternates between CPU and I/O
activity. CPU times are usually shorter than the time of I/O.
2
What is Dispatcher?
It is a module that provides control of the CPU to the process. The Dispatcher should be fast so that it can
run on every context switch. Dispatch latency is the amount of time needed by the CPU scheduler to stop
one process and start another.
Context Switching
Switching to user mode
Moving to the correct location in the newly loaded program.
First Come First Serve is the full form of FCFS. It is the easiest and most simple CPU scheduling
algorithm. In this type of algorithm, the process which requests the CPU gets the CPU allocation
first.
This scheduling method can be managed with a FIFO queue. As the process enters the ready
queue, its PCB (Process Control Block) is linked with the tail of the queue. So, when CPU
becomes free, it should be assigned to the process at the beginning of the queue.
3
However, this method is poor in performance, and the general wait time is quite high.
Convoy Effect in FCFS
FCFS may suffer from the convoy effect if the burst time of the first job is the highest among all. As in
the real life, if a convoy is passing through the road then the other persons may get blocked until it passes
completely. This can be simulated in the Operating System also.
If the CPU gets the processes of the higher burst time at the front end of the ready queue then the
processes of lower burst time may get blocked which means they may never get the CPU if the job in the
execution has a very high burst time. This is called convoy effect or starvation.
The full form of SRT is Shortest remaining time. It is also known as SJF preemptive scheduling. In this
method, the process will be allocated to the task, which is closest to its completion. This method prevents
a newer ready state process from holding the completion of an older process.
This method is mostly applied in batch environments where short jobs are required to be given
preference.
This is not an ideal method to implement it in a shared system where the required CPU time is
unknown.
Associate with each process as the length of its next CPU burst. So that operating system uses
these lengths, which helps to schedule the process with the shortest possible time.
Priority scheduling is a method of scheduling processes based on priority. In this method, the
scheduler selects the tasks to work as per the priority.
Priority scheduling also helps OS to involve priority assignments. The processes with higher
priority should be carried out first, whereas jobs with equal priorities are carried out on a round-
robin or FCFS basis. Priority can be decided based on memory requirements, time requirements,
etc.
4. Round-Robin Scheduling
4
Round robin is the oldest, simplest scheduling algorithm. The name of this algorithm comes from the
round-robin principle, where each person gets an equal share of something in turn. It is mostly used for
scheduling algorithms in multitasking. This algorithm method helps for starvation free execution of
processes.
SJF is a full form of (Shortest job first) is a scheduling algorithm in which the process with the shortest
execution time should be selected for execution next. This scheduling method can be preemptive or non-
preemptive. It significantly reduces the average waiting time for other processes awaiting execution.
This algorithm separates the ready queue into various separate queues. In this method, processes are
assigned to a queue based on a specific property of the process, like the process priority, size of the
memory, etc.
However, this is not an independent scheduling OS algorithm as it needs to use other types of algorithms
in order to schedule the jobs.
Example Demonstration
5
Round Robin Scheduling Example
In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6. Their arrival time
and burst time are given below in the table. The time quantum of the system is 4 units.
1 0 5
2 1 6
3 2 3
4 3 1
5 4 5
6 6 4
According to the algorithm, we have to maintain the ready queue and the Gantt chart. The structure of
both the data structures will be changed after every scheduling.
Ready Queue:
Initially, at time 0, process P1 arrives which will be scheduled for the time slice 4 units. Hence in the
ready queue, there will be only one process P1 at starting with CPU burst time 5 units.
P1
GANTT chart
Ready Queue
Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in the ready queue. P1 has
not completed yet, it needs another 1 unit of time hence it will also be added back to the ready queue.
6
P2 P3 P4 P5 P1
6 3 1 5 1
GANTT chart
After P1, P2 will be executed for 4 units of time which is shown in the Gantt chart.
Ready Queue
During the execution of P2, one more process P6 is arrived in the ready queue. Since P2 has not
completed yet hence, P2 will also be added back to the ready queue with the remaining burst time 2 units.
P3 P4 P5 P1 P6 P2
3 1 5 1 4 2
GANTT chart
After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time is only 3 seconds.
Ready Queue
Since P3 has been completed, hence it will be terminated and not be added to the ready queue. The next
process will be executed is P4.
P4 P5 P1 P6 P2
1 5 1 4 2
7
GANTT chart
After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is lesser then the time
quantum hence it will be completed.
Ready Queue
The next process in the ready queue is P5 with 5 units of burst time. Since P4 is completed hence it will
not be added back to the queue.
P5 P1 P6 P2
5 1 4 2
GANTT chart
P5 will be executed for the whole time slice because it requires 5 units of burst time which is higher than
the time slice.
Ready Queue
P5 has not been completed yet; it will be added back to the queue with the remaining burst time of 1 unit.
P1 P6 P2 P5
1 4 2 1
GANTT Chart
The process P1 will be given the next turn to complete its execution. Since it only requires 1 unit of burst
time hence it will be completed.
8
Ready Queue
P1 is completed and will not be added back to the ready queue. The next process P6 requires only 4 units
of burst time and it will be executed next.
P6 P2 P5
4 2 1
GANTT chart
Ready Queue
Since P6 is completed, hence it will not be added again to the queue. There are only two processes present
in the ready queue. The Next process P2 requires only 2 units of time.
P2 P5
2 1
GANTT Chart
P2 will get executed again, since it only requires only 2 units of time hence this will be completed.
9
Ready Queue
Now, the only available process in the queue is P5 which requires 1 unit of burst time. Since the time
slice is of 4 units hence it will be completed in the next burst.
P5
GANTT chart
The completion time, Turnaround time and waiting time will be calculated as shown in the table below.
As, we know,
1. Turn Around Time = Completion Time - Arrival Time
2. Waiting Time = Turn Around Time - Burst Time
Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
1 0 5 17 17 12
2 1 6 23 22 16
3 2 3 11 9 6
4 3 1 12 9 8
5 4 5 24 20 15
10
6 6 4 21 15 11
Conclusion:
Scheduling algorithms should not affect the behavior of the system (same results regardless of schedule).
However, the algorithms do impact the system's efficiency and response time.
The best schemes are adaptive. To do absolutely best, we'd have to be able to predict the future.
Points in nutshell:
In Preemptive Scheduling, the tasks are mostly assigned with their priorities.
In the Non-preemptive scheduling method, the CPU has been allocated to a specific process.
Burst time is a time required for the process to complete execution. It is also called running time.
The number of processes that finish their execution per unit time is known Throughput.
Waiting time is an amount that specific process needs to wait in the ready queue.It is an amount
to time in which the request was submitted until the first response is produced.
In the First Come First Serve method, the process which requests the CPU gets the CPU
allocation first.
In the Shortest Remaining time, the process will be allocated to the task, which is closest to its
completion.
In, Priority Scheduling the scheduler selects the tasks to work as per the priority.
11
Round robin scheduling works on principle, where each person gets an equal share of
something in turn
In Shortest job first the shortest execution time should be selected for execution next
In Multilevel scheduling, method separates the ready queue into various separate queues. In this
method, processes are assigned to a queue based on a specific property
2. The interval from the time of submission of a process to the time of completion is termed as
____________
a) waiting time
b) turnaround time
c) response time
d) throughput
Answer: b
3. Which scheduling algorithm allocates the CPU first to the process that requests the CPU first?
a) first-come, first-served scheduling
b) shortest job scheduling
c) priority scheduling
d) none of the mentioned
Answer: a
4. In priority scheduling algorithm, when a process arrives at the ready queue, its priority is
compared with the priority of ____________
a) all process
b) currently running process
c) parent process
d) init process
Answer: b
12
b) round robin scheduling algorithm
c) priority scheduling algorithm
d) multilevel queue scheduling algorithm
Answer: b
7. There are 10 different processes running on a workstation. Idle processes are waiting for an
input event in the input queue. Busy processes are scheduled with the Round-Robin time
sharing method. Which out of the following quantum times is the best value for small
response times, if the processes have a short runtime, e.g. less than 10ms?
a) tQ = 15ms
b) tQ = 40ms
c) tQ = 45ms
d) tQ = 50ms
Answer: a
8. Which of the following algorithms tends to minimize the process flow time?
a) First come First served
b) Shortest Job First
c) Earliest Deadline First
d) Longest Job First
Answer: b
a) I only
b) I and III only
c) II and III only
13
d) I, II and III
Answer: d
Final Conclusion :
For fixed priority scheduling algorithms, due to its minimum average waiting time and average
turnaround time, SJF scheduling algorithm serves all types of job with optimum scheduling criteria.
But long process will never been served.
To improve time management performance, we will deal with the advantages of the hardware
like parallel and dependent execution. An operating system where hardware and software model
coexist lead to raising several topics as communication overhead and resource optimization,
portability, suitable interface and mechanisms of synchronization.
14