5.CPU Scheduling

1,766 views
1,649 views

Published on

Senthilkanth,MCA..
The following ppt's full topic covers Operating System for BSc CS, BCA, MSc CS, MCA students..
1.Introduction
2.OS Structures
3.Process
4.Threads
5.CPU Scheduling
6.Process Synchronization
7.Dead Locks
8.Memory Management
9.Virtual Memory
10.File system Interface
11.File system implementation
12.Mass Storage System
13.IO Systems
14.Protection
15.Security
16.Distributed System Structure
17.Distributed File System
18.Distributed Co Ordination
19.Real Time System
20.Multimedia Systems
21.Linux
22.Windows

Published in: Education, Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,766
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
158
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

5.CPU Scheduling

  1. 1. Chapter 5: CPU SchedulingChapter 5: CPU Scheduling
  2. 2. 5.2 Silberschatz, Galvin and GagneOperating System ConceptsChapter 5: CPU SchedulingChapter 5: CPU SchedulingBasic ConceptsScheduling CriteriaScheduling AlgorithmsMultiple-Processor SchedulingReal-Time SchedulingThread SchedulingOperating Systems ExamplesJava Thread SchedulingAlgorithm Evaluation
  3. 3. 5.3 Silberschatz, Galvin and GagneOperating System ConceptsBasic ConceptsBasic ConceptsMaximum CPU utilization obtained with multiprogrammingCPU–I/O Burst Cycle – Process execution consists of a cycle ofCPU execution and I/O waitCPU burst distribution
  4. 4. 5.4 Silberschatz, Galvin and GagneOperating System ConceptsAlternating Sequence of CPU And I/OAlternating Sequence of CPU And I/OBurstsBursts
  5. 5. 5.5 Silberschatz, Galvin and GagneOperating System ConceptsHistogram of CPU-burst TimesHistogram of CPU-burst Times
  6. 6. 5.6 Silberschatz, Galvin and GagneOperating System ConceptsCPU SchedulerCPU SchedulerSelects from among the processes in memory that are ready toexecute, and allocates the CPU to one of themCPU scheduling decisions may take place when a process:1. Switches from running to waiting state2. Switches from running to ready state3. Switches from waiting to ready4. TerminatesScheduling under 1 and 4 is nonpreemptiveAll other scheduling is preemptive
  7. 7. 5.7 Silberschatz, Galvin and GagneOperating System ConceptsDispatcherDispatcherDispatcher module gives control of the CPU to the processselected by the short-term scheduler; this involves:switching contextswitching to user modejumping to the proper location in the user program to restartthat programDispatch latency – time it takes for the dispatcher to stop oneprocess and start another running
  8. 8. 5.8 Silberschatz, Galvin and GagneOperating System ConceptsScheduling CriteriaScheduling CriteriaCPU utilization – keep the CPU as busy as possibleThroughput – # of processes that complete their executionper time unitTurnaround time – amount of time to execute a particularprocessWaiting time – amount of time a process has been waitingin the ready queueResponse time – amount of time it takes from when arequest was submitted until the first response is produced,not output (for time-sharing environment)
  9. 9. 5.9 Silberschatz, Galvin and GagneOperating System ConceptsOptimization CriteriaOptimization CriteriaMax CPU utilizationMax throughputMin turnaround timeMin waiting timeMin response time
  10. 10. 5.10 Silberschatz, Galvin and GagneOperating System ConceptsFirst-Come, First-Served (FCFS)First-Come, First-Served (FCFS)SchedulingSchedulingProcess Burst TimeP1 24P2 3P3 3Suppose that the processes arrive in the order: P1 , P2 , P3The Gantt Chart for the schedule is:Waiting time for P1 = 0; P2 = 24; P3 = 27Average waiting time: (0 + 24 + 27)/3 = 17P1 P2 P324 27 300
  11. 11. 5.11 Silberschatz, Galvin and GagneOperating System ConceptsFCFS Scheduling (Cont.)FCFS Scheduling (Cont.)Suppose that the processes arrive in the orderP2 , P3 , P1The Gantt chart for the schedule is:Waiting time for P1 = 6;P2 = 0; P3 = 3Average waiting time: (6 + 0 + 3)/3 = 3Much better than previous caseConvoy effect short process behind long processP1P3P263 300
  12. 12. 5.12 Silberschatz, Galvin and GagneOperating System ConceptsShortest-Job-First (SJR) SchedulingShortest-Job-First (SJR) SchedulingAssociate with each process the length of its next CPU burst. Usethese lengths to schedule the process with the shortest timeTwo schemes:nonpreemptive – once CPU given to the process it cannot bepreempted until completes its CPU burstpreemptive – if a new process arrives with CPU burst lengthless than remaining time of current executing process, preempt.This scheme is know as theShortest-Remaining-Time-First (SRTF)SJF is optimal – gives minimum average waiting time for a givenset of processes
  13. 13. 5.13 Silberschatz, Galvin and GagneOperating System ConceptsProcess Arrival Time Burst TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4SJF (non-preemptive)Average waiting time = (0 + 6 + 3 + 7)/4 = 4Example of Non-Preemptive SJFExample of Non-Preemptive SJFP1 P3 P273 160P48 12
  14. 14. 5.14 Silberschatz, Galvin and GagneOperating System ConceptsExample of Preemptive SJFExample of Preemptive SJFProcess Arrival Time Burst TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4SJF (preemptive)Average waiting time = (9 + 1 + 0 +2)/4 = 3P1 P3P242 110P45 7P2 P116
  15. 15. 5.15 Silberschatz, Galvin and GagneOperating System ConceptsDetermining Length of Next CPUDetermining Length of Next CPUBurstBurstCan only estimate the lengthCan be done by using the length of previous CPU bursts, usingexponential averaging:Define4.10,3.burstCPUnexttheforvaluepredicted2.burstCPUoflenghtactual1.1≤≤==+αατnthnnt( ) .11 nnnt ταατ −+==
  16. 16. 5.16 Silberschatz, Galvin and GagneOperating System ConceptsPrediction of the Length of the Next CPUPrediction of the Length of the Next CPUBurstBurst
  17. 17. 5.17 Silberschatz, Galvin and GagneOperating System ConceptsExamples of Exponential AveragingExamples of Exponential Averagingα =0τn+1 = τnRecent history does not countα =1τn+1 = α tnOnly the actual last CPU burst countsIf we expand the formula, we get:τn+1 = α tn+(1 - α)α tn -1 + …+(1 - α )jα tn -j + …+(1 - α )n +1τ0Since both α and (1 - α) are less than or equal to 1, eachsuccessive term has less weight than its predecessor
  18. 18. 5.18 Silberschatz, Galvin and GagneOperating System ConceptsPriority SchedulingPriority SchedulingA priority number (integer) is associated with each processThe CPU is allocated to the process with the highest priority(smallest integer ≡ highest priority)PreemptivenonpreemptiveSJF is a priority scheduling where priority is the predicted next CPUburst timeProblem ≡ Starvation – low priority processes may never executeSolution ≡ Aging – as time progresses increase the priority of theprocess
  19. 19. 5.19 Silberschatz, Galvin and GagneOperating System ConceptsRound 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, theprocess is preempted and added to the end of the ready queue.If there are n processes in the ready queue and the timequantum is q, then each process gets 1/n of the CPU time inchunks of at most q time units at once. No process waits morethan (n-1)q time units.Performanceq large ⇒ FIFOq small ⇒ q must be large with respect to context switch,otherwise overhead is too high
  20. 20. 5.20 Silberschatz, Galvin and GagneOperating System ConceptsExample of RR with Time Quantum =Example of RR with Time Quantum =2020Process Burst TimeP1 53P2 17P3 68P4 24The Gantt chart is:Typically, higher average turnaround than SJF, but better responseP1 P2 P3 P4 P1 P3 P4 P1 P3 P30 20 37 57 77 97 117 121 134 154 162
  21. 21. 5.21 Silberschatz, Galvin and GagneOperating System ConceptsTime Quantum and Context SwitchTime Quantum and Context SwitchTimeTime
  22. 22. 5.22 Silberschatz, Galvin and GagneOperating System ConceptsTurnaround Time Varies With The TimeTurnaround Time Varies With The TimeQuantumQuantum
  23. 23. 5.23 Silberschatz, Galvin and GagneOperating System ConceptsMultilevel QueueMultilevel QueueReady queue is partitioned into separate queues:foreground (interactive)background (batch)Each queue has its own scheduling algorithmforeground – RRbackground – FCFSScheduling must be done between the queuesFixed priority scheduling; (i.e., serve all from foreground thenfrom background). Possibility of starvation.Time slice – each queue gets a certain amount of CPU timewhich it can schedule amongst its processes; i.e., 80% toforeground in RR20% to background in FCFS
  24. 24. 5.24 Silberschatz, Galvin and GagneOperating System ConceptsMultilevel Queue SchedulingMultilevel Queue Scheduling
  25. 25. 5.25 Silberschatz, Galvin and GagneOperating System ConceptsMultilevel Feedback QueueMultilevel Feedback QueueA process can move between the various queues; aging can beimplemented this wayMultilevel-feedback-queue scheduler defined by the followingparameters:number of queuesscheduling algorithms for each queuemethod used to determine when to upgrade a processmethod used to determine when to demote a processmethod used to determine which queue a process will enterwhen that process needs service
  26. 26. 5.26 Silberschatz, Galvin and GagneOperating System ConceptsExample of Multilevel FeedbackExample of Multilevel FeedbackQueueQueueThree queues:Q0 – RR with time quantum 8 millisecondsQ1 – RR time quantum 16 millisecondsQ2 – FCFSSchedulingA new job enters queue Q0 which is served FCFS. When itgains CPU, job receives 8 milliseconds. If it does not finish in 8milliseconds, job is moved to queue Q1.At Q1 job is again served FCFS and receives 16 additionalmilliseconds. If it still does not complete, it is preempted andmoved to queue Q2.
  27. 27. 5.27 Silberschatz, Galvin and GagneOperating System ConceptsMultilevel Feedback QueuesMultilevel Feedback Queues
  28. 28. 5.28 Silberschatz, Galvin and GagneOperating System ConceptsMultiple-Processor SchedulingMultiple-Processor SchedulingCPU scheduling more complex when multiple CPUs areavailableHomogeneous processors within a multiprocessorLoad sharingAsymmetric multiprocessing – only one processoraccesses the system data structures, alleviating the needfor data sharing
  29. 29. 5.29 Silberschatz, Galvin and GagneOperating System ConceptsReal-Time SchedulingReal-Time SchedulingHard real-time systems – required to complete acritical task within a guaranteed amount of timeSoft real-time computing – requires that criticalprocesses receive priority over less fortunate ones
  30. 30. 5.30 Silberschatz, Galvin and GagneOperating System ConceptsThread SchedulingThread SchedulingLocal Scheduling – How the threads library decides whichthread to put onto an available LWPGlobal Scheduling – How the kernel decides which kernelthread to run next
  31. 31. 5.31 Silberschatz, Galvin and GagneOperating System ConceptsPthread 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);
  32. 32. 5.32 Silberschatz, Galvin and GagneOperating System ConceptsPthread 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 thisfunction */void *runner(void *param){printf("I am a threadn");pthread exit(0);}
  33. 33. 5.33 Silberschatz, Galvin and GagneOperating System ConceptsOperating System ExamplesOperating System ExamplesSolaris schedulingWindows XP schedulingLinux scheduling
  34. 34. 5.34 Silberschatz, Galvin and GagneOperating System ConceptsSolaris 2 SchedulingSolaris 2 Scheduling
  35. 35. 5.35 Silberschatz, Galvin and GagneOperating System ConceptsSolaris Dispatch TableSolaris Dispatch Table
  36. 36. 5.36 Silberschatz, Galvin and GagneOperating System ConceptsWindows XP PrioritiesWindows XP Priorities
  37. 37. 5.37 Silberschatz, Galvin and GagneOperating System ConceptsLinux SchedulingLinux SchedulingTwo algorithms: time-sharing and real-timeTime-sharingPrioritized credit-based – process with most credits isscheduled nextCredit subtracted when timer interrupt occursWhen credit = 0, another process chosenWhen all processes have credit = 0, recrediting occurs Based on factors including priority and historyReal-timeSoft real-timePosix.1b compliant – two classes FCFS and RR Highest priority process always runs first
  38. 38. 5.38 Silberschatz, Galvin and GagneOperating System ConceptsThe Relationship Between Priorities and Time-sliceThe Relationship Between Priorities and Time-slicelengthlength
  39. 39. 5.39 Silberschatz, Galvin and GagneOperating System ConceptsList of Tasks Indexed According toList of Tasks Indexed According toProritiesProrities
  40. 40. 5.40 Silberschatz, Galvin and GagneOperating System ConceptsAlgorithm EvaluationAlgorithm EvaluationDeterministic modeling – takes a particular predeterminedworkload and defines the performance of each algorithmfor that workloadQueueing modelsImplementation
  41. 41. 5.41 Silberschatz, Galvin and GagneOperating System Concepts5.155.15
  42. 42. End of Chapter 5End of Chapter 5
  43. 43. 5.43 Silberschatz, Galvin and GagneOperating System Concepts5.085.08
  44. 44. 5.44 Silberschatz, Galvin and GagneOperating System ConceptsIn-5.7In-5.7
  45. 45. 5.45 Silberschatz, Galvin and GagneOperating System ConceptsIn-5.8In-5.8
  46. 46. 5.46 Silberschatz, Galvin and GagneOperating System ConceptsIn-5.9In-5.9
  47. 47. 5.47 Silberschatz, Galvin and GagneOperating System ConceptsDispatch LatencyDispatch Latency
  48. 48. 5.48 Silberschatz, Galvin and GagneOperating System ConceptsJava Thread SchedulingJava Thread SchedulingJVM Uses a Preemptive, Priority-Based Scheduling AlgorithmFIFO Queue is Used if There Are Multiple Threads With the SamePriority
  49. 49. 5.49 Silberschatz, Galvin and GagneOperating System ConceptsJava Thread Scheduling (cont)Java Thread Scheduling (cont)JVM Schedules a Thread to Run When:1. The Currently Running Thread Exits the Runnable State2. A Higher Priority Thread Enters the Runnable State* Note – the JVM Does Not Specify Whether Threads are Time-Slicedor Not
  50. 50. 5.50 Silberschatz, Galvin and GagneOperating System ConceptsTime-SlicingTime-SlicingSince the JVM Doesn’t Ensure Time-Slicing, the yield() MethodMay Be Used:while (true) {// perform CPU-intensive task. . .Thread.yield();}This Yields Control to Another Thread of Equal Priority
  51. 51. 5.51 Silberschatz, Galvin and GagneOperating System ConceptsThread PrioritiesThread PrioritiesPriority CommentThread.MIN_PRIORITY Minimum Thread PriorityThread.MAX_PRIORITY Maximum Thread PriorityThread.NORM_PRIORITY Default Thread PriorityPriorities May Be Set Using setPriority() method:setPriority(Thread.NORM_PRIORITY + 2);

×