1 Computer Systems II Process Scheduling. 2 Review of Process States.
-
Upload
julia-hill -
Category
Documents
-
view
218 -
download
2
Transcript of 1 Computer Systems II Process Scheduling. 2 Review of Process States.
1
Computer Systems II
Process Scheduling
2
Review of Process States
3
CPU Scheduling Question: Any process in the pool of ready processes is
ready to run. Which one to pick to run next?
CPU scheduling- Selecting a new process to run from the Ready queue
Preemptive scheduling - Running process may be interrupted and moved to the
Ready queue
Non-preemptive scheduling: - once a process is in Running state, it continues to execute
until it terminates or blocks for I/O or system service
4
When Does a Scheduler Take Decisions?
1. Running - Blocked
2. Running - Terminate
3. Blocked - Ready
4. Running - Ready
Scheduling under 1 and 2: - nonpreemptive scheduling
Scheduling under 3 and 4:- premeptive scheduling
5
Process Set:
(numbers represent time units)
Processes requiring service:
Motivating Example
6
What Are We Trying to Optimize? System-oriented metrics:
- CPU utilization: percentage of time the processor is busy
- Throughput: number of processes completed per unit of time
User-oriented metrics:
- Turnaround time: interval of time between submission and termination (including any waiting time). Appropriate for background jobs
- Response time: for interactive jobs, time from the submission of a request until the response begins to be received
- Missed time: sum of the periods spent in the Ready queue, while other processes run
7
Scheduling Criteria Maximize
- CPU utilization
- Throughput
Minimize- Turnaround time
- Missed time
- Response time
Problem: mutually exclusive objectives- No one best way
- Conflict between missed/response time and throughput
8
CPU-bound vs. I/O –bound
Processes
9
Alternating CPU and I/O Bursts
10
Process Behavior Observed property of processes
- alternate between CPU execution and I/O wait
CPU-bound job: little I/O, long CPU bursts
I/O-bound job: lots of I/O, short CPU bursts
Problem: don’t know the bound type before running
An underlying assumption:- response time most important for I/O bound processes
emacsemacs
Matrix multiplication
11
Long vs. Short Processes Call a CPU-bound process a long process:
- Spends long time on CPU computations, seldom waits for I/O
Call an I/O-bound process a short process:- Small amounts of computation are sandwiched between longer
periods of waiting.
12
Scheduling Algorithms
FCFS
RR
SPN / PSPN
Priority
13
Scheduling Algorithms Next we take a look at four scheduling algorithms and
their amusing behavior
1. First Come First Served – FCFS
2. Round Robin – RR
3. Shortest Process Next – SPN, PSPN
4. Multi-Level Feedback (Priority)
Scheduling very ad hoc. “Try and See”
14
First Come First Served (FCFS or FIFO) Simplest scheduling algorithm:
- Run jobs in order that they arrive
- Uniprogramming: run until done (non-preemptive)
- Multiprogramming: put job at back of queue when blocks on I/O (we’ll assume this)
Advantage: simple
Disadvantages: ???
15
FCFS Example
16
Another FCFS Example
Process Service Time
P1 100
P2 2
P3 1
Suppose that processes arrive in the order: P1 , P2 , P3
FCFS Scheduling:
Missed time for P1 = 0; P2 = 100; P3 = 102 Average missed time: (0 + 100 + 102)/3 ~ 67 Average turnaround time: (100+102+103)/3 ~ 101
P1 P2 P3
100 102 1030
17
What if … … the processes arrive in the order
P2 (2), P3 (1), P1 (100) The chart for the schedule is:
Missed time for P1 = 3; P2 = 0; P3 = 2 Average missed time: (3 + 0 + 2)/3 ~ 2 Average turnaround time: (102+2+3)/3 ~ 36
Long processes love FCFS; short processes hate it!
P1P3P2
32 1030
18
FCFS Disadvantage
Short processes may be
stuck
behind long processes
19
Round Robin (RR) Solution to job monopolizing CPU? Interrupt it.
- Run process for some “time quantum” (time slice) - When time is up or process blocks, move it to the back of the ready queue- Most systems do some flavor of this
Advantages:- Fair allocation of CPU across jobs- Low average waiting time when job lengths vary:
- Assume P1(100), P2(2), P3(1) and time quantum = 1
- What is the average missed time and completion time?
Add a timerAdd a timer
1 2 3 4 5 1031 2 3 4 5 103CPU P2P1 P3 P1 P2 P1
20
RR Example: Time Quantum = 1
21
RR Example: Time Quantum = 4
22
What if … … the processes have similar sizes? Assume 2 processes of about 100 time units each:
- What is the average missed time?
- What is the average completion time?
- How does it compare to FCFS for the same two processes?
Main interesting thing is the length of the time slice1 2 3 4 5 199 200
CPU P2P1 P1 P2 P1 P2P1 P1 P2
23
Round Robin’s BIG Disadvantage
Performance depends
on
the sizes of processes and
the length of the time slice
24
RR Time Slice Tradeoffs (1) Performance depends on the length of the timeslice
Context switching is not a free operation
- If timeslice time is set too high (attempting to amortize context switch cost), you get FCFS (that is, processes will finish or block before their slice is up anyway)
- If timeslice is set too low, all time is spent doing context switching between processes
25
RR Time Slice Tradeoffs (2)
Timeslice frequently set to ~100 milliseconds
Context switches typically cost < 1 millisecond
- context switching is usually negligible (< 1% per timeslice in above example) unless you context switch too frequently and lose all productivity.
26
Shortest-Process-Next (SPN, PSPN) Requires explicit information about the service-time
requirements for each process. Use this information to schedule the process with the shortest time.
Nonpreemptive - Once CPU is given to a process, it cannot be preempted.
Preemptive - The PSPN preempts the current process when another process
arrives, with a total service time requirement less than the remaining service time required by the current process.
27
SPN Example
28
Another SPN Example
3 processes, P1(100), P2(2), P3(1)
- Average completion = (1+3+103) / 3 = ~35 (vs ~101 for FCFS)
Provably optimal- Moving shorter process before longer process improves waiting
time of short process more than harms missed time for long process
CPU P1P3 P2
1 3 103
29
PSPN Example
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
STCF (preemptive)
Average missed time = (9 + 1 + 0 +2)/4 = 3 Average completion time = ?
P1 P3P2
42 110
P4
5 7
P2 P1
16
30
PSPN is Optimal but Unfair
Gives minimum average missed time
Long jobs may starve if too many short jobs
Difficult to implement - how do you know how long a process takes ?
Solution- use the past to predict the future
31
Use the Past to Predict the Future … Use the past to predict the future #1:
- Long running job will probably take a long time more
Use the past to predict the future #2:- View job as sequence of alternating CPU and I/O jobs
- If previous CPU pieces in the sequence have run quickly, future ones will too (usually). Priority must be given to interactive jobs.
gccsample
pico
32
Use the Past to Predict the Future …
Example: Predict length of current CPU burst using length of previous burst- Record length of previous burst (0 when just created)
- At scheduling event (unblock, block, exit, …) pick the smallest “past run length” off of Ready queue
9 10 3 0Pick
0
100 ms
9 10 12 100 9
time
9 10 3 100Pick
12 ms
3
Pick
33
Approximate SPN – Exponential Average
~SPN - exponential averaging
= 0- en+1 = en (recent history does not count)
=1- en+1 = tn (only the actual last CPU burst counts)
.t nnn ee 11
:Define 4.
1, is a constant0 , 3.
burst CPU next the for value expected 2.
burst CPU of length actual 1.
en+1
thn nt
34
Exponential Average (2)
Expand the formula:
en+1 = tn+(1 - ) tn-1 + (1 - ) 2 tn-2 …
+(1 - ) j tn-j+ …
+(1 - ) n+1 t0
Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor (recent past counts more)
.t nnn ee 11
35
Multi-Level Feedback (FB) Motivation -- look at PSPN:
- Faster for a longer time slice (low switching overhead) - So grow the time slice for a long process, but decrease its
priority to prevent starvation of others
Idea: Separate processes with different lengths (CPU bursts)
36
Multi-Level Feedback (FB) Split the Ready list into a number of queues:
queue 0, queue 1, queue 2, and so on. Lower-numbered queues have higher priority. When the current process is interrupted at the end of its
quantum, a new process is selected from the front of the lowest-numbered queue that has any processes.
After a process has used a quanta in its queue, it is placed at the end of the next-higher numbered queue.
The word ‘‘feedback’’ in the name of this method refers to the fact that processes can move from one queue to another.
37
FB Example
38
FB Discussion
Attacks both efficiency and response time problems- Efficiency: long time quanta = low switching overhead
- Response time: quickly run after becoming unblocked Some problems
- A user can insert I/O just to keep priority high. Can low priority processes starve?
Solution: when skipped over, increase priority
- What about when past does not predict future? For instance, a CPU bound (long) process switches to I/O bound (short).
Solution: let past predictions “age” and count less towards current view of the world. Increase the priority of the process.
39
FB Variants
Let the quantum size depend on the queue. A queue numbered n could have a quantum of length 2n q , where q is the ‘‘basic quantum’’ size. Therefore, the queues have quanta of sizes q , 2q , 4q , 8q , and so on.
Let a process in queue use several quanta before being demoted to the next queue
Promote a process to a higher-priority queue after it spends a certain amount of time waiting for service in its current queue.
Instead of granting absolute priority to low-numbered queues, grant slices of time to each queue, with lower-numbered queues receiving larger slices.
These variants can be used by themselves or in any combination.
40
FB Example with Doubling Quanta
41
Some problems
A user can insert I/O just to keep priority high Can low priority processes starve?
- ad hoc: when skipped over, increase priority What about when past does not predict future?
- For instance, a CPU bound process switches to I/O bound
- want past predictions to “age” and count less towards current view of the world
42
FB Variants Scheduler defined by the following parameters:
- number of queues
- scheduling algorithms for each queue
- method used to determine when to upgrade a process
- method used to determine when to demote a process
- method used to determine which queue a process will enter when that process needs service
Windows Operating System- 32 priority levels
- If a running process receives an interrupt, priority is lowered
- If a process unblocks from a waiting queue, priority increases
43
Traditional UNIX Scheduling (1) Uses multilevel feedback queues 128 priorities possible (0-127) One Round Robin queue per priority At every scheduling event, the scheduler
- picks the non-empty queue of highest priority
- runs processes in round-robin Scheduling events:
- Clock interrupt
- Process does a system call
- Process gives up CPU (for instance, to do I/O)
44
Traditional UNIX Scheduling (2) All processes assigned a baseline priority based on the type
and current execution status: - swapper 0
- waiting for disk 20
- waiting for lock 35
- user-mode execution 50
At scheduling events, priorities are adjusted based on the amount of CPU used, the current load, and how long the process has been waiting
45
Linux - Lottery Scheduling Problem: this whole priority thing is really ad-hoc
- how to ensure that jobs will be equally penalized under load?
Lottery scheduling- give each process some number of tickets- at each scheduling event, randomly pick a ticket- run winning process- to give a process n% of CPU, give it total_tickets * n%
How to use?- approximate priority: low priority give few tickets; high
priority give many tickets
- approximates SRT: short processes give more tickets; long processes fewer tickets. If job has at least one ticket, it won’t starve
Summary FCFS:
- Advantage: simple- Disadvantage: short jobs can get stuck behind long ones
RR:- Advantage: better for short jobs- Disadvantage: poor when jobs are the same length
SPN / PSPN: - Advantage: optimal- Disadvantages: hard to predict the future;
unfair to long running jobs Multi-level feedback:
- Advantage: approximates PSPN- Disadvantage: still a bit unfair to long running jobs