Docsity

Operating Systems II, Lecture notes of Operating Systems

Partial preview of the text

Download Operating Systems II and more Lecture notes Operating Systems in PDF only on Docsity!

Operating Systems II

Steven Hand

Michaelmas Term 2005

8 lectures for CST IB

Operating Systems II — P/L/TT/

Course Aims

This course aims to:

  • impart a detailed understanding of the algorithms and techniques used within operating systems,
  • consolidate the knowledge learned in earlier courses, and
  • help students appreciate the trade-offs involved in designing and implementing an operating system.

Why another operating sytems course?

  • OSes are some of the largest software systems around ⇒ illustrate many s/w engineering issues.
  • OSes motivate most of the problems in concurrency
  • more people end up ‘writing OSes’ than you think
    • modifications to existing systems (e.g. linux)
    • embedded software for custom hardware
    • research operating systems lots of fun
  • various subsystems not covered to date...

Operating Systems II — Aims i

Recommended Reading

  • Bacon J [ and Harris T ] Concurrent Systems or Operating Systems Addison Wesley 1997, Addison Wesley 2003
  • Tannenbaum A S Modern Operating Systems (2nd Ed) Prentice Hall 2001
  • Silberschatz A, Peterson J and Galvin P Operating Systems Concepts (5th Ed) Addison Wesley 1998
  • Leffler S J The Design and Implementation of the 4.3BSD UNIX Operating System. Addison Wesley 1989
  • Solomon D [ and Russinovich M ] Inside Windows NT (2nd Ed) or Inside Windows 2000 (3rd Ed) Microsoft Press 1998, Microsoft Press 2000
  • OS links (via course web page) http://www.cl.cam.ac.uk/Teaching/current/OpSysII/

Operating Systems II — Books iii

Operating System Functions

Operating System

Hardware

App 1 App 2 App N

An operating system is a collection of software which:

  • securely multiplexes resources, i.e.
    • protects applications from each other, yet
    • shares physical resources between them.
  • provides an abstract virtual machine, e.g.
    • time-shares CPU to provide virtual processors,
    • allocates and protects memory to provide per-process virtual address spaces,
    • presents h/w independent virtual devices.
    • divides up storage space by using filing systems.

And ideally it does all this efficiently and robustly.

Operating Systems II — Introduction & Review 1

Operating System Structures

H/W

S/W

App.

Priv

Unpriv

App. App. App.

Kernel Scheduler

Device Driver Device Driver

System Calls

File System Protocol Code

H/W

S/W

App.

Priv

Unpriv Server DeviceDriver

Server Server

App. App. App.

Kernel Scheduler

DeviceDriver

Traditionally have had two main variants:

  1. Kernel-based (lhs above)
    • set of OS services accessible via software interrupt mechanism called system calls.
  2. Microkernel-based (rhs above)
    • push various OS services into server processes
    • access servers via some interprocess communication (IPC) scheme.
    • increased modularity (decreased performance?)

Operating Systems II — Introduction & Review 3

Vertically Structured Operating Systems

H/W

Sched. S/W

Process^ System^ App.^ App.^ DeviceDriver

Priv

Unpriv

O.S. O.S. O.S. O.S.

Syscalls Driver Stubs

Shared Library Code

  • Consider interface people really see, e.g.
    • set of programming libraries / objects.
    • a command line interpreter / window system.
  • Separate concepts of protection and abstraction ⇒ get extensibility, accountability & performance.
  • Examples: Nemesis, Exokernel, Cache Kernel.

We’ll see more on this next year...

Operating Systems II — Introduction & Review 4

Thread Architectures

  • User-level threads
    • Kernel unaware of threads’ existence.
    • Thread management done by application using an unpriviliged thread library.
    • Pros: lightweight creation/termination; fast ctxt switch (no kernel trap); application-specific scheduling; OS independence.
    • Cons: non-preemption; blocking system calls; cannot utilise multiple processors.
    • e.g. FreeBSD pthreads
  • Kernel-level threads
    • All thread management done by kernel.
    • No thread library (but augmented API).
    • Scheduling can be two-level, or direct.
    • Pros: can utilise multiple processors; blocking system calls just block thread; preemption easy.
    • Cons: higher overhead for thread mgt and context switching; less flexible.
    • e.g. Windows NT/2K, Linux (?).

Hybrid schemes also exist... (see later)

Operating Systems II — Introduction & Review 6

Thread Scheduling Algorithms

Exit

Running

New

Ready

Blocked

dispatch

timeoutor yield

admit release

event event-wait

A scheduling algorithm is used to decide which ready thread(s) should run (and for how long).

Typically use dynamic priority scheduling:

  • each thread has an associated [integer] priority.
  • to schedule: select highest priority ready thread(s)
  • to resolve ties: use round-robin within priority
    • different quantum per priority?
    • CPU bias: per-thread quantum adaption?
  • to avoid starvation: dynamically vary priorities
  • e.g. BSD Unix: 128 pris, 100ms fixed quantum, load- and usage-dependent priority damping.
  • e.g. Windows NT/2K: 15 dynamic pris, adaptive ∼20ms quantum; priority boosts, then decays.

Operating Systems II — Introduction & Review 7

Multiprocessor Operating Systems

Multiprocessor OSes may be roughly classed as either symmetric or asymmetric.

  • Symmetric Operating Systems:
    • identical system image on each processor ⇒ convenient abstraction.
    • all resources directly shared ⇒ high synchronisation cost.
    • typical scheme on SMP (e.g. Linux, NT).
  • Asymmetric Operating Systems:
    • partition functionality among processors.
    • better scalability (and fault tolerance?)
    • partitioning can be static or dynamic.
    • common on NUMA (e.g. Hive, Hurricane).
    • NB: asymmetric 6 ⇒ trivial “master-slave”
  • Also get hybrid schemes, e.g. Disco:
    • (re-)introduce virtual machine monitor
    • can fake out SMP (but is this wise?)
    • can run multiple OSes simultaneously...

Operating Systems II — Multiprocessors 9

Multiprocessor Scheduling (1)

  • Objectives:
    • Ensure all CPUs are kept busy.
    • Allow application-level parallelism.
  • Problems:
    • Preemption within critical sections: ∗ thread A preempted while holding spinlock. ⇒ other threads can waste many CPU cycles. ∗ similar situation with producer/consumer threads (i.e. wasted schedule).
    • Cache pollution: ∗ if thread from different application runs on a given CPU, lots of compulsory misses. ∗ generally, scheduling a thread on a new processor is expensive. ∗ (can get degradation of factor or 10 or more)
    • Frequent context switching: ∗ if number of threads greatly exceeds the number of processors, get poor performance.

Operating Systems II — Multiprocessors 10

Multiprocessor Scheduling (3)

  • Processor Affinity:
    • modification of central queue.
    • threads have affinity for a certain processor ⇒ can reduce cache problems.
    • but: load balance problem again.
    • make dynamic? (cache affinity?)
  • ‘Take’ Scheduling:
    • pseudo-dedicated assignment: idle CPU “takes” task from most loaded.
    • can be implemented cheaply.
    • nice trade-off: load high ⇒ no migration.
  • Co-scheduling / Gang Scheduling:
    • Simultaneously schedule “related” threads. ⇒ can reduce wasted context switches.
  • Q: how to choose members of gang?
  • Q: what about cache performance?

Operating Systems II — Multiprocessors 12

Example: Mach

  • Basic model: dynamic priority with central queue.
  • Processors grouped into disjoint processor sets:
    • Each processor set has 32 shared ready queues (one for each priority level).
    • Each processor has own local ready queue: absolute priority over global threads.
  • Increase quantum when number of threads is small
    • ‘small’ means #threads < (2× #CPUs)
    • idea is to have a sensible effective quantum
    • e.g. 10 processors, 11 threads ∗ if use default 100ms quantum, each thread spends an expected 10ms on runqueue ∗ instead stretch quantum to 1s ⇒ effective quantum is now 100ms.
  • Applications provide hints to improve scheduling:
    1. discouragement hints: mild, strong and absolute
    2. handoff hints (aka “yield to”) — can improve producer-consumer synchronization
  • Simple gang scheduling used for allocation.

Operating Systems II — Multiprocessors 13

Real-Time Systems

  • Need to both produce correct results and meet predefined deadlines.
  • “Correctness” of output related to time delay it requires to be produced, e.g.
    • nuclear reactor safety system
    • JIT manufacturing
    • video on demand
  • Typically distinguish hard real-time (HRT) and soft real-time (SRT): HRT — output value = 100% before the deadline, 0 (or less) after the deadline. SRT — output value = 100% before the deadline, (100 - f (t))% if t seconds late.
  • Building such systems is all about predictability.
  • It is not about speed.

Operating Systems II — Real-Time Systems 15

Real-Time Scheduling

time

s i

p i

a i d i

Task i

  • Basic model:
    • consider set of tasks Ti, each of which arrives at time ai and requires si units of CPU time before a (real-time) deadline of di.
    • often extended to cope with periodic tasks: require si units every pi units.
  • Best-effort techniques give no predictability
    • in general priority specifies what to schedule but not when or how much.
    • i.e. CPU allocation for thread ti, priority pi depends on all other threads at tj s.t. pj ≥ pi.
    • with dynamic priority adjustment becomes even more difficult.

⇒ need something different.

Operating Systems II — Real-Time Systems 16