Download presentation
Presentation is loading. Please wait.
Published byCoral Snow Modified over 6 years ago
1
CPU Scheduling Optimization of CPU scheduling implies
Number of jobs executed per unit time should be maximum. The term optimization signifies CPU Burst cycles Input Output Burst cycles. CPU burst cycles is the amount of time it takes to execute few instructions before it goes for I/O
2
CPU Scheduler Selects from any of the processes in memory that are ready to execute and allocates the CPU to one of them. CPU scheduling decisions takes place when a process: Switches from running to waiting state Switches from running to ready state Switches from waiting to ready state Terminates Scheduling 1 &4 are non preemptive All other scheduling are preemptive CCPU I/O Device Queue terminate Non Preemptive Preemptive
3
When to schedule? A new process starts The running process exits
The running process is blocked I/O interrupt (some processes will be ready) Clock interrupt (every 10 milliseconds)
4
Preemptive vs. Non-preemptive scheduling
The running process keeps the CPU until it voluntarily gives up the CPU process exits switches to blocked state 1 and 4 only (no 3) Preemptive scheduling: The running process can be interrupted and must release the CPU (can be forced to give up CPU) Running Terminated Ready Blocked 1 4 3
5
Scheduling Objectives
Fairness (equitable shares of CPU) Priority (most important first) Efficiency (make best use of equipment) Encouraging good behavior (can’t take advantage of the system) Support for heavy loads (degrade gracefully) Adapting to different environments (interactive, real-time, multi-media)
6
Scheduling Criteria CPU utilization: Percentage of the time that CPU is busy. [(Processor busy time) / (Processor busy time + Processor idle time) ] Throughput: the number of processes completed per unit time [(No. of processes completed) / (Time unit) ] Turnaround time (elapsed time) : The interval from the time of submission of a process to the time of completion. [t(Process Completed) –t(Process Submitted)] Wait time: The sum of the periods spent waiting in the ready queue [Turn around time – Processing time ] Response time: The time of submission to the time the first response is produced
7
Graphical Reresentation of Scheduling Criteria example
Suppose we have processes A, B, and C, submitted at time 0 We want to know the response time, waiting time, and turnaround time of process A turnaround time + wait time response time = 0 A B C A B C A C A C Time Gantt chart: visualize how processes execute.
8
Graphical Reresentation of Scheduling Criteria example
Suppose we have processes A, B, and C, submitted at time 0 We want to know the response time, waiting time, and turnaround time of process B turnaround time + wait time response time A B C A B C A C A C Time
9
Graphical Reresentation of Scheduling Criteria example
Suppose we have processes A, B, and C, submitted at time 0 We want to know the response time, waiting time, and turnaround time of process C turnaround time + wait time response time A B C A B C A C A C Time
10
Goal of CPU scheduling Other performance metrics: Goal:
fairness: It is important, but harder to define quantitatively. Goal: Maximize CPU utilization, Throughput, Fairness. Minimize Turnaround time Waiting time, Response time.
11
Scheduling Policies Process that requests the CPU FIRST and allocated. Also called FIFO FIFO (first in, first out) SJF (shortest job first) Round robin Priority Scheduling Multilevel feedback queues Lottery scheduling
12
FIFO FIFO: assigns the CPU based on the order of requests
Non preemptive: A process keeps running on a CPU until it is blocked or terminated Also known as FCFS (first come, first serve) + Simple Short jobs can get stuck behind long jobs(convoy effect) Turnaround time is not ideal (get an example from the class)
13
FCFS Example Process Duration Order Arrival Time P1 24 1 P2 3 2 P3 4
P2 3 2 P3 4 The final schedule: P2 (3) P3 (4) P1 (24) 24 27 31 P1 waiting time: 0 P2 waiting time: 24 P3 waiting time: 27 The average waiting time: ( )/3 = 17
14
Problems with FCFS Non-preemptive Not optimal AWT
Cannot utilize resources in parallel: Assume 1 process CPU bounded and many I/O bounded processes result: Convoy effect, low CPU and I/O Device utilization
15
Example of FCFS Average waiting Time: [(0-0)+(8-0.4)+(12-1)] /3=6.2
Process Arrival Time Burst time P1 0.0 8 P2 0.4 4 P3 1.0 1 Find the average wating time for these processes with FCFS scheduling P1 8 P2 12 P3 13 Average waiting Time: [(0-0)+(8-0.4)+(12-1)] /3=6.2
16
FCFS Example Calculate the waiting time, average waiting time, throughput and processor utilization for the given set of processes that arrive at a given arrive time shown in the table, with the length of processing time given in milliseconds: Process Arrival Time Processing Time P 1 3 P2 2 P3 1 P4 5 4 P5 8
17
FCFS Example(Cont) P1 (3) 11 13 3 6 P2 (3) P4 (4) 7 P3 (1) P5 (2)
If the processes arrive as per the arrival time, the Gantt chart will be P1 (3) 11 13 3 6 P2 (3) P4 (4) 7 P3 (1) P5 (2) Time Process Completed Turnaround time= [t(Process Completed) –t(Process Submitted)] Waiting Time= [Turn around time – Processing time ] 3 P1 3-0=3 3-3=0 6 P2 6-2=4 4-3=1 7 P3 7-3=4 4-1=3 11 P4 11-5=6 6-4=2 13 P5 13-8=5 5-2=3
18
FCFS Example(Cont) Average waiting time = ( ) / 5 = [9/5]=1.8 Processor utilization = (13/13)*100 = 100% Throughput = 5/13 = 0.38
19
SJF Variants Two Schemes:
i) Non-Preemptive -- Once CPU is given to the job, it cannot be preempted until it completes its CPU burst. ii) Preemptive -- If a new job arrives with CPU burst length shorter than remaining time of the current executing job -- preempt. (Shortest remaining time first or SRTF)
20
Shortest-Job-First (SJF) Scheduling
For each process, identify duration (i.e., length) of its next CPU burst. Use these lengths to schedule process with shortest burst Two schemes:– Non-preemptive – once CPU given to the process, it is not 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. This scheme is known as the Shortest-Remaining-Time-First (SRTF)
21
Shortest-Job-First (SJF) Scheduling (cont.)
SJF probably optimal – gives minimum average waiting time for a given set of process bursts Moving a short burst ahead of a long one reduces wait time of short process more than it lengthens wait time of long one. Optimal if all jobs are available simultaneously: Gives the best possible AWT (average waiting time)
22
Shortest Job First 1 2 10 A B C Process A B C Burst Time 8 1
1 2 10 A B C Avg Wait Time ( ) / 3 = 1
23
Non-preemptive SJF: Example
Process Duration Order Arrival Time P1 6 1 P2 8 2 P3 7 3 P4 4 3 P4 (3) P1 (6) 9 P3 (7) 16 P2 (8) 24 P4 waiting time: 0 P1 waiting time: 3 P3 waiting time: 9 P2 waiting time: 16 The total time is: 24 The average waiting time (AWT): ( )/4 = 7
24
Shortest-Job First (SJF)
This algorithm is assigned to the process that has smallest next CPU processing time, In case of a tie, FCFS scheduling algorithm can be used As an example, consider the following set of processes with the following processing time which arrived at the same time. Process Processing Time P1 06 P2 08 P3 07 P4 03 Using SJF scheduling the shortest length of process will first get executed
25
Shortest-Job First (SJF)
Gantt chart will be P4 P1 P3 P2 16 24 3 9 Because the shortest processing time is of the process P4, then process P1 and then P3 and Process P2. The waiting time for process P1 is 3 ms, for process P2 is 16 ms, for process P3 is 9 ms and for the process P4 is 0 ms as – Time Process Completed Turn around Time = t (Process Completed)– t (Process Submitted) Waiting Time = Turn around time – Processing time 3 P4 3 – 0 = 3 3 – 3 = 0 9 P1 9 – 0 = 9 9 – 6 = 3 16 P3 16 – 0 = 16 16 – 7 = 9 24 P2 24 – 0= 24 24 – 8 = 16
26
Example(Non Preemptive SJF)
Consider a set of four processes P1,P2,P3, and p4 Process CPU Time CT=TA WT P1 5 8 8-5=3 P2 10 26 26-10=16 P3 16 16-8=8 P4 3 3-3-0 P4 P3 P1 8 3 16 26 P2 Gantt Chart AWT:-( )/4=27/4=6.75
27
Example of Non-Preemptive SJF
Process AT BT CT TAT WT(TA-BT) P =0 P =6 P =3 P =7 SJF (non-preemptive) Average waiting time = [ )]/4 = 4 P1 P3 P2 7 16 P4 8 12
28
Example of Preemptive SJF
Process Arrival Time Burst Time P P P P SJF (preemptive) Average waiting time =12/4 = 3 5 2 _ - P1 P3 P2 4 2 11 P4 5 7 16
29
Example of SRTF Consider the set of processes, P1 to P5 with the following CPU burst times and find the TAT and relative time delay STEP `1: Process P1 arrives at t=0 Therefore P1 executes At time t=2, P2 arrives Therefore remaining time of P1=3-2=1 unit and Burst time of P2 =6 unit Therefore 1<6 , therefore P1 will continue Process CPU Burst Time Arrival time P1 3 P2 6 2 P3 4 P4 5 P5 8 STEP `2: Process P3 arrives at t=4 Therefore P2 executes for (4-3)=1 unit Now Burst time of P3=4 and Remaining time of P2=6-1=5 unit ,Therefore 4<5 , therefore P3 continues STEP `3:Process P5 arrives at t=8 Now P5 executes for 2 units STEP `4:Then P2 executes for remaining 5 units and then P4 for 5 units
30
Example of SRTF ATAT:- (3+13+4+14+2)/5=36/5=7.2 ms
Process CPU Burst Time Arrival time P1 3 P2 6 2 P3 4 P4 5 P5 8 CT Turnaround time Relative delay =[TAT/service time] 3 3-0=3 3/3=1 15 15-2=13 13/6=2.17 8 8-4=4 4/4=1 20 20-6=14 14/5=2.80 10 10-8=2 2/2=1 Gant t Chart P1 P2 P3 P5 P4 3 4 8 10 15 20 ATAT:- ( )/5=36/5=7.2 ms Average relative delay =7.97/5=1.59 ms
31
Example of SRTF(Shortest BT)
CT TA=(ct-at) WT=(TA-BT) 19 12 13 7 6 4 1 9 5 3 2 PNO AT BT P1 7 6 P2 1 5 4 P3 2 3 2 1 P4 3 P5 4 P6 5 Gantt Chart 1 2 3 4 5 6 7 9 13 19 P1 P2 P3 P4 P6 P5
32
Priority Scheduling 1 9 10 A B C SJF is a special case Process A B C
Burst Time 8 1 Priority 2 1 3 1 9 10 A B C Avg Wait Time ( ) / 3 = 3.3
33
Priority Scheduling(Non Preemptive)
PNO Priority AT BT P1 2(L) 4 P2 1 2 P3 6 3 P4 10 5 P5 8 P6 12(H) P7 9 CT TA WT RT=wt 4 25 24 22 23 21 18 9 6 1 20 16 15 13 8 19 7 Gantt Chart 4 9 13 19 20 23 25 P1 P4 P6 P7 P5 P3 P2
34
Scheduling Algorithms
Round Robin (RR) FCFS + preemptive scheduling. Ready queue is treated as a circular queue. Each process gets the CPU for a time quantum (or time slice), typically ms. A process runs until it blocks, terminates, or uses up its time slice.
35
Round Robin Round Robin (RR) periodically releases the CPU from long-running jobs Based on timer interrupts so short jobs can get a fair share of CPU time Preemptive: a process can be forced to leave its running state and replaced by another running process Time slice: interval between timer interrupts
36
More on Round Robin If time slice is too long
Scheduling degrades to FIFO If time slice is too short Throughput suffers Context switching cost dominates
37
Round Robin(RR)[Preemptive]
TQ=1 (Max allowable time) Process BT P1 5 4, 3,2,1,0 P2 3 2, 1,0 P3 2 1,1,0 CT TAT WT 10 10-0=10 10-5=5 8 8-0=8 8-3=5 6 6-0=6 6-2=4 Gantt Chart 1 2 3 4 5 P1 P2 P3 6 7 8 9 10 Avg Turn Around Time ( ) / 3 = 8 Avg Wait Time ( ) / 3 = 4.6
38
Round Robin(RR)[Preemptive]
TQ=1 (Max allowable time) Process AT BT P1 2 P2 1 5 P3 P4 8 CT TAT WT 4 4-0=4 4-2=2 16 16-1=15 15-5=10 5 5-1=4 4-1=3 11 11-2=9 9-8=1 RQ P1 P2 P3 P4 Gantt Chart 1 2 3 4 5 P1 P2 P3 P4 6 7 8 9 10 11 12 16
39
Example of Round Robin (q=8)
Process P1 P2 P3 P4 P5 P6 P7 Burst Time 10 6 23 9 31 3 19 Wait Times 41 8 60 51 70 38 75 2 41 8 15 7 14 29 17 1 22 29 23 15 7 30 22 15 3 38 11 3 41 19 15 P1 8 P2 6 P3 8 P4 8 P5 8 P6 3 P7 8 P1 2 P3 8 P4 1 P5 8 P7 8 P3 7 P5 8 P7 3 343 / 7 = 49.00 P5 7
40
Round Robin(RR)[Preemptive]
TQ=2 (Max allowable time) Process AT BT P1 4 2 P2 1 5 3 P3 P4 P5 6 P6 CT TAT WT 8 4 18 17 12 6 2 9 5 21 11 19 13 10 RQ P1 P2 p3 P4 P5 P6 Gantt Chart 2 4 6 8 9 11 13 15 17 18 P1 P2 P3 P4 P5 P6 P6 P5 19 21
41
Round Robin(RR)[Preemptive]
TQ=4 (Max allowable time) Process AT BT P1 4 P2 1 5 P3 2 P4 3 P5 6 P6 CT TAT WT 4 19 18 15 10 8 11 21 17 16 12 RQ P1 P2 P3 P4 P5 P6 Gantt Chart 4 8 10 11 15 P1 P2 P3 P4 P5 P6 18 19 21
42
Round Robin(RR)[Preemptive]
TQ=2 (Max allowable time) Process AT BT P1 4 P2 1 5 P3 2 6 P4 P5 3 P6 7 CT TAT WT 8 4 18 17 12 21 19 13 9 5 10 RQ P1 P2 P3 P4 P5 P6 Gantt Chart 2 4 6 8 9 P1 P2 P3 P4 P5 P6 11 13 15 17 18 19 21
43
Example Say we have the process arrival time chart given below. Process Arrival time Burst Time p p p3 6 4 p4 7 6 p Draw a Gantt chart to illustrate how these processes would be scheduled using Round Robin (RR) and First-come First-Served (FCS) scheduling, and calculate the waiting time for each process. If when choosing a process to schedule next you could legally choose any of a number of processes, choose the one with the lowest numbered name; i.e., choose pi over pj if i < j . Do not include context-switching time in your chart. Assume that the quantum is set to 5 time units. FCFS= GC(RR)= P1 P2 P3 P4 P5 26 30 36 46 P1 P2 P3 P4 P5 0 5 10 15 19 24 29 34 39 40 45 46
44
A Multi level Que Scheduling Algorithm
It partitions the ready queue into several separate queues. Each queue has its own scheduling algo. Process are permanently assigned to one queue based on some priority. Let us have a multilevel queue scheduling algo with five queues in order of priority: Highest priority Lowest Priority Each queue has absolute Priority over lower priority queues.No process in the interactive process could run unless the queues for system processes is empty. If an interactive editingprocess entered the ready queue while a batch process was runing, the batch process would be preempted. System Process Interactive Process Interactive Editing Process Batch Process Student Process
45
Multi-Level feedback Queue Scheduling
Highest priority High Priority Queue Runnable processes Medium Priority Queue Low priority Que Here processes are not permanently assigned to a queue on entry to the system. Instead, they are allowed to move between queues. The idea is to separate processes With different CPU burst characteristics. If a processes uses too much CPU time, it will be moved to a lower priority queue. Similarly, a processes that waits too long in a low priority queue will be moved to a higher priority queue. This form of aging prevents starvation.
46
Multi-Level feedback Queue Scheduler
In general a Multi-Level feedback Queue Scheduler is defined by the following parameters The number of queues The scheduling algo for each queue The method used to determine when to upgrade a process to a higher priority queue The method used to determine when to demote a process to a lower priority queue The method used to determine which queue a process will enter when that process needs service
47
Dynamic multi level scheduling
The main drawback of priority scheduling: starvation! To avoid it: Prevent high priority processes from running indefinitely by changing priorities dynamically: Each process has a base priority increase priorities of waiting processes at each clock tick Approximation SJF scheduling increase priorities of I/O bound processes (decrease those of CPU bound processes) priority = 1/f (f is the fraction of time-quantum used)
48
Objective Throughput is Process completed per time unit
Completion of the whole process Time for waiting in ready queue Waiting to get into memory 2. Response time is The time it takes to start responding Time required for the completion of the process Total time for execution of the process None of these Which scheduling algorithm is inherently preemptive? FCFS b)SJF c)RR d)Priority Scheduling 4. The optimal scheduling algorithm a)FCFS b)SJF c)RR None of these
49
Objective 5. Find the average waiting time in RR scheduling with time slice 4 Process Burst Time P P P 7 b) c) d) None of these 6. Which of the following scheduling algorithm does give minimum average waiting time? FCFS b)SJF c)RR d)Priority Scheduling 7. The scheduler which select jobs from the pool of jobs and loads them to the ready queue is a) Long Term b) Short Term c) Medium Term d)none of these
50
Objective 8. Under which of the following situations CPU scheduling decision takes place When a process switches from running to waiting state When a process switches from running to ready state When a process switches from waiting to ready state All of these 9.Which of the following reduces degree of multi programming a)Long term scheduler b) Mid-term scheduler c) Short-term scheduler d) all of these 10. Preemptive SJF is known as: Shortest I/O Burst first b)shortest CPU burst first Round Robin d) Shortest remaining time first 11. If dispatch latency increases then the degree of multiprogramming a)Increases b)decreases c)Remains constant d)None of these 12. The preemption of a process takes place after a fixed interval of time called a) Time Slice b) Time Quantum c)Response Time d) None of these
51
Answer I 2 3 4 5 6 7 8 9 10 11 12 a c d b
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.