490 likes | 778 Views
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.
E N D
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 Static priorities can lead to starvation!
Dynamic Priorities Example: multilevel feedback
ProcessesThe Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant
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
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.
Computer Process Shared resources: Physical memory Disk Printer/keyboard/mouse Process Thread Shared resources: Address space I/O Device handles Processes vs. Threads
The Thread Model (1) (a) Three processes each with one thread (b) One process with three threads
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
The Thread Model (2) • Items shared by all threads in a process • Items private to each thread
The Thread Model (3) Each thread has its own stack
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
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
Example 2: Web server • Gets requests, sends web pages back • Some pages much more popular than others • Keep popular pages in memory cache
Thread Code Dispatcher thread Worker thread
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)
Scheduling User-space Threads • 50-msec process quantum • threads run 5 msec/CPU burst
Win2000 Processes and Threads • Win2000 implements kernel-space threads. • OS does not schedule a process – it schedules a thread inside a process.
Win2000 Priorities Mapping of Win32 priorities to Windows 2000 priorities
Win2000 Scheduling A detailed view of the Win2000 Ready-queue
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
Inter-Process Communication Mutual Exclusion
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
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?
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”
Print Spooler State thread thread
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
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.
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.
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.
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.
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;
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
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.
Software Solution 2 : Alternation Thread 1 Thread 0 Busy waiting on spin-lock variable “turn”.Each process sets turn to the other process.
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.
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