Operating Systems ECE 344 PriorityBased Scheduling Policies Ashvin
Operating Systems ECE 344 Priority-Based Scheduling Policies Ashvin Goel ECE University of Toronto
Overview q Scheduler overview q Scheduling goals Batch systems o Interactive systems o q Scheduling policies o Batch scheduling policies § First-Come, First Served (FIFO) § Shortest Job First (SJF) § Shortest Remaining Time (SRT) o Interactive scheduling policies § Round-Robin § Static priority scheduling § Dynamic priority scheduling 2
Problem with Round-Robin Scheduling q Interactive scheduling policies aim to provide: o o q Low response time Good throughput as well Round-robin scheduling Preemptive FIFO o Response time depends on time slice o q Time slice trade-off Small time slice: good response, lower throughput o Large time slice: good throughput, poor response o q Why can’t round-robin provide both good response and good throughput? o RR does not distinguish between IO, CPU-bound threads 3
Priority-Based Scheduling Policies q Priority schemes run threads with highest priority q Intuition o Run IO-bound threads with higher priority § Provides good response o Run CPU-bound threads with long time slice § Provides good throughput q Two policies Static priority scheduling o Dynamic priority scheduling o 4
Static Priority Scheduling q Each thread is assigned a priority when it is started o q Priority assigned by user, administrator or programmer Run: Select threads in priority order Keep threads in priority order in the ready queue o Run thread at the front of the ready queue o q Stop: Preempt thread if another thread with higher priority arrives 5
Multi-level Queue Scheduling q Combines priority with round-robin scheduling q Multiple ready queues, one for each priority Scheduler chooses thread from highest-priority queue that has a ready thread o Round-robin scheduling within each queue o High priority Mid priority CPU Low priority q Typically, IO bound threads have higher priority, CPU bound threads have lower priority 6
Priority-Based Scheduling Policies q Two policies Static priority scheduling o Dynamic priority scheduling o 7
Problems with Static Priority Scheduling q Static priority algorithms are hard to use Specifying priority is tricky, depends on other threads o Incorrect choice can cause starvation of low priority threads o § Run a program with a tight loop at highest priority! q What is the root cause of the starvation problem? o CPU bound thread is run at high priority, so it hogs the CPU 8
Dynamic Priority (Feedback) Scheduling q Dynamic priority algorithm o q 1) No priority specification required, 2) No starvation Algorithm has three main ideas o Measure CPU usage of threads over time o Raise priority of threads that do not use much CPU § Provides good response to IO-bound threads o Lower priority of threads that use CPU, use long time slice § Provides good throughput to CPU-bound threads § Avoids starvation q Dynamic priority is also called feedback scheduling 9
Unix Feedback Scheduling q Scheduler uses same algorithm as static priority Each thread is assigned a priority when it is started o Run: Select threads in priority order o Stop: Preempt thread if another thread with higher priority arrives o q Scheduler updates thread priorities periodically o q Period is called time slice Scheduler performs two operations: Measure CPU usage of threads that run within a time slice o Update priority of all threads at the end of the time slice o 10
Measuring CPU Usage, Updating Priority Time slice T 1 T 2 T 1 0 Timer interrupt (clock tick) q T 1 T 2 10 20 C 1 = 0, C 2 = 0 P 1 = 2, P 2 = 3 4, C 2 = 0 6 C 1 = 0, P 1 = 2/2+4, P 2 = 3/2+6 Measure CPU usage (C) of running threads in time slice o q T 2 At interrupt, increment CPU usage of current thread: C = C+1 Update priority (P) of all threads at end of time slice Update priority of all threads: P = P / 2 + C o Reinitialize CPU usage for all threads: C = 0 o Choose thread with smallest P value to run o 11
Understanding Feedback Scheduling q Why does the thread with the smallest P value have the highest priority? Priority is calculated as: P = P/2 + C o When a thread does not use the CPU, its P value will be halved after each time slice, eventually converging to 0! o Thus IO-bound threads will have the smallest P value and be given the highest priority o q When is the priority value updated for any thread? o q Only at the end of each time slice Then how can multiple threads run within a time slice? When a higher priority thread wakes up, it will run o Otherwise, a thread will run for entire time slice o 12
Benefits of Feedback Scheduling q Does the scheduler provide good response time? o q Does the scheduler avoid starvation? o q Priority of threads that use too much CPU will be lowered over time, and so other threads will be able to run Does the scheduler run CPU-bound threads fairly? o q Priority of IO-bound threads goes up over time, and so they run immediately CPU-bound threads will run in round-robin order Is the scheduler efficient? o Scheduler can run with a large time slice to reduce context switch overhead, with minimal impact on the response time! 13
Summary q q q Interactive systems need to support both IO-bound, and CPU-bound programs Scheduling policies need to provide o Good response time to IO-bound programs o Good throughput to CPU-bound programs Priority-based scheduling policies o Static priority scheduling § Uses priorities, raises fairness/starvation, usability issues o Feedback scheduling § Adjusts priorities, provides good response time to IO-bound jobs, while providing fairness and good throughput to CPU-bound jobs q Next lecture: Unix threads and processes 14
Think Time: Static Priority q Static priority schedulers can suffer from priority inversion. What is priority inversion? 15
Think Time: Feedback Scheduling q q Feedback scheduling require a timer interrupt for estimating CPU usage. What is the timer interrupt period in Linux? Run the “top” program. On the top right, you will see three numbers for the load average. How do you think the OS calculates these values? In Unix, users can change thread priorities by using the “nice” value. How does this nice value work? Say 10 timer interrupts occur in a time slice, and a thread takes 30% of the CPU in each time slice. With the Unix feedback scheduler, what will its priority value be over time? 16
Think Time: Feedback Example q Say time slice of the scheduler is 5 timer interrupt units q A thread runs as follows: C = 3 0 C = 2 5 C = 3 10 C = 2 15 20 q Assume the initial priority value of this thread is 0 q The priority of the thread P is updated as: P = P/2 + C q Calculate its priority at time 20 17
- Slides: 17