0% found this document useful (0 votes)
14 views29 pages

Lecture 3 CPU Scheduling

Chapter 6 of 'Operating System Concepts' discusses CPU scheduling, essential for multiprogrammed operating systems, detailing various algorithms like First-Come, First-Served (FCFS), Shortest-Job-First (SJF), Priority Scheduling, and Round Robin (RR). It explains the importance of maximizing CPU utilization and minimizing waiting times, as well as the role of the CPU scheduler and dispatcher in managing process execution. The chapter also highlights the trade-offs and challenges associated with different scheduling strategies.

Uploaded by

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

Lecture 3 CPU Scheduling

Chapter 6 of 'Operating System Concepts' discusses CPU scheduling, essential for multiprogrammed operating systems, detailing various algorithms like First-Come, First-Served (FCFS), Shortest-Job-First (SJF), Priority Scheduling, and Round Robin (RR). It explains the importance of maximizing CPU utilization and minimizing waiting times, as well as the role of the CPU scheduler and dispatcher in managing process execution. The chapter also highlights the trade-offs and challenges associated with different scheduling strategies.

Uploaded by

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

Operating System Concepts

Chapter 6: CPU Scheduling

Operating System Concepts 9th Edition


Objectives

 To introduce CPU scheduling, which is the basis for multiprogrammed


operating systems
 To describe various CPU-scheduling algorithms.

Operating System Concepts 9th Edition


Basic Concept
 The objective of multiprogramming is to have some process running at all
times, to maximize CPU utilization. With multiprogramming, Several processes
are kept in memory at one time. When one process has to wait (typically for the
completion of some I/O request.), the operating system takes the CPU away
from that process and gives the CPU to another process. This pattern
continues. Every time one process has to wait, another process can take over
use of the CPU.

 Scheduling of this kind is a fundamental operating-system function. Almost


all computer resources are scheduled before use. The CPU is, of course, one of
the primary computer resources. Thus, its scheduling is central to operating-
system design.

Operating System Concepts 9th Edition


Alternating Sequence of CPU And I/O Bursts
 The success of CPU scheduling depends
on an observed property of processes:
process execution consists of a cycle of
CPU execution and I/O wait.

 Processes alternate between these two


states. Process execution begins with a
CPU burst. That is followed by an I/O burst,
which is followed by another CPU burst,
then another I/O burst, and so on.

 Eventually, the final CPU burst ends with a


system request to terminate execution

Operating System Concepts 9th Edition


CPU Scheduler
 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.
 CPU scheduler selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them.
 CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates

Operating System Concepts 9th Edition


CPU Scheduler
 For situations 1 and 4, there is no choice in terms of scheduling. A new
process (if one exists in the ready queue) must be selected for execution.
There is a choice, however, for situations 2 and 3.
 When scheduling takes place only under circumstances 1 and 4, we say that
the scheduling scheme is nonpreemptive or cooperative. Otherwise, it is
preemptive.
 Under nonpreemptive 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 was
used by Microsoft Windows 3.x.
 Windows 95 introduced preemptive scheduling, and all subsequent versions
of Windows operating systems have used preemptive scheduling.
 The Mac OS X operating system for the Macintosh also uses preemptive
scheduling; previous versions of the Macintosh operating system
relied on cooperative scheduling.

Operating System Concepts 9th Edition


Despatcher
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 the following:

• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that
program.

The dispatcher should be as fast as possible, since it is invoked during every


process switch. The time it takes for the dispatcher to stop one process and start
another running is known as the dispatch latency.

Operating System Concepts 9th Edition


Scheduling Criteria
Different CPU-scheduling algorithms have different properties, and the
choice of a particular algorithm may favour one class of processes over
another.

Many criteria have been suggested for comparing CPU-scheduling


algorithms. Which characteristics are used for comparison can make a
substantial difference in which algorithm is judged to be best.

 CPU utilization – We want to keep the CPU as busy as possible.


Conceptually, CPU utilization can range from 0 to 100 percent. In a real
system, it should range from 40 percent to 90 percent
 Throughput – is the number of processes that are completed per time
unit. For long processes, this rate may be one process per hour; for
short transactions, it may be ten processes per second.

Operating System Concepts 9th Edition


Scheduling Criteria
 Turnaround time – The interval from the time of submission of a process
to the time of completion is the turnaround time. Turnaround time is
the sum of the periods spent waiting to get into memory, waiting in the
ready queue, executing on the CPU, and doing I/O.
 Waiting time – amount of time a process has been waiting in the ready queue.
 Response time – is the time from the submission of a request until the
first response is produced. This measure, called response time, is the
time it takes to start responding, not the time it takes to output the
response.

It is desirable to maximize CPU utilization and throughput and to minimize


turnaround time, waiting time, and response time.

Operating System Concepts 9th Edition


First-Come, First-Served (FCFS) Scheduling
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. In this section, we describe several of them.

First-Come, First-Served Scheduling:

By far 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 a process enters the ready queue, its PCB is linked onto the tail of
the queue. When the CPU is free, it is allocated to the process at the
head of the queue. The running process is then removed from the queue.
The code for FCFS scheduling is simple to write and understand.

Operating System Concepts 9th Edition


First-Come, First-Served (FCFS) Scheduling
On the negative side, the average waiting time under the FCFS policy is
often quite long. Consider the following set of processes that arrive at
time 0, with the length of the CPU burst given in milliseconds:
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

Operating System Concepts 9th Edition


First-Come, First-Served (FCFS) Scheduling
Suppose that the processes arrive in the order
P2 , P3 , P1
The Gantt chart for the schedule is:

P2 P3 P1

0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 The average waiting time under an FCFS policy is generally not minimal
and may vary substantially if the processes’ CPU burst times vary greatly.

Operating System Concepts 9th Edition


First-Come, First-Served (FCFS) Scheduling

Operating System Concepts 9th Edition


First-Come, First-Served (FCFS) Scheduling

Operating System Concepts 9th Edition


Shortest-Job-First (SJF) Scheduling
This algorithm associates with each process the length of the process’s next
CPU burst. 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.
Process Arrival Time Burst Time
P1 0.0
6
P2 2.0
8
P3 4.0
7
P4 P4 P1 P3 5.0
P2
3
 SJF0 scheduling3chart 9 16 24

Operating System Concepts 9th Edition

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


Shortest-Job-First (SJF) Scheduling
The SJF scheduling algorithm is provably optimal, in that it gives the minimum
average waiting time for a given set of processes. Moving a short process
before a long one decreases the waiting time of the short process more than it
increases the waiting time of the long process. Consequently, the average waiting
time decreases.

The real difficulty with the SJF algorithm is knowing the length of the next CPU
request. For long-term (job) scheduling in a batch system, we can use the
process time limit that a user specifies when he submits the job. SJF scheduling
is used frequently in long-term scheduling.

Although the SJF algorithm is optimal, it cannot be implemented at the level of


short-term CPU scheduling. With short-term scheduling, there is no way to know
the length of the next CPU burst.

Operating System Concepts 9th Edition


Shortest-Job-First (SJF) Scheduling

Operating System Concepts 9th Edition


Priority Scheduling
The SJF algorithm is a special case of the general priority-scheduling algorithm.
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.

An SJF algorithm is simply a priority algorithm where the priority (p) is the
inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower the
priority, and vice versa.

Note that we discuss scheduling in terms of high priority and low priority. Priorities
are generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to
4,095. We assume that low numbers represent high priority.

Operating System Concepts 9th Edition


Priority Scheduling
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:

Process Burst Time Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

Using priority scheduling, we would schedule these processes according to


the following Gantt chart:

The average waiting time is (0 + 1 + 6 + 16 + 18 ) / 5 = 8.2 milliseconds.

Operating System Concepts 9th Edition


Priority Scheduling
Priority scheduling can be either preemptive or nonpreemptive. 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 nonpreemptive priority scheduling algorithm will simply put the new process at
the head of the ready queue.

A major problem with priority scheduling algorithms is indefinite blocking, or


starvation. A process that is ready to run but waiting for the CPU can be
considered blocked. A priority scheduling algorithm can leave some low priority
processes waiting indefinitely. In a heavily loaded computer system, a steady
stream of higher-priority processes can prevent a low-priority process from ever
getting the CPU.

Operating System Concepts 9th Edition


Priority Scheduling
Generally, one of two things will happen. Either the process will eventually be run
(at 2 A.M. Sunday, when the system is finally lightly loaded), or the computer
system will eventually crash and lose all unfinished low-priority processes.

A solution to the problem of indefinite blockage of low-priority processes is


Aging. Aging involves gradually increasing the priority of processes that
wait in the system for a long time.

For example, if priorities range from 127 (low) to 0 (high), we could


increase the priority of a waiting process by 1 every 15 minutes. Eventually,
even a process with an initial priority of 127 would have the highest priority
in the system and would be executed. In fact, it would take no more than
32 hours for a priority-127 process to age to a priority-0 process.

Operating System Concepts 9th Edition


Priority Scheduling

Operating System Concepts 9th Edition


Priority Scheduling

Operating System Concepts 9th Edition


Priority Scheduling

Operating System Concepts 9th Edition


Round Robin (RR)
The round-robin (RR) scheduling algorithm is designed especially for timesharing
systems. It is similar to FCFS scheduling, but preemption is added to enable the
system 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 in length.

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 again treat 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.

Operating System Concepts 9th Edition


Round Robin (RR)
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. 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.

A context switch will be executed, 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


Round Robin (RR)
Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:

Process Burst Time


P1 24
P2 3
P3 3
If we use a time quantum of 4 milliseconds, then process P1 gets the first 4
milliseconds. Since it requires another 20 milliseconds, it is preempted after the
first time quantum, and the CPU is given to the next process in the queue, process
P2.

Process P2 does not need 4 milliseconds, so it quits before its time quantum
expires. The CPU is then given to the next process, process P3. Once each
process has received 1 time quantum, the CPU is returned to process P1 for an
additional time quantum.

Operating System Concepts 9th Edition


Round Robin (RR)
The resulting RR schedule is as follows:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

Let’s calculate the average waiting time for this schedule. P1 waits for 6
milliseconds (10 - 4), P2 waits for 4 milliseconds, and P3 waits for 7 milliseconds.
Thus, the average waiting time is 17/3 = 5.66 milliseconds.

The performance of the RR algorithm depends heavily on the size of the time
quantum. At one extreme, if the time quantum is extremely large, the RR policy is
the same as the FCFS policy. In contrast, if the time quantum is extremely small
(say, 1 millisecond), the RR approach can result in a large number of context
switches.

Operating System Concepts 9th Edition


Round Robin (RR)
Assume, for example, that we have only one process of 10 time units. If the
quantum is 12 time units, the process finishes in less than 1 time quantum, with no
overhead. If the quantum is 6 time units, however, the process requires 2 quanta,
resulting in a context switch. If the time quantum is 1 time unit, then nine context
switches will occur, slowing the execution of the process accordingly.

Thus, we want the time quantum to be large with respect to the context
switch time. In practice, most modern systems have time quanta ranging
from 10 to 100 milliseconds. The time required for a context switch is
typically less than 10 microseconds.

Operating System Concepts 9th Edition

You might also like