Scheduling 6 894 Lecture 6 What is Scheduling

















- Slides: 17

Scheduling 6. 894 Lecture 6

What is Scheduling? • An O/S often has many pending tasks. – Threads, async callbacks, device input. • The order may matter. – Policy, correctness, or efficiency. • Providing sufficient control is not easy. – Mechanisms must allow policy to be expressed.

Scheduling Policy Examples • • Allocate cycles in proportion to money. Maintain high throughput under high load. Never delay high pri thread by > 1 ms. Maintain good interactive response. • Can we enforce policy with the thread scheduler?

Pitfall: Priority Inversion • • Low-priority thread X holds a lock. High-priority thread Y waits for the lock. Medium-priority thread Z pre-empts X. Y is indefinitely delayed despite high priority.

Pitfall: Long Code Paths • Large-granularity locks are convenient. – Non-pre-emptable threads are an extreme case. • May delay high-priority processing.

Pitfall: Efficiency • Efficient disk use requires unfairness. – Shortest-seek-first vs FIFO. – Read-ahead vs data needed now. • Efficient paging policy creates delays. – O/S may swap out my idle Emacs to free memory. – What happens when I type a key? • Thread scheduler doesn’t control these.

Pitfall: Multiple Schedulers • Every resource with multiple waiting threads has a scheduler. • Locks, disk driver, memory allocator. • The schedulers may not cooperate or even be explicit.

Pitfall: Server Processes • User-level servers schedule requests. – X 11, DNS, NFS. • They usually don’t know about kernel’s scheduling policy. • Network packet scheduling also interferes.

Pitfall: Hardware Schedulers • Memory system scheduled among CPUs. • I/O bus scheduled among devices. • Interrupt controller chooses next interrupt. • Hardware doesn’t know about O/S policy. • O/S often doesn’t understand hardware.

Scheduling is a System Problem • Thread/process scheduler can’t enforce policies by itself. • Needs cooperation from: – All resource schedulers. – Software structure. • Conflicting goals may limit effectiveness.

Example: UNIX • Goals: – Simple kernel concurrency model. • Limited pre-emption. – Quick response to device interrupts. • Many kinds of execution environments. • Some transitions are not possible. • Some transitions can’t be controlled.

UNIX Environments Process User Half Kernel Half Timer Soft Interrupt Device Interrupt Network Soft Interrupt Device Interrupt Timer Interrupt User Kernel

UNIX: Process User Half • Interruptable. • Pre-emptable via timer interrupt. – We don’t trust user processes. • Enters kernel half via system calls, faults. – Save user state on stack. – Raise privilege level. – Jump to known point in the kernel. • Each process has a stack and saved registers.

UNIX: Process Kernel Half • Executes system calls for its user process. – May involve many steps separated by sleep(). • Interruptable. – May postpone interrupts in critical sections. • Not pre-emptable. – Simplifies concurrent programming. – No context switch until voluntary sleep(). – No user process runs if a kernel half is runnable. • Each kernel half has a stack and saved registers. • Many processes may be sleep()ing in the kernel.

UNIX: Device Interrupts • Device hardware asks CPU for an interrupt. – To signal new input or completion of output. – Cheaper than polling, lower latency. • Interrupts take priority over u/k half. – – Save current state on stack. Mask other interrupts. Run interrupt handler function. Return and restore state. • The real-time clock is a device.

UNIX: Soft Interrupts • Device interrupt handlers must be short. • Expensive processing deferred to soft intr. – Can’t do it in kernel-half: process not known. – Example: TCP protocol input processing. – Example: periodic process scheduling. • Devices can interrupt soft intr. • Soft intr has priority over user & kernel processes. – But only entered on return from device intr. – Similar to async callback. – Can’t be high-pri thread, since no pre-emption.

UNIX Environments Process User Half Kernel Half Soft Interrupt Transfer w/ choice Transfer, limited choice Transfer, no choice Device Interrupt User Kernel