Operating Systems Scheduling
Operating Systems Scheduling
SCHEDULING
5: CPU-Scheduling 1
CPU Scheduling
What Is In This Chapter?
• This chapter is about how to get a process attached to a processor.
• The design of a scheduler is concerned with making sure all users get
their fair share of the resources.
5: CPU-Scheduling 2
CPU Scheduling
What Is In This Chapter?
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Multiple-Processor Scheduling
• Real-Time Scheduling
• Thread Scheduling
• Operating Systems Examples
• Java Thread Scheduling
• Algorithm Evaluation
5: CPU-Scheduling 3
CPU SCHEDULING Scheduling
Concepts
Multiprogramming A number of programs can be in
memory at the same time. Allows
overlap of CPU and I/O.
Jobs (batch) are programs that run
without user interaction.
User (time shared) are programs that
may have user interaction.
Process is the common name for both.
CPU - I/O burst cycle Characterizes process execution,
which alternates, between CPU and
I/O activity. CPU times are
generally much shorter than I/O
times.
Preemptive Scheduling An interrupt causes currently
running process to give up the CPU
and be replaced by another process.
5: CPU-Scheduling 4
CPU SCHEDULING The 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
! Scheduling under 1 and 4 is nonpreemptive
! All other scheduling is preemptive
5: CPU-Scheduling 5
CPU SCHEDULING The Dispatcher
! Dispatcher module gives control of the CPU to the process selected by the short-
term scheduler; this involves:
!switching context
!switching to user mode
!jumping to the proper location in the user program to restart that program
! Dispatch latency – time it takes for the dispatcher to stop one process and start
another running
5: CPU-Scheduling 6
Criteria For
CPU SCHEDULING Performance
Evaluation
Note usage of the words DEVICE, SYSTEM, REQUEST, JOB.
UTILIZATION The fraction of time a device is in use. ( ratio of in-use time / total
observation time )
QUEUEING TIME Time on a queue waiting for service from the device. (seconds)
THINK TIME The time spent by the user of an interactive system to figure out the next
request. (seconds)
The goal is to optimize both the average and the amount of variation. (but beware the ogre
predictability.)
5: CPU-Scheduling 7
Scheduling
CPU SCHEDULING Behavior
Most Processes Don’t Use Up Their Scheduling Quantum!
5: CPU-Scheduling 8
CPU SCHEDULING Scheduling
Algorithms
5: CPU-Scheduling 9
CPU SCHEDULING Scheduling
Algorithms
EXAMPLE DATA:
Process Arrival Service
Time Time
1 0 8
2 1 4
3 2 9
4 3 5
FCFS
P1 P2 P3 P4
0 8 12 21 26
Residence Time
at the CPU 5: CPU-Scheduling 10
CPU SCHEDULING Scheduling
Algorithms
t( n+1 ) = w * t( n ) + ( 1 - w ) * T( n )
5: CPU-Scheduling 11
CPU SCHEDULING Scheduling
Algorithms
PREEMPTIVE ALGORITHMS:
• Yank the CPU away from the currently executing process when a higher
priority process is ready.
• Give short jobs a higher priority – perceived response time is thus better.
• What are average queueing and residence times? Compare with FCFS.
5: CPU-Scheduling 12
CPU SCHEDULING Scheduling
Algorithms
EXAMPLE DATA:
Process Arrival Service
Time Time
1 0 8
2 1 4
3 2 9
4 3 5
P1 P2 P4 P1 P3
0 1 5 10 17 26
5: CPU-Scheduling 13
CPU SCHEDULING Scheduling
Algorithms
• Assign each process a priority. Schedule highest priority first. All processes within
same priority are FCFS.
• Starvation occurs if a low priority process never runs. Solution: build aging into a
variable priority.
• Delicate balance between giving favorable response for interactive jobs, but not
starving batch jobs.
5: CPU-Scheduling 14
CPU SCHEDULING Scheduling
Algorithms
ROUND ROBIN:
• Use a timer to cause an interrupt after a predetermined time. Preempts if task
exceeds it’s quantum.
• Train of events
Dispatch
Time slice occurs OR process suspends on event
Put process on some queue and dispatch next
• Use numbers in last example to find queueing and residence times. (Use
quantum = 4 sec.)
• Definitions:
– Context Switch Changing the processor from running one task
(or process) to another. Implies changing memory.
– Processor Sharing Use of a small quantum such that each process
runs frequently at speed 1/n.
– Reschedule latency How long it takes from when a process requests
to run, until it finally gets control of the CPU.
5: CPU-Scheduling 15
CPU SCHEDULING Scheduling
Algorithms
ROUND ROBIN:
5: CPU-Scheduling 16
CPU SCHEDULING Scheduling
Algorithms
EXAMPLE DATA:
Process Arrival Service Note:
Example violates rules for
Time Time
quantum size since most
1 0 8 processes don’t finish in one
2 1 4 quantum.
3 2 9
4 3 5
P1 P2 P3 P4 P1 P3 P4 P3
0 4 8 12 16 20 24 25 26
5: CPU-Scheduling 17
CPU SCHEDULING Scheduling
Algorithms
MULTI-LEVEL QUEUES:
5: CPU-Scheduling 18
CPU SCHEDULING Using Priorities
5: CPU-Scheduling 19
CPU SCHEDULING Scheduling
Algorithms
MULTIPLE PROCESSOR SCHEDULING:
• Load sharing in the distribution of work, such that all processors have an
equal amount to do.
5: CPU-Scheduling 20
CPU SCHEDULING Linux Scheduling
Two algorithms: time-sharing and real-time
• Time-sharing
– Prioritized credit-based – process with most credits is scheduled next
– Credit subtracted when timer interrupt occurs
– When credit = 0, another process chosen
– When all processes have credit = 0, recrediting occurs
• Based on factors including priority and history
• Real-time
– Soft real-time
– Posix.1b compliant – two classes
• FCFS and RR
• Highest priority process runs first
5: CPU-Scheduling 21
CPU SCHEDULING Algorithm Evaluation
How do we decide which algorithm is best for a particular environment?
5: CPU-Scheduling 22
CPU SCHEDULING
WRAPUP
We’ve looked at a number of different scheduling algorithms.
5: CPU-Scheduling 23