Synchronization Issue CPU Scheduling • Consider the following program:

advertisement
TDDI12:
Operating Systems, Real-time and
Concurrent Programming
Synchronization Issue
[SGG7] Chapter 5
•
void main(void) {
blocked[0] = false;
blocked[1] = false;
turn = 0;
start in parallel(P(0), P(1));
}
CPU Scheduling
Introduce CPU scheduling
Describe various CPU scheduling algorithms
Discuss evaluation criteria of CPU scheduling
algorithms
Copyright Notice: The lecture notes are mainly based on Silberschatz’s, Galvin’s and Gagne’s book (“Operating System
Concepts”, 7th ed., Wiley, 2005). No part of the lecture notes may be reproduced in any form, due to the copyrights
reserved by Addison-Wesley. These lecture notes should only be used for internal teaching purposes at the Linköping
University.
Andrzej Bednarski, IDA
Linköpings universitet, 2006
Consider the following program:
•
boolean blocked[2];
int turn;
void P(int id) {
while(true) {
blocked[id] = true;
while(turn != id) {
while(blocked[1-id])
/* do nothing */;
turn = id;
}
/* critical section */
blocked[id] = false;
/* reminder section */
}
}
This software solution to the mutual exclusion problem for two processes is
proposed in Hyman, H. “Comments on a Problem in Concurrent
Programming Control.”, ACM, January 1966. Find a counterexample that
demonstrates that this solution is incorrect.
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.2
Silberschatz, Galvin and Gagne ©2005
Agenda
Basic Concepts
•
•
•
•
•
•
•
•
Maximum CPU utilization obtained with multiprogramming
•
CPU–I/O Burst Cycle – Process execution consists of
+ cycle of CPU execution, and
+ I/O wait
•
CPU burst distribution
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Multiple-Processor Scheduling
Thread Scheduling
Operating Systems Examples
Algorithm Evaluation
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.3
Silberschatz, Galvin and Gagne ©2005
Alternating Sequence of CPU And I/O Bursts
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.5
Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.4
Silberschatz, Galvin and Gagne ©2005
Histogram of CPU-burst Times
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.6
Silberschatz, Galvin and Gagne ©2005
1
CPU Scheduler
Dispatcher
•
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 (task arrives).
4. Terminates.
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 under 1 and 4 is nonpreemptive.
•
All other scheduling is preemptive.
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.7
Silberschatz, Galvin and Gagne ©2005
Scheduling Criteria
CPU utilization – quotient of time the CPU is busy and the
observation period
•
Throughput – number of processes that complete their
execution per time unit
•
Turnaround time – the time between job submit time
and job completion time.
•
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, not output
(for time-sharing environment)
5.9
Silberschatz, Galvin and Gagne ©2005
First-Come, First-Served (FCFS)
Scheduling
•
Example:
Process
Burst Time (ms)
24
3
3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt chart for the schedule is:
•
Maximize CPU utilization
•
Maximize throughput
•
Minimize turnaround time
•
Minimize waiting time
•
Minimize response time
TDDI12, A. Bednarski, IDA, Linköpings universitet
P2
0
24
P3
27
30
Waiting time for P1 = 0; P2 = 24; P3 = 27ms
Average waiting time: (0 + 24 + 27)/3 = 17ms
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.10
Silberschatz, Galvin and Gagne ©2005
Suppose that the processes arrive in the order
P2 , P3 , P1 .
• The Gantt chart for the schedule is:
P2
0
P1
•
•
Silberschatz, Galvin and Gagne ©2005
FCFS Scheduling (Cont.)
P1
P2
P3
•
5.8
Optimization Criteria
•
TDDI12, A. Bednarski, IDA, Linköpings universitet
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.11
Silberschatz, Galvin and Gagne ©2005
•
•
•
•
P3
3
P1
6
30
Waiting time for P1 = 6ms; P2 = 0ms; P3 = 3ms
Average waiting time: (6 + 0 + 3)/3 = 3ms
Much better than previous case
Convoy effect: short process behind long process
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.12
Silberschatz, Galvin and Gagne ©2005
2
SJF (nonpreemptive)
Shortest-Job-First (SJF) Scheduling
•
•
Process
Associate with each process the length of its next CPU burst. Use
these lengths to schedule the process with the shortest time.
P1
P2
P3
P4
Two schemes:
+ Nonpreemptive – once CPU given to the process it cannot be
preempted until it completes its CPU burst.
+ Preemptive – if a new process arrives with CPU burst length
less than remaining time of current executing process,
preempt.
Shortest-Remaining-Time-First (SRTF).
P4
0
•
SJF is optimal – gives minimum average waiting time for a given
set of processes.
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.13
Silberschatz, Galvin and Gagne ©2005
Determining Length of Next CPU Burst
•
Can be done by using the length of previous CPU bursts,
using exponential averaging.
•
1. t n = actual lenght of n th CPU burst
•
3. α , 0 ≤ α ≤ 1
4. Define :
5.15
•
Silberschatz, Galvin and Gagne ©2005
P2
16
9
24
5.14
Silberschatz, Galvin and Gagne ©2005
Examples of Exponential Averaging
•
TDDI12, A. Bednarski, IDA, Linköpings universitet
3
TDDI12, A. Bednarski, IDA, Linköpings universitet
Can only estimate the length.
τ n +1 = α t n + (1 − α )τ n .
P3
Average waiting time = (3 + 16 + 9 + 0)/4 = 7ms
•
2. τ n +1 = predicted value for the next CPU burst
P1
Burst Time (ms)
6
8
7
3
α =0
+ τn+1 = τn
+ Recent history does not count.
α =1
+ τn+1 = tn
+ Only the actual last CPU burst counts.
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.
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.16
Silberschatz, Galvin and Gagne ©2005
SJF (preemptive)
Prediction of Next CPU Burst
Process
Arrival Time (ms)
0
1
2
3
P1
P2
P3
P4
12
10
Burst Time (ms)
8
4
9
5
8
•
6
4
Shortest Remaining Time
P1
P2
P4
P1
P3
2
0 1
CPU burst (ti)
-
6
4
6
13
13
13
…
“guess” (ττi)
10
8
6
5
9
11
11
…
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.17
Silberschatz, Galvin and Gagne ©2005
5
17
10
26
Average waiting time = (9 + 0 + 15 + 2)/4 = 6.5
calculated as: (10 – 1) + (1 – 1) + (17 – 2) + (5 – 3)
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.18
Silberschatz, Galvin and Gagne ©2005
3
Priority Scheduling
Process
Burst Time (ms)
10
1
2
1
5
P1
P2
P3
P4
P5
P2
0 1
•
•
Priority Scheduling (Cont.)
P5
Priority
3
1
4
5
2
P1
18 19
A priority number (integer) is associated with each process
Priorities
+ Internal (time limits, memory requirement, ...)
+ External (process importance, political/economical factors, …)
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.19
The CPU is allocated to the process with the highest priority
(smallest integer ≡ highest priority).
+ Preemptive
+ Nonpreemptive
•
SJF is a priority scheduling where priority is
the predicted next CPU burst time.
•
Problem: Starvation – low priority processes may never execute.
•
Solution: Aging – as time progresses
increase the priority of the process.
P3 P4
16
6
•
Silberschatz, Galvin and Gagne ©2005
Round Robin (RR)
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.20
Silberschatz, Galvin and Gagne ©2005
Example of RR with Time Quantum=20
•
Each process gets a small unit of CPU time (time quantum,
time slice), 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.
Process
Burst Time
53
17
68
24
P1
P2
P3
P4
•
The Gantt chart is:
No process waits more than (n -1)q time units.
P1
•
Performance
+ q large ⇒ FIFO
+ q small ⇒ Processor sharing
(virtually n processes running at the speed of 1/n)
Observe that q must be large with respect to context switch,
otherwise overhead is too high.
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.21
Silberschatz, Galvin and Gagne ©2005
5.23
•
P2
20
37
P3
P4
57
P1
77
P3
P4
P1
P3
P3
97 117 121 134 154 162
RR has typically higher average turnaround than SJF,
but better response.
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.22
Silberschatz, Galvin and Gagne ©2005
Turnaround Time Varies
with The Time Quantum
How a Smaller Time Quantum
Increases Context Switches
TDDI12, A. Bednarski, IDA, Linköpings universitet
0
Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.24
Silberschatz, Galvin and Gagne ©2005
4
Multilevel Queue
Example of Multilevel Queue
•
Ready queue is partitioned into separate queues:
+ foreground (interactive)
+ background (batch)
•
Each queue has its own scheduling algorithm
+ foreground – RR + background – FCFS
•
Scheduling must be done between the queues.
+ Fixed priority scheduling; i.e., serve all from foreground then
from background. Possibility of starvation.
+ Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e.,
- 80% to foreground in RR
- 20% to background in FCFS
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.25
Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.26
Silberschatz, Galvin and Gagne ©2005
Multilevel Feedback Queue
Example of Multilevel Feedback Queue
•
A process can move between the various queues;
aging can be implemented this way
•
•
Multilevel-feedback-queue 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
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.27
Silberschatz, Galvin and Gagne ©2005
Three queues:
+ Q0 – RR with time quantum 8 ms
+ Q1 – RR time quantum 16 ms
+ Q2 – FCFS
Q0
Q1
Q2
•
Scheduling
+ A new job enters queue Q0 which is served RR.
̶ When it gains CPU, job receives 8 ms.
̶ If it does not finish in 8 ms,
job is moved to queue Q1.
+ At Q1 job is again served RR and receives 16 additional ms.
+ If it still does not complete, it is preempted and moved to
queue Q2 served in FCFS.
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.28
Silberschatz, Galvin and Gagne ©2005
Multiple-Processor Scheduling
Thread Scheduling
•
CPU scheduling more complex when multiple CPUs are available.
•
User-level and kernel-level threads
•
Homogeneous processors within a multiprocessor.
•
•
Asymmetric multiprocessing – only one processor accesses the
system data structures, alleviating the need for data sharing.
Contention scope
+ Process-contention scope (PCS) – local
The threads library decides which thread to put
onto an available LWP
•
Symmetric multiprocessing (SMP) – each processor is selfscheduling.
•
Processor affinity
•
Load sharing/balancing
•
Symmetric multithreading (SMT)
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.29
Silberschatz, Galvin and Gagne ©2005
+ System-contention scope (SCS) – global
Kernel decides which kernel thread to run next
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.30
Silberschatz, Galvin and Gagne ©2005
5
Solaris Scheduling
Windows XP: Priority-based Scheduling
•
Dispatch table for interactive
and time-sharing threads
Scheduling
TDDI12, A. Bednarski, IDA, Linköpings universitet
Priority-based
+ Variable class (priorities 1-15)
+ Real-time class (priorities 15-32)
+ Idle thread (priority 0): memory management
5.31
Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet
Linux Scheduling
Algorithm Evaluation
•
•
•
Preemptive, priority based scheduling with two priority ranges:
+ Real-time (0-99)
+ Other (100-140)
Complexity: O(1)
•
•
•
5.32
Silberschatz, Galvin and Gagne ©2005
Deterministic modeling:
Takes a particular predetermined workload and defines the
performance of each algorithm for that workload.
Queueing models – mathematical formulation
Simulation models – based on system trace
Implementation – evaluation in real situation
Active/expired arrays of tasks
Evaluation of CPU
schedulers by simulation
Priorities/time-slice relation
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.33
Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.34
Silberschatz, Galvin and Gagne ©2005
Recommended Reading and Exercises
•
Reading:
+ Chapter 5 [SGG7]
+ Chapter 6 (sixth edition)
•
Exercises:
+ All
•
No project in Chapter 5
TDDI12, A. Bednarski, IDA, Linköpings universitet
5.35
Silberschatz, Galvin and Gagne ©2005
6
Download