0% found this document useful (0 votes)
2 views

Week 6

Uploaded by

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

Week 6

Uploaded by

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

Week 6

Operating System Concepts

Muhammad Daniyal Liaquat

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Topics

Scheduling Algorithms

Operating System Concepts – 9th Edition 3.2 Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithms
• CPU scheduling deals with the problem of deciding which of the processes
in the ready queue is to be allocated the CPU. There are many different
CPU scheduling algorithms.

First-Come, First-Served
Scheduling
• The simplest CPU-scheduling algorithm is the first-come, first-served (FCFS)
scheduling algorithm. With this scheme, the process that requests the CPU first
is allocated the CPU first. The implementation of the FCFS policy is easily
managed with a FIFO queue.
• When the CPU is free, it is allocated to the process
• The running process is then removed from the queue.
• The code for FCFS scheduling is simple to write and understand.
• The average waiting time under the FCFS policy, however, is often quite long.

Operating System Concepts – 9th Edition 3.3 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

In FCFS Scheduling
• The process which arrives first in the ready queue is firstly
assigned the CPU.
• In case of a tie, process with smaller process id is executed
first.
• It is always non-preemptive algorithm in nature.
• Jobs are executed on first come, first serve basis.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• Poor in performance as average wait time is high.

Operating System Concepts – 9th Edition 3.4 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3
0 24 27 30

Waiting time for P1 = 0; P2 = 24; P3 = 27


Average waiting time: (0 + 24 + 27)/3 = 17
• The waiting time is 0 milliseconds for process P1, 24 milliseconds for
process P2, and 27 milliseconds for process P3.

Operating System Concepts – 9th Edition 3.5 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

• Average waiting time under an FCFS policy is generally


not minimal (minimum) and may vary substantially
(largely) if the process's CPU burst times vary greatly.
• The FCFS scheduling algorithm is nonpreemptive.
• Once the CPU has been allocated to a process, that
process keeps the CPU until it releases the CPU, either
by terminating or by requesting I/O.
• The FCFS algorithm is thus particularly troublesome for
time-sharing systems, where it is important that each
user get a share of the CPU at regular intervals. It would
be disastrous to allow one process to keep the CPU for
an extended period.

Operating System Concepts – 9th Edition 3.6 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

Advantages-
• It is simple and easy to understand.
• It can be easily implemented using queue data structure.
• It does not lead to starvation.

Disadvantages-
• It does not consider the priority or burst time of the
processes.
• It suffers from convoy effect i.e. processes with higher
burst time arrived before the processes with smaller
burst time.

Operating System Concepts – 9th Edition 3.7 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

Operating System Concepts – 9th Edition 3.8 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

Operating System Concepts – 9th Edition 3.9 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

Operating System Concepts – 9th Edition 3.10 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

Operating System Concepts – 9th Edition 3.11 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling

• A different approach to CPU scheduling is the


shortest-job-first (SJF) scheduling algorithm.
• When the CPU is available, it is assigned to the
process that has the smallest next CPU burst. If the
next CPU bursts of two processes are the same,
FCFS scheduling is used to break the tie.
• A more appropriate term for this scheduling
method would be the shortest-next-CPU-burst
algorithm because scheduling depends on the
length of the next CPU burst of a process

Operating System Concepts – 9th Edition 3.12 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling

• Process which have the shortest burst time are scheduled first.
• If two processes have the same bust time, then FCFS is used to break
the tie.
• This is a non-pre-emptive, pre-emptive scheduling algorithm.
• Best approach to minimize waiting time.
• Easy to implement in Batch systems where required CPU time is
known in advance.
• Impossible to implement in interactive systems where required CPU
time is not known.
• The processer should know in advance how much time process will
take.
• Pre-emptive mode of Shortest Job First is called as Shortest
Remaining Time First (SRTF).

Operating System Concepts – 9th Edition 3.13 Silberschatz, Galvin and Gagne ©2013
Example of SJF

ProcessArrival Time Burst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

Operating System Concepts – 9th Edition 3.14 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling

Advantages-
• SRTF is optimal and guarantees the minimum average waiting
time.
• It provides a standard for other algorithms since no other
algorithm performs better than it.
Disadvantages-
• It can not be implemented practically since burst time of the
processes can not be known in advance.
• It leads to starvation for processes with larger burst time.
• Priorities can not be set for the processes.
• Processes with larger burst time have poor response time.

Operating System Concepts – 9th Edition 3.15 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling

Operating System Concepts – 9th Edition 3.16 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling

Operating System Concepts – 9th Edition 3.17 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling
• A priority is associated with each process, and the CPU is allocated
to the process with the highest priority. Equal-priority processes are
scheduled in FCFS order.
• Priorities are generally indicated by some fixed range of numbers,
such as 0 to 7 or 0 to 4,095.
• However, there is no general agreement on whether 0 is the highest
or lowest priority.
• Some systems use low numbers to represent low priority; others use
low numbers for high priority. This difference can lead to confusion.
• As an example, consider the following set of processes,
assumed to have arrived at time 0, in the order P1, P2, ….., P5, with
the length of the CPU burst given in milliseconds:

Operating System Concepts – 9th Edition 3.18 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling

• Priority scheduling can be either preemptive or non-preemptive. When a process


arrives at the ready queue, its priority is compared with the priority of the currently
running process.
• A preemptive priority scheduling algorithm will preempt the CPU if the priority of the
newly arrived process is higher than the priority of the currently running process.
• A non-preemptive priority scheduling algorithm will simply put the new process at the
head of the ready queue.

Operating System Concepts – 9th Edition 3.19 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling

Operating System Concepts – 9th Edition 3.20 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling

Operating System Concepts – 9th Edition 3.21 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling

Operating System Concepts – 9th Edition 3.22 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling

Operating System Concepts – 9th Edition 3.23 Silberschatz, Galvin and Gagne ©2013
Round-Robin Scheduling
• The round-robin (RR) scheduling algorithm is designed especially for time- sharing
systems. It is similar to FCFS scheduling, but preemption is added to switch
between processes. A small unit of time, called a time quantum or time slice, is
defined. A time quantum is generally from 10 to 100 milliseconds. The ready queue
is treated as a circular queue. The CPU scheduler goes around the ready queue,
allocating the CPU to each process for a time interval of up to 1 time quantum.
• To implement RR scheduling, we keep the ready queue as a FIFO queue of
processes. New processes are added to the tail of the ready queue. The CPU
scheduler picks the first process from the ready queue, sets a timer to interrupt after
1 time quantum, and dispatches the process. One of two things will then happen.
The process may have a CPU burst of less than 1 time quantum. In this case, the
process itself will release the CPU voluntarily.
• The scheduler will then proceed to the next process in the ready queue. Otherwise,
if the CPU burst of the currently running process is longer than 1 time quantum, the
timer will go off and will cause an interrupt to the operating system and the process
will be put at the tail of the ready queue.
• The CPU scheduler will then select the next process in the ready queue.

Operating System Concepts – 9th Edition 3.24 Silberschatz, Galvin and Gagne ©2013
Round-Robin Scheduling
• The average waiting time under the RR policy is often long. Consider
the following set of processes that arrive at time 0, with the length of
the CPU burst given in milliseconds:

• In the RR scheduling algorithm, no process is allocated the CPU for more than 1 time
quantum
• If a process's CPU burst exceeds 1 time quantum, that process is preempted and is put back
in the ready queue. The RR scheduling algorithm is thus preemptive.

Operating System Concepts – 9th Edition 3.25 Silberschatz, Galvin and Gagne ©2013
Round-Robin Scheduling

Operating System Concepts – 9th Edition 3.26 Silberschatz, Galvin and Gagne ©2013
Round-Robin Scheduling
Advantages-
• It gives the best performance in terms of average
response time.
• It is best suited for time sharing system, client server
architecture and interactive system.

Disadvantages-
• It leads to starvation for processes with larger burst time
as they have to repeat the cycle many times.
• Its performance heavily depends on time quantum.
• Priorities can not be set for the processes.

Operating System Concepts – 9th Edition 3.27 Silberschatz, Galvin and Gagne ©2013
Round-Robin Scheduling

Operating System Concepts – 9th Edition 3.28 Silberschatz, Galvin and Gagne ©2013
Round-Robin Scheduling

Operating System Concepts – 9th Edition 3.29 Silberschatz, Galvin and Gagne ©2013
Round-Robin Scheduling

Operating System Concepts – 9th Edition 3.30 Silberschatz, Galvin and Gagne ©2013
Round-Robin Scheduling

Operating System Concepts – 9th Edition 3.31 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue Scheduling
• Another class of scheduling algorithms has been created for situations in which
processes are easily classified into different groups.
• For example, a common division is made between foreground processes and
background processes.
• These two types of processes have different response time requirements and
so might have different scheduling needs.
• In addition, foreground processes may have priority over background
processes.
• A multilevel queue-scheduling algorithm partitions the ready queue into several
separate queues.
• Each queue has its own priority and scheduling algorithm.
• Processes are permanently assigned to one queue, generally based on some
property of the process, such as memory size, process priority or process type.
• In addition, there must be scheduling among the queues i.e. serve all from
foreground then from background.
• Another possibility is to add time slice between queues.
• Each queue gets a certain portion of the CPU time, which it can then schedule
among the various processes in its queue, e.g., 80% to foreground in RR and
20% to background in FCFS.

Operating System Concepts – 9th Edition 3.32 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue Scheduling
• A multi-level queue scheduling algorithm partitions the ready queue into several
separate queues. The processes are permanently assigned to one queue,
generally based on some property of the process, such as memory size,
process priority, or process type. Each queue has its own scheduling algorithm.

• Let us consider an example of a multilevel queue-scheduling algorithm with five


queues:

1. System Processes.
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes

Each queue has absolute priority over lower-priority queues. No process in the
batch queue, for example, could run unless the queues for system processes,
interactive processes, and interactive editing processes were all empty. If an
interactive editing process entered the ready queue while a batch process was
running, the batch process will be pre-empted

Operating System Concepts – 9th Edition 3.33 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue Scheduling

Operating System Concepts – 9th Edition 3.34 Silberschatz, Galvin and Gagne ©2013
Multilevel Feedback Queue Scheduling
• Multilevel feedback queue scheduling allows a process to move between
queues.
• The idea is to separate processes with different CPU burst characteristics.
• If a process uses too much CPU time, it will be moved to a lower-priority
queue.
• This scheme leaves I/O bound and interactive processes in the higher-priority
queues.
• Similarly a process that waits too long in a lower-priority queue may be moved
to a higher priority queue.
• In general, a multi-level feedback queue scheduler is defined by the following
parameters:
– Number of queues
– Scheduling algorithm for each queue
– Method used to determine when to upgrade a process to higher priority
queue
– Method used to determine when to demote a process
– Method used to determine which queue a process enters when it needs
service

Operating System Concepts – 9th Edition 3.35 Silberschatz, Galvin and Gagne ©2013
Multilevel Feedback Queue Scheduling
• Figure shows an example of multilevel feedback queue scheduling
system with the ready queue partitioned into three queues.
• In this system, processes with next CPU bursts less than or equal to
8 time units are processed with the shortest possible wait times,
followed by processes with CPU bursts greater than 8 but no greater
than 16 time units. Processes with CPU greater than 16 time units
wait for the longest time.

Operating System Concepts – 9th Edition 3.36 Silberschatz, Galvin and Gagne ©2013
Any Questions ?

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013

You might also like