Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Windows Scheduler


Published on

An introduction to the Windows Scheduling algorithms with a discussion of its relevance to software developers.

Published in: Technology
  • Be the first to comment

The Windows Scheduler

  1. 1. The Windows Scheduler Peter Shirley-Quirk
  2. 2. The Windows Scheduler <ul><li>In this presentation we will look at: </li></ul><ul><ul><li>What the scheduler is </li></ul></ul><ul><ul><li>How it works </li></ul></ul><ul><ul><li>Tips for developers </li></ul></ul>
  3. 3. What is a scheduler? <ul><li>A scheduler shares out the computer system’s processing resource fairly between all the processes it has to run </li></ul><ul><li>Types of scheduler </li></ul><ul><ul><li>Long-term decides whether the system can run any additional jobs/processes </li></ul></ul><ul><ul><li>Mid-term used to swap out a process </li></ul></ul><ul><ul><li>Short-term decides which thread is to be executed </li></ul></ul>
  4. 4. How does Windows Scheduler work? <ul><li>Scheduling algorithm </li></ul><ul><ul><li>Managing a queue of threads that are ready to execute </li></ul></ul><ul><ul><li>Performing context switches and allocating time slices to respond to interrupts that affect which threads are able to run </li></ul></ul><ul><ul><li>Ensuring that threads get a chance to run </li></ul></ul>
  5. 5. When is a thread Ready to run? Figure 3-19, Inside Microsoft Windows 2000, Third Edition by David A. Solomon and Mark E. Russinovich
  6. 6. Context switching (1) <ul><li>Context switching happens: </li></ul><ul><ul><li>At the end of a time slice </li></ul></ul><ul><ul><li>When a higher priority thread becomes ready e.g. garbage collection thread starts </li></ul></ul><ul><ul><li>When the currently running thread goes into a wait state </li></ul></ul><ul><li>To run a thread the processor needs the full context </li></ul><ul><ul><li>registers </li></ul></ul><ul><ul><li>stack (user and kernel) </li></ul></ul><ul><ul><li>environment </li></ul></ul>
  7. 7. Context switching (2) <ul><li>To switch context the scheduler: </li></ul><ul><ul><li>Saves the context for the executing thread </li></ul></ul><ul><ul><li>Places that thread at the end of the queue for its priority </li></ul></ul><ul><ul><li>Finds the highest priority queue that contains ready threads </li></ul></ul><ul><ul><li>Removes the thread at the head of the queue, loads its context, and executes it </li></ul></ul>
  8. 8. Process Priority Classes The Process Priority Class is under the designer’s control Can be read or set using System.Diagnostics.Process.PriorityClass AboveNormal Time-critical tasks that must be executed immediately e.g. the Task List dialog High BelowNormal E.g. multimedia applications RealTime E.g. screen saver, indexer etc. Idle No special scheduling needs. Normal
  9. 9. Where does the priority come from? <ul><li>The thread’s base priority is based on several factors </li></ul><ul><ul><li>Process Priority Class </li></ul></ul><ul><ul><li>Thread Priority Level </li></ul></ul><ul><ul><li>Foreground / background </li></ul></ul><ul><li>These combine to give the Thread’s Base Priority </li></ul><ul><li>The dynamic priority can be boosted by the scheduler </li></ul>
  10. 10. When does the scheduler boost the priority? <ul><li>Avoiding starvation </li></ul><ul><ul><li>To prevent a high priority task from completely blocking a lower priority task the latter will be given a priority boost </li></ul></ul><ul><li>Avoiding choking </li></ul><ul><ul><li>CPU bound vs. IO bound – burst characteristics </li></ul></ul><ul><ul><li>A CPU bound thread can choke resources from an IO bound thread – perceived as poor responsiveness from mouse or keyboard </li></ul></ul>
  11. 11. Resolving priority inversion <ul><li>Thread A, a low-priority thread, is executing code in a critical section (lock). </li></ul><ul><li>Thread B, the high-priority thread, begins waiting for a shared resource (e.g. the lock) from thread A. </li></ul><ul><li>Thread C has medium priority. Thread C receives all the processor time </li></ul><ul><li>Scheduler boosts priority of A so that it can exit the lock </li></ul>A B C Wait Ready Time Priority A
  12. 12. What if there’s more than one processor? <ul><li>The system uses a SMP model to schedule threads on multiple processors </li></ul><ul><ul><li>Any thread can be assigned to any processor </li></ul></ul><ul><ul><li>The system schedules threads to run concurrently </li></ul></ul><ul><ul><li>You can influence the scheduler </li></ul></ul><ul><ul><ul><li>Thread Affinity </li></ul></ul></ul><ul><ul><ul><li>Thread Ideal Processor </li></ul></ul></ul>
  13. 13. Multi-Processor Architecture <ul><li>Hyperthreading </li></ul><ul><ul><li>2 virtual processors but sharing some processor resources – consider using thread affinity </li></ul></ul><ul><li>Intel Dual-core </li></ul><ul><ul><li>Sharing onboard cache – thread affinity </li></ul></ul><ul><li>AMD dual core </li></ul><ul><ul><li>Treat as independent processors </li></ul></ul><ul><li>The future: 4-way, 8-way </li></ul><ul><ul><li>Design for parallel processing </li></ul></ul>
  14. 14. Some final thoughts <ul><li>Designing for concurrency </li></ul><ul><ul><li>Take advantage of multiple processors </li></ul></ul><ul><ul><li>Aim to blend CPU and IO work </li></ul></ul><ul><li>Know your synchronization mechanisms </li></ul><ul><ul><li>Design to avoid deadlock </li></ul></ul><ul><ul><li>Keep locks private and short </li></ul></ul><ul><li>Be aware of Garbage Collection in .NET </li></ul><ul><ul><li>Usually quick, occasionally quite slow </li></ul></ul>
  15. 15. More information <ul><li> </li></ul><ul><ul><li>Managing Processes Sample  </li></ul></ul><ul><ul><li>System.Diagnostics namespace  </li></ul></ul><ul><ul><ul><li>Process </li></ul></ul></ul><ul><ul><ul><li>ProcessThread </li></ul></ul></ul><ul><ul><li>System.Threading namespace  </li></ul></ul><ul><ul><ul><li>Thread </li></ul></ul></ul>