Slides

advertisement
Operating Systems
CMPSC 473
CPU Scheduling
February 12, 2008 - Lecture 8
Instructor: Trent Jaeger
• Last class:
– Threads
• Today:
– CPU Scheduling
Resource Allocation
• In a multiprogramming system, we need to share
resources among the running processes
– What are the types of OS resources?
• Question: Which process gets access to which
resources?
– To maximize performance
Resources Types
• Memory: Allocate portion of finite resource
– Virtual memory tries to make this appear infinite
– Physical resources are limited
• I/O: Allocate portion of finite resource and time with
resource
– Store information on disk
– A time slot to store that information
• CPU: Allocate time slot with resource
– A time slot to run instructions
• We will focus on CPU scheduling in the section
CPU Scheduling Examples
• Single process view
– GUI request
• Click on the mouse
– Scientific computation
• Long-running, but want to complete ASAP
• System view
– Get as many tasks done as quickly as possible
– Minimize waiting time for processes
– Utilize CPU fully
Process Scheduling
Dispatched (CPU assigned)
Process
Terminates
Running
Ready
Pre-empted (CPU yanked)
Wait
For
Event
(e.g. I/O)
Event
Occurred
Blocked
New process
creation
Scheduling Problem
• Choose the ready/running process to run at any time
– Maximize “performance”
• Model/estimate “performance” as a function
– System performance of scheduling each process
• f(process) = y
– What are some choices for f(process)?
• Choose the process with the best y
– Estimating overall performance is intractable
• E.g., scheduling so all tasks are completed as soon as possible
Scheduling Concepts
When Can Scheduling Occur?
CPU scheduling decisions may take place when a
process:
•
1.
2.
3.
4.
Switches from running to waiting state
Switches from running to ready state
Switches from waiting to ready
Terminates
Scheduling for events 1 and 4 do not preempt a
process
•
•
Process volunteers to give up the CPU
Preemptive vs Nonpreemptive
• Can we reschedule a process that is actively running?
– If so, we have a preemptive scheduler
– If not, we have a non-preemptive scheduler
•
Suppose a process becomes ready
– E.g., new process is created or it is no longer waiting
• It may be better to schedule this process
– So, we preempt the running process
• In what ways could the new process be better?
Bursts
• A process runs in CPU and I/O Bursts
– Run instructions (CPU Burst)
– Wait for I/O (I/O Burst)
• Scheduling is aided by knowing the length of
these bursts
– More later…
Bursts
CPU Burst Duration
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
Scheduling Loop
• How a system runs
– From a scheduling perspective
• Don’t care about what the process is actually doing…
• Sequence of:
– Run
– Scheduling event
– Schedule
• Latency
– Dispatch (if necessary)
• Latency
– Rinse, repeat…
Scheduling Criteria
•
•
•
•
•
•
Utilization/efficiency: keep the CPU busy 100% of the time with
useful work
Throughput: maximize the number of jobs processed per hour.
Turnaround time: from the time of submission to the time of
completion.
Waiting time: Sum of times spent (in Ready qqueue) waiting to be
scheduled on the CPU.
Response Time: time from submission till the first response is
produced (mainly for interactive jobs)
Fairness: make sure each process gets a fair share of the CPU
Scheduling Algorithms
One Algorithm
• First-Come, First-Served (FCFS)
– Serve the jobs in the order they arrive.
– Non-preemptive
– Simple and easy to implement: When a process
is ready, add it to tail of ready queue, and serve
the ready queue in FCFS order.
– Very fair: No process is starved out, and the
service order is immune to job size, etc.
First-Come, First-Served (FCFS)
•
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
0
•
•
P2
24
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
P3
27
30
Reducing Waiting Time
Suppose that the processes arrive in the order
P2 , P3 , P1
• The Gantt chart for the schedule is:
P2
0
•
•
•
•
P3
3
P1
6
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect short process behind long process
30
Shortest-Job-First (SJF)
Scheduling
• Associate with each process the length of its next CPU
burst. Use these lengths to schedule the process with the
shortest time
• Two schemes:
– Non-preemptive – once CPU given to the process it cannot be
preempted until completes its CPU burst
– Preemptive – if a new process arrives with CPU burst length less
than remaining time of current executing process, preempt. This
scheme is know as the Shortest-Remaining-Time-First (SRTF)
• SJF is optimal – gives minimum average waiting time for a given
set of processes
Example of Non-Preemptive
SJF
•
Process
Arrival Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
SJF (non-preemptive)
P1
0
•
3
P3
7
Burst Time
7
4
1
4
P2
8
P4
12
Average waiting time = (0 + 6 + 3 + 7)/4 = 4
16
Example of Preemptive SJF
•
Process
P1
P2
P3
P4
SJF (preemptive)
P1
P3
P2
Burst Time
7
4
1
4
P4
11
2
4
5
7
Average waiting time = (9 + 1 + 0 +2)/4 = 3
0
•
P2
Arrival Time
0.0
2.0
4.0
5.0
P1
16
Determining Next CPU Burst
• Can only estimate the length
• Can be done by using the length of previous
CPU bursts, using exponential averaging
1. t n = actual length of n th CPU burst
2. # n +1 = predicted value for the next CPU burst
3. " , 0 ! " ! 1
4. Define : ! n =1 = " t n + (1 # " )! n .
Determining Next CPU Burst
• If α=0, no weightage to recent history
• If α=1, no weightage to old history
• Typically, choose α=1/2 which gives more weightage to
newer information compared to older information.
1. t n = actual length of n th CPU burst
2. # n +1 = predicted value for the next CPU burst
3. " , 0 ! " ! 1
4. Define : ! n =1 = " t n + (1 # " )! n .
Exponential Averaging
• If we expand the formula, we get:
τn+1 = α tn+(1 - α)α tn -1 + …
+(1 - α )j α tn -j + …
+(1 - α )n +1 τ0
• Since both α and (1 - α) are less than or equal
to 1, each successive term has less weight
than its predecessor
Prediction of the Length
of the Next CPU Burst
Summary
• CPU Scheduling
– Choose the process to assign to the CPU
• To maximize “performance”
– Hard problem in general
– Goal: minimize average waiting time
• CPU bursts
• Can devise optimal algorithms
– If we can only predict the next CPU burst
– Algorithms
• FCFS
• SJF
• Next time: More CPU Scheduling
Algorithms and Systems
Download