Upload
ngonhu
View
239
Download
4
Embed Size (px)
Citation preview
1
6.1 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Chapter 6: CPU Scheduling
n Basic Concepts
n Scheduling Criteria
n Scheduling Algorithms
n Multiple-Processor Scheduling
n Real-Time Scheduling
n Thread Scheduling
n Operating Systems Examples
n Java Thread Scheduling
n Algorithm Evaluation
6.2 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Basic Concepts
n Maximum CPU utilization obtained with multiprogramming
n CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait
n CPU burst distribution
2
6.3 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Alternating Sequence of CPU And I/O Bursts
6.4 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Histogram of CPU-burst Times
3
6.5 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
CPU Scheduler
n Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them
n 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
n Scheduling under 1 and 4 is nonpreemptive
n All other scheduling is preemptive
6.6 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Dispatcher
n Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:
l switching context
l switching to user mode
l jumping to the proper location in the user program to restart that program
n Dispatch latency – time it takes for the dispatcher to stop one process and start another running
4
6.7 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Scheduling Criteria
n CPU utilization – keep the CPU as busy as possible
n Throughput – # of processes that complete their execution per time unit
n Turnaround time – amount of time to execute a particular process
n Waiting time – amount of time a process has been waiting in the ready queue
n 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)
6.8 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Optimization Criteria
n Max CPU utilization
n Max throughput
n Min turnaround time
n Min waiting time
n Min response time
5
6.9 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
First-Come, First-Served (FCFS) Scheduling
Process Burst Time
P1 24
P2 3
P3 3
n Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:
n Waiting time for P1 = 0; P2 = 24; P3 = 27
n Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 300
6.10 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1
n The Gantt chart for the schedule is:
n Waiting time for P1 = 6; P2 = 0; P3 = 3
n Average waiting time: (6 + 0 + 3)/3 = 3
n Much better than previous case
n Convoy effect short process behind long process
P1P3P2
63 300
6
6.11 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Shortest-Job-First (SJR) Scheduling
n Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time
n Two schemes:
l nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst
l preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. Thisscheme is know as the Shortest-Remaining-Time-First (SRTF)
n SJF is optimal – gives minimum average waiting time for a given set of processes
6.12 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
n SJF (non-preemptive)
n Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Example of Non-Preemptive SJF
P1 P3 P2
73 160
P4
8 12
7
6.13 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
n SJF (preemptive)
n Average waiting time = (9 + 1 + 0 +2)/4 = 3
P1 P3P2
42 110
P4
5 7
P2 P1
16
6.14 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Determining Length of Next CPU Burst
n Can only estimate the length
n Can be done by using the length of previous CPU bursts, using exponential averaging
:Define 4.10 , 3.
burst CPU next the for value predicted 2.
burst CPU of lenght actual 1.
1
≤≤=
=
+
αατ n
thn nt
( ) .1 1 nnn t ταατ −+==
8
6.15 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Prediction of the Length of the Next CPU Burst
6.16 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Examples of Exponential Averaging
n α =0
l τn+1 = τn
l Recent history does not count
n α =1
l τn+1 = tn
l Only the actual last CPU burst counts
n If we expand the formula, we get:τn+1 = α tn+(1 - α) α tn -1 + …
+(1 - α )j α tn -1 + …
+(1 - α )n+1 tn τ0
n Since both α and (1 - α) are less than or equal to 1, each successive term has less weight than its predecessor
9
6.17 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Priority Scheduling
n A priority number (integer) is associated with each process
n The CPU is allocated to the process with the highest priority (smallest integer ≡ highest priority)
l Preemptive
l nonpreemptive
n SJF is a priority scheduling where priority is the predicted next CPU burst time
n Problem ≡ Starvation – low priority processes may never execute
n Solution ≡ Aging – as time progresses increase the priority of the process
6.18 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Round Robin (RR)
n 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.
n 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.
n Performancel q large ⇒ FIFO
l q small ⇒ q must be large with respect to context switch, otherwise overhead is too high
10
6.19 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
n The Gantt chart is:
n Typically, higher average turnaround than SJF, but better response
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
6.20 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Time Quantum and Context Switch Time
11
6.21 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Turnaround Time Varies With The Time Quantum
6.22 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Multilevel Queue
n Ready queue is partitioned into separate queues:foreground (interactive)background (batch)
n Each queue has its own scheduling algorithm
l foreground – RR
l background – FCFS
n Scheduling must be done between the queuesl Fixed priority scheduling; (i.e., serve all from foreground then from
background). Possibility of starvation.
l 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
l 20% to background in FCFS
12
6.23 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Multilevel Queue Scheduling
6.24 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Multilevel Feedback Queue
n A process can move between the various queues; aging can be implemented this way
n Multilevel-feedback-queue scheduler defined by the following parameters:
l number of queues
l scheduling algorithms for each queue
l method used to determine when to upgrade a process
l method used to determine when to demote a process
l method used to determine which queue a process will enter when that process needs service
13
6.25 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Example of Multilevel Feedback Queue
n Three queues:
l Q0 – time quantum 8 milliseconds
l Q1 – time quantum 16 milliseconds
l Q2 – FCFS
n Schedulingl A new job enters queue Q0 which is served FCFS. When it gains
CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.
l At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.
6.26 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Multilevel Feedback Queues
14
6.27 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Multiple-Processor Scheduling
n CPU scheduling more complex when multiple CPUs are available
n Homogeneous processors within a multiprocessor
n Load sharing
n Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing
6.28 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Real-Time Scheduling
n Hard real-time systems – required to complete a critical task within a guaranteed amount of time
n Soft real-time computing – requires that critical processes receive priority over less fortunate ones
15
6.29 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Dispatch Latency
6.30 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Algorithm Evaluation
n Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for thatworkload
n Queueing models
n Implementation
16
6.31 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Single Queue
customerswaiting line
W S
T
T = W + S
© 2004 D.A. Menascé
6.32 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Example of An Analytic ModelM/G/1 Queue
n Single server.
n Arrival process is Poisson (interarrival times are exponentially distributed).
n Service time is arbitrarily distributed.
)1(2)1]([
][)1(2][
][22
ρρ
ρλ
−+
+=−
+= sCSESE
SESET
Where
1][ <= SEλρ
© 2004 D.A. Menascé
(utilization)
Average CPU time
Second moment of the CPU time
17
6.33 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Little’s Law
NX
RThe average number of customers in a “black box” is equal tothe average time spent in the box multiplied by the throughputof the box.
N = R x X© 2004 D.A. Menascé
6.34 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
M/G/1 with Prioritiesn P static priorities (p= 1, …, P).
n P is the highest priority.n FCFS within each priority queue.
1λ
2λ
Pλ
© 2004 D.A. Menascé
18
6.35 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
M/G/1 with Non-Preemptive Priorities
∑
∑ ∑
∑
=
= =
=
+
=Π
==
=
Π−Π−=
P
piip
P
p
P
pppp
P
pip
ppp
SE
SEW
WW
ρ
ρλρ
λ
1 1
1
20
1
0
][
][21
)1)(1(
© 2004 D.A. Menascé
6.36 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
0
2
4
6
8
10
12
14
0.00 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00
Total Utilization
Ave
rag
e W
aiti
ng
Tim
e
W1 W2 W3
© 2004 D.A. Menascé
M/G/1 with Non-Preemptive Priorities
19
6.37 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
M/G/1 with Preemptive Resume Priorities
∑
∑
=
+
=
=Π
Π−Π−
+Π−=
P
piip
pp
P
piiipp
p
SESET
ρ
λ
)1)(1(
2/][)1]([
1
2
© 2004 D.A. Menascé
6.38 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Comparing Preemptive vs. Non-Preemptive M/G/1 Queues
a b c d e f g=b*fNon-
preemptive Preemptive
Priority Lambda E[S] ρ Π E[S2] T T1 1 0.50 0.500 0.775 0.375 0.3750 2.103 2.2932 0.5 0.40 0.200 0.275 0.240 0.1200 0.790 0.5433 0.3 0.25 0.075 0.075 0.094 0.0281 0.533 0.265
© 2004 D.A. Menascé
20
6.39 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Evaluation of CPU Schedulers by Simulation
6.40 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Solaris 2 Scheduling
21
6.41 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Windows XP Priorities
Priority ClassesR
elat
ive
Prio
rity
6.42 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Linux Scheduling
n Two algorithms: time-sharing and real-time
n Time-sharing
l Prioritized credit-based – process with most credits is scheduled next
l Credit subtracted when timer interrupt occurs
l When credit = 0, another process chosen
l When all processes have credit = 0, recrediting occurs
4Based on factors including priority and history
n Real-timel Soft real-time
l Posix.1b compliant – two classes
4FCFS and RR
4Highest priority process always runs first
22
6.43 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Thread Scheduling
n Local Scheduling – How the threads library decides which thread to put onto an available LWP
n Global Scheduling – How the kernel decides which kernel thread to run next
6.44 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Pthread Scheduling API
#include <pthread.h>#include <stdio.h>#define NUM THREADS 5int main(int argc, char *argv[]){
int i;pthread t tid[NUM THREADS];pthread attr t attr;/* get the default attributes */pthread attr init(&attr);/* set the scheduling algorithm to PROCESS or SYSTEM */pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM);/* set the scheduling policy - FIFO, RT, or OTHER */pthread attr setschedpolicy(&attr, SCHED OTHER);/* create the threads */for (i = 0; i < NUM THREADS; i++)
pthread create(&tid[i],&attr,runner,NULL);
23
6.45 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Pthread Scheduling API
/* now join on each thread */
for (i = 0; i < NUM THREADS; i++)
pthread join(tid[i], NULL);
}
/* Each thread will begin control in this function */
void *runner(void *param )
{
printf("I am a thread\n");
pthread exit(0);
}
6.46 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Java Thread Scheduling
n JVM Uses a Preemptive, Priority-Based Scheduling Algorithm
n FIFO Queue is Used if There Are Multiple Threads With the Same Priority
24
6.47 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Java Thread Scheduling (cont)
JVM Schedules a Thread to Run When:
1. The Currently Running Thread Exits the Runnable State
2. A Higher Priority Thread Enters the Runnable State
* Note – the JVM Does Not Specify Whether Threads are Time-Sliced or Not
6.48 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Time-Slicing
Since the JVM Doesn’t Ensure Time-Slicing, the yield() Method
May Be Used:
while (true) {
// perform CPU-intensive task
. . .
Thread.yield();
}
This Yields Control to Another Thread of Equal Priority
25
6.49 Silberschatz, Galvin and Gagne ©2003Operating System Concepts with Java
Thread Priorities
Priority Comment
Thread.MIN_PRIORITY Minimum Thread Priority
Thread.MAX_PRIORITY Maximum Thread Priority
Thread.NORM_PRIORITY Default Thread Priority
Priorities May Be Set Using setPriority() method:
setPriority(Thread.NORM_PRIORITY + 2);