1.CPU Scheduling
-
Upload
bhavesh-bihani -
Category
Documents
-
view
51 -
download
0
description
Transcript of 1.CPU Scheduling
Unit-II: CPU SchedulingUnit-II: CPU Scheduling
5.2 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
CPU SchedulingCPU Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Multiple-Processor Scheduling
Real-Time Scheduling
5.3 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Scheduling CriteriaScheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – Number 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,
not output (for time-sharing environment)
5.4 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Optimization CriteriaOptimization Criteria
Max CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
5.5 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
SchedulingScheduling
Preemptive – A Process which can be terminated.
Non-Preemptive – A Process which cannot terminated.
Algorithms:
First Come First Serve(FCFS) – (Non- Preemptive)
Shortest Job first(SJFS) – Preemptive and Non-preemptive
Priority - Preemptive and Non-preemptive
Round Robin – Preemptive FCFS
5.6 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
First-Come, First-Served (FCFS) SchedulingFirst-Come, First-Served (FCFS) Scheduling
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:
Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 300
5.7 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Process WT BT TT
P1 0 24 24
P2 24 3 27
P3 27 3 30
81
First-Come, First-Served (FCFS) SchedulingFirst-Come, First-Served (FCFS) Scheduling
Turn around time for P1 = 24; P2 = 27; P3 = 30 Average Turn around time: (24 + 27+30)/3 = 81/3 = 27ms
5.8 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
FCFS Scheduling (Cont.)FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1
The Gantt chart for the schedule is:
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
P1P3P2
63 300
5.9 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Process WT BT TT
P1 6 24 30
P2 0 3 3
P3 3 3 6
39
First-Come, First-Served (FCFS) SchedulingFirst-Come, First-Served (FCFS) Scheduling
Turn around time for P1 = 30; P2 = 3; P3 = 6 Average Turn around time: (30 + 3+6)/3 = 39/3 = 13ms
5.10 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Shortest-Job-First (SJF) SchedulingShortest-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:
nonpreemptive – 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
5.11 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Process Burst Time
P1 6
P2 8
P3 7
P4 3
SJF (non-preemptive)
Average waiting time = (3 + 16 + 9 + 0)/4 = 7Ms
Example of Non-Preemptive SJFExample of Non-Preemptive SJF
P4 P1 P3
3 240
P2
9 16
5.12 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Process WT BT TT
P1 3 6 9
P2 16 8 24
P3 9 7 16
P4 0 3 3
52
Turn around time for P1 = 9; P2 = 24; P3 = 16 ; p4 = 3 Average Turn around time: (9 + 24 +16 + 3)/4 = 52/4 = 13ms
Example of Non-Preemptive SJFExample of Non-Preemptive SJF
5.13 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive SJFExample of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)
After 2ms of Time period p2 enters
Compare the RT of p1= 5ms and BT of entering process p2 = 4ms.
BT of p2 is less than the RT of p1 so p1 is preempted.
P1
0
2
5.14 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive SJFExample of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)
After 4ms of Time period p2 has executed for 2ms. The RT of p2 = 2ms has to be compared with RT of p1= 5ms and BT of entering process p3 = 1ms. Since p3 BT is minimum compared to p1 and p2, p3 starts executing.
P1 P2
0 2
2 2
5.15 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive SJFExample of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)
P3 executes for 1ms i.e after 5ms of Time p4 enters by that time p3 would have completely been executed. Now we have p1,p2 & p4 in the ready queue with corresponding BT p1=5ms, p2=2ms & p4 =4ms.Now apply non-preemptive concept to execute the processes. P2 p4 p1
P1 P2 P3
0 2 4
2 2 1
5.16 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive SJFExample of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)
P1 P2 P3 P2
0 2 4 5
2 2 1 2
5.17 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive SJFSExample of Preemptive SJFS
Process Arrival Time Burst Time
P1 0.0 7 P2 2.0 4 P3 4.0 1 P4
5.0 4
SJF (preemptive)
AWT = ( (11-2) + (5-(2+2)) + (4-4) + (7-5) ) = 12/4 = 3 Ms
P1 P2 P3 P2
0 2 4 5
2 2 1 2
P4
7
P1
4
11
5
5.18 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive SJFSExample of Preemptive SJFS
AWT = ( (11-2) + (5-(2+2)) + (4-4) + (7-5) ) = 12/4 = 3 Ms
ATT = 30/4 = 7.5ms
P1 P2 P3 P2
0 2 4 5
2 2 1 2
P4
7
P1
4
11
5
Process Burst Time WT TT
P1 7 9 16
P2 4 3 7
P3 1 0 1
p4 4 2 6
Total 14 30
5.19 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Priority SchedulingPriority Scheduling
A priority number (integer) is associated with each process
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
5.20 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Priority SchedulingPriority Scheduling
Problem Starvation – low priority processes may never execute
Solution Aging – as time progresses increase the priority of the process.
P1 P2 P3
12100
5.21 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Priority SchedulingPriority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1
T0 P1 P2 P3
1210
24
5.22 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Priority SchedulingPriority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1
T0
T1
P1 P2 P3
1210
24
P1 P2 P3
24
119
5.23 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Priority SchedulingPriority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1
T1
T2
P1 P2 P3
119
24
P1 P2 P3
118
2RT=2
5.24 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority (non-preemptive)
Average waiting time = (6 + 0 + 16 + 18 + 1) / 5 = 41 / 5 = 8.2Ms
Example of Non-Preemptive Priority Example of Non-Preemptive Priority SchedulingScheduling
P2 P5 P1
1 190
P4
6 16
P3
18
5.25 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive PriorityExample of Preemptive Priority
Process Arrival Time Burst TimePriority
P1 0.0 7 1
P2 2.0 4 0.5
P3 4.0 1 4
P4 5.0 4 3
priority (preemptive)
P1
0
2
5.26 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive PriorityExample of Preemptive Priority
Process Arrival Time Burst TimePriority
P1 0.0 7 1
P2 2.0 4 0.5
P3 4.0 1 4
P4 5.0 4 3 priority (preemptive)
P1 P2
0 2
2 4
5.27 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive PriorityExample of Preemptive Priority
Process Arrival Time Burst TimePriority
P1 0.0 7 1
P2 2.0 4 0.5
P3 4.0 1 4
P4 5.0 4 3 priority (preemptive)
P1 P2
0 2
2 4
P1
5
6
5.28 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Preemptive PriorityExample of Preemptive Priority
Process Arrival Time Burst Time Priority
P1 0.0 7 1
P2 2.0 4 0.5
P3 4.0 1 4
P4 5.0 4 3 priority (preemptive)
P1 = 6 – 2 = 4msp4 = 11 – 5 = 6ms
P2 = 2 – 2 = 0ms Avg WT = 21/4 = 5.2ms
P3 = 15 – 4 = 11ms
P1 P2
0 2
2 4
P1
5
6
P4
4
11
P3
1
15
5.29 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Round Robin (RR)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
5.30 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of RR with Time Quantum = 4Example of RR with Time Quantum = 4
Process Burst Time
P1 8
P2 3
P3 3
The Gantt chart is:
P1 is preempted and p2 starts its execution. Pending BT of p1 = 4ms
P1
0
4
5.31 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of RR with Time Quantum = 4Example of RR with Time Quantum = 4
Process Burst Time
P1 8
P2 3
P3 3
The Gantt chart is:
P2 is completely executed since its BT is less than the Time Quantum.
P1 P2
0 4
4 3
5.32 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of RR with Time Quantum = 4Example of RR with Time Quantum = 4
Process Burst Time
P1 8
P2 3
P3 3
The Gantt chart is:
P1 P2 P3
0 4 7
4 3 3
5.33 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of RR with Time Quantum = 4Example of RR with Time Quantum = 4
Process Burst Time
P1 8
P2 3
P3 3
The Gantt chart is:
WT TT
P1 = 10 – 4 = 6 8+6 = 14
P2 = 4 3+4 = 7 Avg WT = 17/3 = 5.66ms
P3 = 7 3+7 = 10 Avg TT = 31/3 = 10.33ms
Typically, higher average turnaround than SJF, but better response
P1 P2 P3 P1
0 4 7 10
4 3 3 4
5.34 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of RR with Time Quantum = 20Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24 The Gantt chart is:
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
5.35 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Time Quantum and Context Switch TimeTime Quantum and Context Switch Time
What will happen to RR scheduling if the What will happen to RR scheduling if the Time Quantum is high when compared to Time Quantum is high when compared to
all the process in the ready queue?all the process in the ready queue?
5.36 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2
The Processes are assumed to have arrived in the order p1,p2,p3,p4,p5 all at time 0ms.
a) Draw 4 Gantt charts illustrating the execution of these processes using FCFS, SJF, a non preemptive priority and RR(Quantum=1ms) scheduling.
b) What is the turnaround time & waiting Time of each process for each of the scheduling algorithms in part (a)?
c) Which of the schedules in part (a) results in the minimal average waiting time(overall process)?
ExerciseExercise
5.37 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Problem – 1:
Process Burst Time Priority
P1 6 3
P2 8 1
P3 7 2
P4 3 4
(i) For the above processes apply non-preemptive FCFS, SJFS and Priority scheduling and find the waiting Time and Turnaround Time for each process.
(ii) Calculate the Average Waiting Time and Average Turnaround Time for the above 3 algorithms and compare the AWT and ATT and discuss the effectiveness of the algorithms based on the AWT and ATT.
5.38 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Problem – 1:
Solution:
FCFS : AWT = 41/4 = 10.2ms
ATT = 65/4 = 16.2ms
SJFS : AWT = 28/4 = 7ms
ATT = 52/4 = 13ms
Priority : AWT = 54 /4 = 13.5ms
ATT = 78/4 = 19.5ms
5.39 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Problem – 2:
Process Arrival Time Burst Time Priority
P1 0 8 3
P2 1 4 4
P3 2 9 1
P4 3 5 2
(i) For the above processes apply preemptive SJFS , and Priority. find the waiting Time and Turnaround Time for each process.
(ii) Calculate the Average Waiting Time and Average Turnaround Time for the above 2 algorithms and compare the AWT and ATT and discuss the effectiveness of the algorithms based on the AWT and ATT.
5.40 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Problem – 2:
Solution:
SJFS : AWT = 26/4 = 6.5ms
ATT = 52/4 = 13ms
Priority : AWT = 43/4 = 10.75ms
ATT = 69/4 = 17.25ms
5.41 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Multilevel QueueMultilevel 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
5.42 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Multilevel Queue SchedulingMultilevel Queue Scheduling
5.43 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Multilevel Feedback QueueMultilevel 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
5.44 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Example of Multilevel Feedback QueueExample of Multilevel Feedback Queue
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
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.
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.
5.45 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Multilevel Feedback QueuesMultilevel Feedback Queues
5.46 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Multiple-Processor SchedulingMultiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are available
Homogeneous processors within a multiprocessor
Load sharing
Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing
5.47 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Real-Time SchedulingReal-Time Scheduling
Hard real-time systems – required to complete a critical task within a guaranteed amount of time
Soft real-time computing – requires that critical processes receive priority over less fortunate ones
5.48 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Thread SchedulingThread Scheduling
Local Scheduling – How the threads library decides which thread to put onto an available LWP
Global Scheduling – How the kernel decides which kernel thread to run next
5.49 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Pthread Scheduling APIPthread 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);
5.50 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Pthread Scheduling APIPthread 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);
}
5.51 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Operating System ExamplesOperating System Examples
Solaris scheduling
Windows XP scheduling
Linux scheduling
5.52 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Solaris 2 SchedulingSolaris 2 Scheduling
5.53 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Solaris Dispatch Table Solaris Dispatch Table
5.54 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Windows XP PrioritiesWindows XP Priorities
5.55 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Linux SchedulingLinux Scheduling
Two algorithms: time-sharing and real-time Time-sharing
Prioritized credit-based – process with most credits is scheduled next
Credit subtracted when timer interrupt occurs When credit = 0, another process chosen When all processes have credit = 0, recrediting occurs
Based on factors including priority and history Real-time
Soft real-time Posix.1b compliant – two classes
FCFS and RR Highest priority process always runs first
5.56 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
The Relationship Between Priorities and Time-slice lengthThe Relationship Between Priorities and Time-slice length
5.57 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
List of Tasks Indexed According to ProritiesList of Tasks Indexed According to Prorities
5.58 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Algorithm EvaluationAlgorithm Evaluation
Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload
Queueing models
Implementation
5.59 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
5.155.15
End of Chapter 5End of Chapter 5
5.61 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
5.085.08
5.62 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
In-5.7In-5.7
5.63 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
In-5.8In-5.8
5.64 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
In-5.9In-5.9
5.65 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Dispatch LatencyDispatch Latency
5.66 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Java Thread SchedulingJava Thread Scheduling
JVM Uses a Preemptive, Priority-Based Scheduling Algorithm
FIFO Queue is Used if There Are Multiple Threads With the Same Priority
5.67 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Java Thread Scheduling (cont)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
5.68 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Time-SlicingTime-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
5.69 Silberschatz, Galvin and Gagne ©2005Operating System Concepts – 7th Edition, Feb 2, 2005
Thread PrioritiesThread 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);