Download 4.OS_CPU_Scheduling - M Umair`s Web Page

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Course: Operating Systems
Instructor: M Umair
M Umair – http://www.m-umair.com
CPU Scheduling
M Umair – http://www.m-umair.com
Introduction
 CPU scheduling is the basis of multi-programmed operating
systems.
By switching the CPU among processes, the operating system
can make the computer more productive.
In a single-processor system, only one process can run at a time;
any others must wait until the CPU is free and can be rescheduled.
The objective of multiprogramming is to have some process
running at all times, to maximize CPU utilization.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
Introduction
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
 The success of CPU scheduling depends on an observed
property of processes. CPU-scheduling decisions may take place
under the following four circumstances:
a) When a process switches from the running state to the
waiting state (for example, as the result of an I/O request ).
b) When a process switches from the running state to the
ready state (for example, when an interrupt occurs).
c) When a process switches from the waiting state to the
ready state (for example, at completion of I/O).
d) When a process{ Ref:terminates.
Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Non preemptive
 Scheduling algorithms can be divided into two categories with
respect to how they deal with clock interrupts.
A non preemptive scheduling algorithm picks a process to run
and then just lets it run until it blocks (either on I/O or waiting for
another process) or until it voluntarily releases the CPU.
Even if it runs for hours, it will not be forcibly suspended.
In effect, no scheduling decisions are made during clock
interrupts. After clock interrupt processing has been completed,
the process that was running before the interrupt is always
resumed.
{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Preemptive
In contrast, a preemptive scheduling algorithm picks a process
and lets it run for a maximum of some fixed time.
If it is still running at the end of the time interval, it is suspended
and the scheduler picks another process to run (if one is
available).
Doing preemptive scheduling requires having a clock interrupt
occur at the end of the time interval to give control of the CPU
back to the scheduler.
If no clock is available, non-preemptive scheduling is the only
option.
{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Preemptive
Preemptive scheduling incurs a cost associated with access to
shared data.
Consider the case of two processes that share data.
While one is updating the data, it is preempted so that the
second process can run.
The second process then tries to read the data, which are in an
inconsistent state.
In such situations, we need new mechanisms to coordinate
access to shared data.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
- Dispatcher
Component involved in the CPU-scheduling function is known as
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 time it takes for the dispatcher to stop one process and start
another running is known
as the dispatch latency.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Algorithm Comparison
Many criteria have been suggested for comparing CPU-scheduling
algorithms.
CPU utilization [keep the CPU as busy as possible]
Throughput [number of processes that are completed per time unit]
Turnaround time[The interval from the time of submission of a
process to the time of completion is the turnaround time.]
Waiting time [Waiting time is the sum of the periods spent waiting in
the ready queue.]
Response time[the time it takes to start responding, not the time it
takes to output the response]
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– FCFS Algorithm
Simplest CPU-scheduling algorithm is the first-come, first-served
(FCFS) scheduling algorithm.
The process that requests the CPU first is allocated the CPU first.
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 {isRef:then
removed from the queue.
Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– FCFS Algorithm
 The negative side, the average waiting time under an FCFS
policy is generally not minimal and may vary substantially.
Average Wait Time = (0+24+27)/3 = 17
Average Wait Time = (0+3+6)/3 = 3
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler – FCFS Algorithm
– Convoy effect
{ www.cse.ohio-state.edu/~agrawal/660/Slides/jan18.pdf }
M Umair – http://www.m-umair.com
CPU Scheduler
– Shortest Job First Algorithm
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.
Scheduling depends on the length of the next CPU burst of a
process, rather than its total length.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Shortest Job First Algorithm
SJF- Average Wait Time = (0+3+9+16)/4 = 7
FCFS- Average Wait Time = (0+6+14+21)/4 = 10.25
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Shortest Job First Algorithm
The real difficulty with the SJF algorithm is knowing the length
of the next CPU request.
With short-term scheduling, there is no way to know the length
of the next CPU burst.
One approach is to try to approximate SJF scheduling.
We may not know the length of the next CPU burst, but we may
be able to predict its value.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Shortest Job First Algorithm
 t is the last CPU burst value
 α is a constant indicating how much to base estimate on last CPU burst
 τn is the last estimate
Say, α = 0.5 , τ0 = 10, t = 6
What is estimate of next CPU burst?
τ1 = 0.5 * 6 + 0.5 * 10 = 8
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Shortest Job First Algorithm
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– SJF {Preemptive/Non Preemptive}
 A preemptive SJF algorithm will preempt the currently
executing process.
 A non preemptive SJF algorithm will allow the currently running
process to finish its CPU burst.
Preemptive SJF Example
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Priority Scheduling
A priority is associated with each process, and the CPU is
allocated to the process with the highest priority
At a university, the order may be deans first, then professors,
secretaries, coordinators, and finally students.
The need to take external factors into account leads to priority
scheduling .
The basic idea is straightforward: each process is assigned a
priority, and the run-able process with the highest priority is
allowed to run.
{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Priority Scheduling
Even on a PC with a single owner, there may be multiple
processes, some more important than others.
For example, a daemon process sending electronic mail in the
background should be assigned a lower priority than a process
displaying a video film on the screen in real time.
Priorities can be defined either internally or externally.
Internally defined priorities use some measurable quantity(ies)
to compute the priority of a process. For example, time limits,
memory requirements, the number of open files, and the ratio of
average I/O burst to average CPU burst.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin -Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Priority Scheduling
External priorities are set by criteria outside the operating
system, such as the importance of the process, the type and
amount of funds being paid for computer use, the department
sponsoring the work, and other, often political, factors.
A major problem with priority scheduling algorithms is
indefinite blocking, or starvation.
A priority scheduling algorithm can leave some low priority
processes waiting indefinitely.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Priority Scheduling
Rumor has it that, when they shut down the IBM 7094 at MIT in
1973, they found a low-priority process that had been submitted
in 1967 and had not yet been run.
A solution to the problem of indefinite blockage of low-priority
processes is aging.
Aging is a technique of gradually increasing the priority of
processes that wait in the system for a long time.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Priority Scheduling
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.
The UNIX system has a command, nice , which allows a user to
voluntarily reduce the priority of his process, in order to be nice
to the other users.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin -Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Priority Scheduling
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }
M Umair – http://www.m-umair.com
CPU Scheduler
– Round Robin Scheduling
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.
If the process is still running at the end of the quantum, the CPU
is preempted and given to another process.
{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin -Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Round Robin Scheduling
When the process uses up its quantum, it is put on the end of
the list.
{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Round Robin Scheduling
The interesting issue with round robin is the length of the
quantum.
Switching from one process to another requires a certain
amount of time for doing the context switching.
Suppose that this context switch, takes 1 msec, including
switching memory maps, flushing and reloading the cache, etc.
Also suppose that the quantum is set at 4 msec.
After doing 4 msec of useful work, the CPU will have to spend 1
msec on process switching. Twenty percent of the CPU time will
be wasted.
{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Round Robin Scheduling
To improve the CPU efficiency, we could set the quantum to 100
msec. Now the wasted time is only 1 percent.
what happens on a timesharing system if ten interactive users
hit the carriage return key at roughly the same time. Ten
processes will be put on the list of run-able processes.
If the CPU is idle, the first one will start immediately, the second
one may not start until 100 msec later, and so on. The unlucky last
one may have to wait 1 sec before getting a chance, assuming all
the others use their full quanta. Most users will perceive a 1-sec
response to a short command as sluggish.
{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com
CPU Scheduler
– Round Robin Scheduling
The conclusion can be formulated as follows:
Setting the quantum too short causes too many process
switches and lowers the CPU efficiency.
Setting it too long may cause poor response to short interactive
requests.
A quantum around 20-50 msec is often a reasonable
compromise.
{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }
M Umair – http://www.m-umair.com