1 / 45

Priority Scheduling

Priority Scheduling. Idea: Jobs are assigned priorities . Always , the job with the highest priority runs. Note: All scheduling policies are priority scheduling! Question: How to assign priorities?. priority 1. priority 2. priority M. Example for Priorities.

jett
Download Presentation

Priority Scheduling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Priority Scheduling Idea: Jobs are assigned priorities.Always, the job with the highest priority runs. Note: All scheduling policies are priority scheduling! Question: How to assign priorities? priority 1 priority 2 priority M

  2. Example for Priorities Static priorities can lead to starvation!

  3. Dynamic Priorities Example: multilevel feedback

  4. Introduction to Systems Programming Lecture 3 Threads

  5. ProcessesThe Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant

  6. Concepts of a Process • Group of resources • Address space • Program image • file handles, child processes, accounting info… • Thread of execution • Instruction address (program counter) • Registers and CPU state • Stack of called procedures

  7. Separating the concepts • Think of the two concepts separately. • A thread executes in a process • … but allow multiple threads in one process. • All threads share the same address space. • Every process has at least one thread. • Also called lightweight process.

  8. Computer Process Shared resources: Physical memory Disk Printer/keyboard/mouse Process Thread Shared resources: Address space I/O Device handles Processes vs. Threads

  9. The Thread Model (1) (a) Three processes each with one thread (b) One process with three threads

  10. Properties of Threads • Pseudo-parallel: seem to run in parallel, but really take turns. • No protection between threads: • Share the same address space • Supposed to cooperate

  11. The Thread Model (2) • Items shared by all threads in a process • Items private to each thread

  12. The Thread Model (3) Each thread has its own stack

  13. Example 1: Word Processor • Edit p.1  need to reformat all pages • Thread 1: interact with user • Thread 2: reformat (in background) • Thread 3: periodic backups

  14. Word Processor with 3 Threads

  15. Properties • Cannot be separate processes: • All threads need to access the same data (book in memory) • Better performance: • By the time the user wants to see page 100, reformatting may be done

  16. Example 2: Web server • Gets requests, sends web pages back • Some pages much more popular than others • Keep popular pages in memory cache

  17. A Multi-threaded Web server

  18. Thread Code Dispatcher thread Worker thread

  19. Implementing Threads in User Space

  20. Properties of User-space Threads • Thread context switch much faster (x10) without trap to OS • Can work on OS w/o thread support • BUT: • How to handle blocked threads? We do NOT want OS to make whole process blocked. • Threads have to yield CPU voluntarily (no timer interrupt)

  21. Implementing Threads in the Kernel

  22. Scheduling User-space Threads • 50-msec process quantum • threads run 5 msec/CPU burst

  23. Scheduling Kernel-space Threads

  24. Win2000 Processes and Threads • Win2000 implements kernel-space threads. • OS does not schedule a process – it schedules a thread inside a process.

  25. Win2000 Processes and Threads Kernel

  26. Win2000 Priorities Mapping of Win32 priorities to Windows 2000 priorities

  27. Win2000 Scheduling A detailed view of the Win2000 Ready-queue

  28. Win2000 Scheduling Scheme • Priority scheduling (31 == highest) • Within each priority: round robin • Priority of user thread can be • increased (not to exceed 15) when wake up from I/O wait: • +1 from disk, +6 from kb, +8 from soundcard • decreased (not below base priority) when thread uses its full quantum

  29. Inter-Process Communication Mutual Exclusion

  30. Terminology • Historically called “Inter-Process Communication” (IPC) • Really “Inter-Thread Communication” – only the advanced solutions allow communication between separate processes. • Basic examples assume shared variables – which are a feature of threads • We mix the use of thread & process in this chapter

  31. Difficulties with Threads • Share resources  can conflict • Counter = 0; // global variable • Thread 1 does Counter++ • Thread 2does Counter–- // “at the same time” • What is the order of values of Counter ? • 0 : 1 : 0? • 0 : -1 : 0?

  32. Print Spooler Example • n slots in a queue (FIFO) • Two shared (global) control variables: • In : position of first free slot (where next new file will be inserted) • Out : position of first used slot (next to be printed) • Threads A & B want to print at “same time”

  33. Print Spooler State thread thread

  34. NextA = read(In);// NextA == 7 Spool(“fA”, NextA); NextA++; //NextA == 8 write(NextA,In); NextB = read(In);// NextB == 7 Spool(“fB”, NextB); NextB++;//NextB == 8 write(NextB,In); Race Conditions Thread B Thread A Interrupt - context switch

  35. What went wrong? • We have a shared variable: In • Context switch in the middle of a “read-update-write” sequence. • Result: both wrote their file to slot 7. • Because of scheduling order – B’s update is lost!! • This is a Race Condition.

  36. Mutual Exclusion • Need to make sure that shared variables are not read and written “at the same time”. • Mutual exclusion: if one thread is accessing a shared variable, other threads are excluded. • Not always a shared variable. In general – a critical region.

  37. Conditions for Mutual Exclusion • [Safety] No two threads simultaneously in critical region. • [Criticality] No thread running outside its critical region may block another thread. • [Liveness] No thread must wait forever to enter its critical region. No assumptions made about speed of CPU or scheduling.

  38. Desirable Execution

  39. Solution 0: Disable Interrupts • No interrupt  no context switch at bad time But • User bugs will freeze system • Only useful for very short intervals • OS uses this method internally to protect internal data structures.

  40. Software Solution 1: Lock ? int lock; // shared, initially 0 // lock == 1 means “someone in critical region” // Both threads run this code while(lock != 0) /* do nothing */; lock = 1; critical_region(); lock = 0;

  41. Does not work! • If both threads reach while(lock != 0) at (about) the same time, both will find a value 0  both enter critical region. • Violates the Safety property

  42. Mutual Exclusion Terminology • A tight loop like while(lock != 0) is called busy-waiting. • Busy waiting should usually be avoided  wastes CPU. • A lock using busy waiting is a spin lock.

  43. Software Solution 2 : Alternation Thread 1 Thread 0 Busy waiting on spin-lock variable “turn”.Each process sets turn to the other process.

  44. Properties of Alternation • Each process lets the other process into critical region. • Satisfies “Safety”. • A process cannot go into critical region twice in a row (even if other process is outside critical region). • Violates Criticality property.

  45. Concepts for review • Thread • User-space threads • Kernel-space threads • Thread scheduling • Inter-Process-Communication • Mutual Exclusion • Race Condition • Critical Region / Critical Section • Busy-Wait / Spin-lock

More Related