Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: CPU Scheduling

Similar presentations

Presentation on theme: "Chapter 5: CPU Scheduling"— Presentation transcript:

1 Chapter 5: CPU Scheduling

2 Outline Why scheduling? Scheduling Criteria Scheduling Algorithms
Real-Time Scheduling Thread Scheduling Example: Solaris Algorithm Evaluation

3 Process Execution Process execution consists of alternating cycles of CPU execution and I/O wait Long-term scheduler gets a good mix of CPU-bound & I/O-bound jobs Given that, how to scheudle ready processes? – Shor-term scheduling: focus of this chapter

4 Why scheduling is necessary?
Maximize CPU utilization via multiprogramming, while minimizing response time and/or maximizing throughput → Efficient CPU scheduling required

5 CPU Scheduler Decide which process to execute next among the ready processes in memory Order ready processes according to the specified policy such as FCFS (First Come First Serve)

6 Preemptive vs. nonpreemptive scheduling
A hihger priority process can preempt a currently running low priority process to avoid priority inversion Most real-time scheduling algorithms, e.g., EDF & RM More details about RT scheduling will be discussed later Nonpreemptive scheduling: A higher priority process waits a currently running process to finish regardless of the priority e.g., FCFS Others such as I/O & networking devices are not preemptive

7 Dispatcher Gives control of the CPU to the process selected by the short-term scheduler Context switch between the two user processes Context switch to user mode Jump to the proper location in the user program to restart it → Dispatch latency : Time for dispatcher to stop one process and start another

8 Scheduling Criteria CPU utilization: #CPU cycles used for computation/#total CPU cycles per unit time Throughput: # of processes that complete the execution per unit time Response time: Amount of time it takes from when a request was submitted until the first response is produced

9 Scheduling Criteria Waiting time
Amount of time a process has been waiting in the ready queue Response time = waiting time + execution time

10 Optimization Criteria
Maximize CPU utilization Maximize throughput Minimize response time Minimize waiting time Sometimes they may conflict with each other e.g., system can be overloaded while trying to maximize CPU utilization; response time may significantly increase as a result

11 First-Come, First-Served (FCFS) Scheduling
Process Burst Time (Execution 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: ( )/3 = 17 P1 P2 P3 24 27 30

12 FCFS Scheduling (Cont.)
Suppose that processes arrive in the order P2 , P3 , P1 Gantt chart: Average waiting time: ( )/3 = 3 Much better than previous case Convoy effect: A short process behind a long process suffer long wating/response time P1 P3 P2 6 3 30

13 Shortest-Job-First (SJF)
Schedule the process with the shortest execution time Two schemes: Nonpreemptive Once the CPU is given to a process it cannot be preempted until completes its execution Preemptive Shortest-Remaining-Time-First (SRTF) If a new process arrives with exec time shorter than the remaining time of the currently running process executing process, preempt

14 Shortest-Job-First (SJF)
SJF is optimal: It minimizes average waiting time for a given set of processes

15 Example of Non-Preemptive SJF
Process Arrival Time Exec Time P P P P Average waiting time = ( )/4 = 4 P1 P3 P2 7 3 16 P4 8 12

16 Example of Preemptive SJF
Process Arrival Time Burst Time P P P P Average waiting time = ( )/4 = 3 P1 P3 P2 4 2 11 P4 5 7 16

17 Hard to implement SJF Don’t know the exact exec time when a process arrives Can only estimate it by using the length of previous exec time (Take moving average) Doesn’t always work

18 Prediction of the Length of the Next CPU Burst

19 Priority 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

20 Priority Scheduling SJF is a priority scheduling where priority is determined based on the exec time Problem: Starvation Low priority processes may never execute or suffer very long waiting time Solution: Aging As time progresses increase the priority of the process How to support fairness while preferring high priority processes? Hard problem

21 Round Robin (RR) Each process gets a small unit of CPU time (time quantum), usually milliseconds After the time quantum, preempt the process and add to the end of the ready queue

22 Round Robin 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

23 Example of RR with Time Quantum = 20
Process Burst Time P1 53 P2 17 P3 68 P4 24 Gantt chart: P1 P2 P3 P4 20 37 57 77 97 117 121 134 154 162

24 Time Quantum and Context Switch Time

25 Multilevel Queue Ready queue is partitioned into separate queues, e.g., foreground (interactive) & background (batch) Each queue can have its own scheduling algorithm foreground – RR background – FCFS

26 Multilevel Queue Scheduling must be done between the queues Time slice
Fixed priority scheduling Serve all foreground process first Serve background process only if the foreground queue is empty Possibility of starvation Time slice Each queue gets a certain amount of CPU time which it can schedule amongst its processes, e.g., 80% to foreground in RR 20% to background in FCFS

27 Multilevel Queue Scheduling

28 Multilevel Feedback Queue
A process can move between queues Aging can be implemented this way Defined by the following parameters: number of queues scheduling algorithms for each queue method used to determine when to demote/upgrade a process method used to determine which queue a process will enter when that process needs service

29 Multilevel Feedback Queues

30 Example of Multilevel Feedback Queue
Q0 – RR with time quantum 8 milliseconds Q1 – RR time quantum 16 milliseconds Q2 – FCFS A new job enters queue Q0 . When it gets CPU, it receives 8 ms. If it does not finish in 8 milliseconds, it is moved to Q1. At Q1 job receives 16 ms. If it does not complete within 16ms, it is preempted and moved to queue Q2.

31 Real-Time Scheduling Hard real-time systems Soft real-time systems
A deadline miss may cause catastrophic results, e.g., air traffic control, flight control, engine control, etc. Soft real-time systems A deadline miss does not cause catastrophic results Meet as many deadlines as possible, e.g., video streaming

32 Earliest Deadline First
Optimal Dynamic scheduling algorithm Schedule the task with the earliest absolute deadline Advantage: 100% utilization bound Disadvantage: Domino effect under overload

33 Rate Monotonic Scheduling
Optimal fixed priority scheduling algorithm Assign the highest priority to the task with the shortest period Advantage: Easy to implement Disadvantage Low utilization bound, i.e., 69% Blind to deadlines RMS schedules T1 first T1 T2

34 Multiple Processor Scheduling
Asymmetric multiprocessing One processor, called master, makes all scheduling decisions Symmetric multiprocessing (SMP) Each processor is self-scheduling Need to avoid for two processors to schedule one process at the same time Supported by most OS – Linux, Solaris, Mac OS X, Windows XP, Windows 2000 … Load balancing: Push vs. pull migration

35 Thread Scheduling Local Scheduling (process contention scope)
How the threads library decides which thread to put onto an available LWP Threads belonging to one process compete for a LWP Global Scheduling (system contention scope) How the kernel decides which kernel thread to run next Assign priority POSIX Thread API Select scheduling algorithm Assign priority to threads

36 Example: Solaris 2 Scheduling
Assign the highest priority to RT threads Apply fixed priority among thread groups

37 Algorithm Evaluation Deterministic model Queuing model Simulation
Little’s Formula: Queue length n = average arrival rate * average waiting time Simulation Implementation

38 5.15

39 End of Chapter 5: Any Questions?

Download ppt "Chapter 5: CPU Scheduling"

Similar presentations

Ads by Google