Scheduling
Scheduling
Scheduling
Outline
• CPU Scheduling
• Importance of scheduling in diff. environment
• CPU bound process, I/O bound process
• Preemptive, non-preemptive scheduling
• Batch system scheduling algorithms
• FCFS, Shortest Job First, Shortest Remaining First
• Interactive system scheduling
• RR, Priority scheduling, Lottery Scheduling
• Realtime system scheduling
• Thread Scheduling
• User-level thread
• Kernel-level thread
Process States
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Process Scheduling Queues
• Job queue – set of all processes in the system
• Ready queue – set of all processes residing in main
memory, ready and waiting to execute
• Device queues – set of processes waiting for an I/O
device
• Processes migrate among the various queues
Ready Queue And Various I/O Device Queues
• Ready queue –
processes residing in
main memory, ready
and waiting to
execute
• Device queues –
processes waiting for
an I/O device
• Processes migrate
among the various
queues
Representation of Process Scheduling
Schedulers
• When to schedule?
• when a process is created, or exits
• when a process blocks on I/O, semaphore, mutex…
• interrupts: hardware or software
• timer interrupt: nonpreemptive vs preemptive scheduling
• Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue
• Short-term scheduler (or CPU scheduler) – selects which
process should be executed next and allocates CPU
Schedulers
• Short-term scheduler is invoked very frequently
(milliseconds) ⇒ (must be fast)
• Long-term scheduler is invoked very infrequently
(seconds, minutes) ⇒ (may be slow)
• The long-term scheduler controls the degree of
multiprogramming
• Processes can be described as either:
– I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts
– CPU-bound process – spends more time doing
computations; few very long CPU bursts
Process Behavior
Bursts of CPU usage alternate with periods of waiting for I/O. (a) A
CPU-bound process. (b) An I/O-bound process.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Categories of Scheduling Algorithms
• Batch system: business application, no
end users
• non-preemptive, preemptive
• Interactive system: with interactive users,
or server (with multiple remote interactive
users)
• preemption
• Real time system:
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling Algorithm Goals
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling in Batch Systems
• First-come first-served
• Shortest job first
• Shortest remaining Time next
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
First Come First Served
• First-come first-served
• processes assigned CPU in order of request
• when running process blocks, schedule next one in
queue
• when blocking process becomes ready, enter end of
queue
• Pros: simple, easy to implement, fair (in some sense)
• Cons:
• short jobs arrive after a very long job
• one compute-bound process (1 sec at a time), many IO-
bound processes (perform 1000 disk reads) => take a
long time to finish I/O bound process
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Shortest Job First
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Shortest Job First
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Shortest Remaining Time next
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling in Interactive Systems
• Round-robin scheduling
• Priority scheduling
• Multiple queues
• Shortest process next
• Guaranteed scheduling
• Lottery scheduling
• Fair-share scheduling
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Round-Robin Scheduling
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Priority Scheduling: data structure
Example (CTSS)
• CPU bound process will sink to long priority queue
• If used up quantum, move down one class
• larger quantum => cut context switch overhead
• I/O bound process will stay at high priority queue
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Interactive Systems: shortest process next
• Interactive process:
1. wait for command
2. execute command
3. go back to 1
• To minimize response time (step 2 above), schedule
process with shortest running time
• Estimate running time of a process’s step 2 using
history (weighted average, aging)
• Te’ = aTe + (1-a) Ti
• Te: current estimation
• Te’: new estimation
• Ti: current measured running time
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Interactive Systems:
Lottery Scheduling
• A randomized scheme
• each process given lottery tickets for CPU resource
• Scheduler: choose a lottery ticket at random, the process
holds the ticket is the scheduled to run
• The more tickets a process holds, the higher
probability of scheduled to run
• Pros:
• proportional allocation of CPU
• allow transferring of tickets among cooperating
processes,
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Scheduling in Real-time Systems
» Figure 2-44. (a) Possible scheduling of user-level threads with a 50-msec process
quantum and threads that run 5 msec per CPU burst.
Thread Scheduling
» Figure 2-44. (b) Possible scheduling of kernel-level threads with the same characteristics
as (a).
Outline
• CPU Scheduling
• Importance of scheduling in diff. environment
• CPU bound process, I/O bound process
• Preemptive, non-preemptive scheduling
• Batch system scheduling algorithms
• FCFS, Shortest Job First, Shortest Remaining First
• Interactive system scheduling
• RR, Priority scheduling, Lottery Scheduling
• Realtime system scheduling
• Thread Scheduling
• User-level thread
• Kernel-level thread