Transcript CPU Scheduling
CPU Scheduling Basic Concepts
Chapter 5: CPU Scheduling
• • • • • • • Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm Evaluation
Objectives
• • To introduce CPU scheduling, which is the basis for multiprogrammed operating systems • To describe various CPU-scheduling algorithms To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system
Basic Concepts
• • Maximum CPU utilization obtained with multiprogramming CPU –I/O Burst Cycle – Process execution consists of a
cycle
of CPU execution and I/O wait •
CPU burst
distribution
Histogram of CPU-burst Times
CPU-I/O Burst Cycle
• • • • Process execution consists of a cycle of CPU execution and I/O wait Processes alternate between these two states Each scheduling decision is about which job to give to the CPU for use by its next CPU burst With time slicing, job may be forced to give up CPU before finishing current CPU burst
CPU 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 All other scheduling is nonpreemptive preemptive
Scheduling
• Managing resource (e.g., CPU) • n processes – A schedule is a listing – Number of possible schedules if no interdependencies?
• Environment – Number of processors involved – Whether preemption is allowed (priorities or time slicing) – Whether a process once active can become blocked (due to I/O, etc…)
Scheduling
• First the easy case – One processor, no preemption, no blocking – n/1 static scheduling problem • n processes • Know arrival times, processing times
Scheduling
• • Given the following information about process P i – Arrival Time a i – Execution Time e i – Due Time d i We can obtain – Completion (finish) Time f i – Turn-around Time t i = ( f i – a i ) – Waiting Time w i = ( t i – e i ) – Lateness L i = ( f i – d i ) – Tardiness T i = max(0, L i )
P 1 P 2 P 3 P 4 P 5 P 6 Process p i P 1 15 P 2 25
Example
P 3 5 P 4 20 P 5 25 P 6 30 t i 65 120 50 20 45 95 w i 50 95 45 0 20 65 P 4 P 5 P 3 P 1 P 6 P 2 0 20 45 50 65 95 120
Scheduling Criteria
• • • • • CPU utilization – keep the CPU as busy as possible Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process Waiting time – amount of time a process has been waiting in the ready queue Response time – amount of time it takes from when a request was submitted until the first response is produced
How good is a schedule? Performance Measures • • • • • Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time
First-Come, First-Served (FCFS) Scheduling • First-Come, First-Served (FCFS) – Also “First In, First Out” (FIFO) or “Run until done” • In early systems, FCFS meant one program scheduled until done (including I/O) • Now, means keep CPU until process blocks
First-Come, First-Served (FCFS) Scheduling Process p i P 1 24 P 2 3 P 3 3 • Suppose that the processes arrive in the order: P 1 , P 2 , P 3 Draw the Gantt Chart for the schedule and compute the
.
average waiting time: 3 P 2 P 3 P 1 0 3 6 30
First-Come, First-Served (FCFS) Scheduling Process p i P 1 24 P 2 3 P 3 3 • Suppose that the processes arrive in the order: P 2 , P 3 , P 1 Draw the Gantt Chart for the schedule and compute the
.
average waiting time: 17 0 P 1 24 P 2 27 P 3 30
First-Come, First-Served (FCFS) Scheduling • There is a convoy effect as all the other processes wait for the one big process to get off the CPU – Average waiting time for FCFS is often quite long – What about average turnaround time?
• Pros and cons – Simple (+) – Short jobs get stuck behind long ones (-) • Analogy: getting milk at a supermarket and waiting to pay, always stuck behind carts full of items. Upside: get to read about space aliens!
•
Example of (Shortest Job First) SJF
Process Arrival Time
P 1 P 2 P 3 P 4
0.0
2.0
4.0
5.0
SJF scheduling chart Burst Time 6 8 7 3 P 4 P 1 P 3 P 2 0 3 9 16 24 • Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
SJF Scheduling example Process p i P 1 15 P 2 25 P 3 5 P 4 20 P 5 18 P 6 30 • • Sometimes called Shortest Processing Time (SPT) First Draw the Gantt Chart for a SPT schedule and compute the average waiting time and average turnaround time (assume all processes arrive at time 0)
Round Robin (RR)
• • • Each process gets a small unit of CPU time (
time quantum
), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.
If there are
n
processes in the ready queue and the time quantum is
q
, then each process gets 1/
n
of the CPU time in chunks of at most
q
time units at once. No process waits more than (
n
-1)
q
time units.
Performance –
q
large FIFO –
q
small
q
must be large with respect to context switch, otherwise overhead is too high