Unit-II: CPU Scheduling
Unit-II: CPU Scheduling
Unit-II: CPU Scheduling
CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling
5.2
Scheduling Criteria
CPU utilization keep the CPU as busy as possible Throughput Number of processes that complete their
process
Waiting time amount of time a process has been waiting
request was submitted until the first response is produced, not output (for time-sharing environment)
5.3
Optimization Criteria
Max CPU utilization
Max throughput
Min turnaround time Min waiting time Min response time
5.4
Scheduling
Preemptive A Process which can be terminated. Non-Preemptive A Process which cannot terminated. Algorithms:
preemptive
P2 27
P3 30
5.6
Turn around time for P1 = 24; P2 = 27; P3 = 30 Average Turn around time: (24 + 27+30)/3 = 81/3 = 27ms
5.7
P2 , P3 , P1
The Gantt chart for the schedule is:
P2 0 3
P3 6
P1 30
5.8
Turn around time for P1 = 30; P2 = 3; P3 = 6 Average Turn around time: (30 + 3+6)/3 = 39/3 = 13ms
5.9
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)
set of processes
5.10
P1
P2 P3 P4
SJF (non-preemptive)
6
8 7 3
P4 0 3
P1 9
P3 16
P2 24
5.11
P4
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
5.12
P1
P2 P3 P4
SJF (preemptive)
0.0
2.0 4.0 5.0
7
4 1 4
2 P1 0 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.
5.13
P1
P2 P3 P4
SJF (preemptive)
0.0
2.0 4.0 5.0
7
4 1 4
2 P1 0 2
2 P2
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.
Operating System Concepts 7th Edition, Feb 2, 2005 5.14 Silberschatz, Galvin and Gagne 2005
P1
P2 P3 P4
SJF (preemptive)
0.0
2.0 4.0 5.0
7
4 1 4
2 P1 0 2
2 P2 4
1 P3
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
Operating System Concepts 7th Edition, Feb 2, 2005 5.15 Silberschatz, Galvin and Gagne 2005
P1
P2 P3 P4
SJF (preemptive)
0.0
2.0 4.0 5.0
7
4 1 4
2 P1 0 2
2 P2 4
1 P3 5
2 P2
5.16
P1 P2 P3 P4
SJF (preemptive)
7 4 1 4
2 P1 0 2
2 P2 4
1 P3 5
2 P2 7
4 P4
5 P1
11
5.17
Burst Time
7 4 1
WT
9 3 0
TT
16 7 1
p4
Total
2
14
6
30
2
P1 0 2
2 P2 4
1
P3 5
2 P2
4 P4
5 P1
11
5.18
Priority Scheduling
A priority number (integer) is associated with each process
Preemptive nonpreemptive
burst time
Problem Starvation low priority processes may never execute Solution Aging as time progresses increase the priority of the
process
5.19
Priority Scheduling
Problem Starvation low priority processes may never execute
process. P1 100 P2 2 P3 1
5.20
Priority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1 4 T0 P1 P2 2 P3
10
5.21
Priority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1 4 T0 P1 P2 2 P3
10
2
4
1
2 P3 1
T1
P1 9
P2 1
5.22
Priority Scheduling
After 2ms of time period increase the priority of all the process in ready queue by 1 4 T1 P1 P2 1 2 P3
RT=2 T2 P1 8 P2 1
2 P3 1
5.23
P1
P2 P3 P4
10
1 2 1
3
1 4 5
P5
Priority (non-preemptive)
P2 0 1
P5 6
P1 16
P3
P4
18 19
5.24
P1
P2 P3 P4
priority (preemptive)
0.0
2.0 4.0 5.0
7
4 1 4
1
0.5 4 3
2 P1 0
5.25
P1
P2 P3 P4 priority (preemptive) 2 P1 0 2 4 P2
0.0
2.0 4.0 5.0
7
4 1 4
1
0.5 4 3
5.26
P1
P2 P3 P4 priority (preemptive) 2 P1 0 2 4 P2 6
0.0
2.0 4.0 5.0
7
4 1 4
1
0.5 4 3
5 P1
5.27
P1
P2 P3 P4 priority (preemptive) 2 P1 0 2 4 P2 6
0.0
2.0 4.0 5.0
7
4 1 4
1
0.5 4 3
5 P1 11
4 P4 15
1 P3
P1 = 6 2 = 4ms P2 = 2 2 = 0ms
P3 = 15 4 = 11ms
Operating System Concepts 7th Edition, Feb 2, 2005 5.28 Silberschatz, Galvin and Gagne 2005
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.29
Burst Time 8 3 3
5.30
Burst Time 8 3 3
5.31
Burst Time 8 3 3
5.32
Burst Time 8 3 3
P1 = 10 4 = 6 8+6 = 14 P2 = 4 3+4 = 7
P3 = 7 3+7 = 10 Avg TT = 31/3 = 10.33ms Typically, higher average turnaround than SJF, but better response
Operating System Concepts 7th Edition, Feb 2, 2005 5.33 Silberschatz, Galvin and Gagne 2005
P1 0 20
P2 37
P3 57
P4 77
P1
P3 97 117
P4
P1
P3
P3
5.34
What will happen to RR scheduling if the Time Quantum is high when compared to all the process in the ready queue?
Operating System Concepts 7th Edition, Feb 2, 2005 5.35 Silberschatz, Galvin and Gagne 2005
Exercise
Process P1 P2 P3 P4 P5 Burst Time 10 1 2 1 5 Priority 3 1 3 4 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
preemptive
priority
and
b) What is the turnaround time & waiting Time of each process for
P3
P4
7
3
2
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.
Operating System Concepts 7th Edition, Feb 2, 2005 5.37 Silberschatz, Galvin and Gagne 2005
Problem 1: Solution:
FCFS :
SJFS :
5.38
(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.
Operating System Concepts 7th Edition, Feb 2, 2005 5.39 Silberschatz, Galvin and Gagne 2005
Problem 2: Solution:
5.40
Multilevel Queue
Ready queue is partitioned into separate queues:
foreground RR background FCFS 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.41
5.42
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.43
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.44
5.45
Multiple-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.46
Real-Time Scheduling
Hard real-time systems required to complete a
5.47
Thread Scheduling
Local Scheduling How the threads library decides which
5.48
5.49
5.50
5.51
Solaris 2 Scheduling
5.52
5.53
Windows XP Priorities
5.54
Linux 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.55
5.56
5.57
Algorithm Evaluation
Deterministic modeling takes a particular
predetermined workload and defines the performance of each algorithm for that workload
Queueing models Implementation
5.58
5.15
5.59
End of Chapter 5
5.08
5.61
In-5.7
5.62
In-5.8
5.63
In-5.9
5.64
Dispatch Latency
5.65
FIFO Queue is Used if There Are Multiple Threads With the Same
Priority
5.66
The Currently Running Thread Exits the Runnable State A Higher Priority Thread Enters the Runnable State
* Note the JVM Does Not Specify Whether Threads are Time-Sliced or Not
5.67
Time-Slicing
Since the JVM Doesnt 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.68
Thread Priorities
Comment Minimum Thread Priority Maximum Thread Priority Default Thread Priority
5.69