Title: Operating Systems
1Operating Systems
2OS Overview
- What is an OS
- all the code that you didn't write
- the code that manages physical (hardware)
resources - provides users with "logical" well-behaved
environment - OS defines a set of logical resources and a set
of well-defined operations on those objects
(interfaces) - provides mechanisms and policies for the control
of objets/resources - policies define what should be done
- mechanism implements how
- controls how different users and programs
interact - Resources managed by an OS
- CPU
- memory
- disk
- networks
- keyboard, mouse
- various IO devices printers, speakers, cameras.
...
3OS Taxonomy
- Multiprogramming OS
- keeps multiple runnable programs loaded in the
memory - overlaps IO processing of a job with computing of
another - benefits from asynchronous IO devices
- needs mechanism of interrupts and DMA
- tries to optimizes throughput, not response time
- Timesharing
- each user feels as if she has the entire machine
- timesharing tries to optimize response time, not
throughput - needs mechanism of of time-slicing of CPU
- timer clock
- scheduler
- users are permitted to control of the execution
of process
4Computer Architecture and OS
- OS and Architecture
- the functionality of an OS is limited by
architecture features - the structure of an OS can be simplified by
architectural support - DOS is so primitive due to the lack of hardware
support when it was first designed - Most proprietary OS's were developed with the
architecture - Architectural features for modern OS
- timer operation
- atomic synchronization operations
- memory protection
- IO control and operations
- interrupts and exceptions
- OS protection (kernel/user mode)
- protected instructions
- system calls
- partial completion of an instruction
5Architectural Support for OS
- Exceptions
- hardware must detect special conditions
- page fault, write to read-only page, overflow,
... - must transfer control to OS immediately
- hardware must save state of the running process,
so that the faulting process can be restarted
afterwards - what if there is no hardware support
- OS should keep checking the condition at a very
high cost - Synchronization
- interrupts cause potential problem because an
interrupt can occur at any time
non-deterministic race condition - concurrent processes may interfere with each
other - needs an atomic operation at various level
- primitive one (test and set) by hardware
- software can do this at a high cost
- some by programming languages
- then, why do we study this in OS courses
- OS contains lots of concurrency in itself
- synchronization sometimes involves process
management
6Kernel Mode
- Protected Instructions
- users should not be allowed direct access to
- IO devices such as disks, printers, ...
- use privileged instructions or memory mapping
- memory management states
- page table updates,
- page table pointers
- TLB loads, flush, ...
- setting special mode bits
- halt instruction (why do we need this?)
- OS Protection
- how do we know if we can execute a protected
instruction? - architecture must support at least two modes of
operations kernel and user - mode is indicated by a status bit in a protected
processor register - user program executes in user mode the OS
executes in kernel mode - protected instructions can only be executed in
kernel mode.
7Kernel Mode (2)
- Crossing Protection Boundaries
- users have to ask OS to do something privileged
in behalf of them - how does a user program call a kernel mode
service? - system call
- system call instructions should be available to
users - required features of a system call
- stop what is going on exceptions
- specifies the kind of service asked interrupt
vector - specifies the exact contents of the action
parameter passing - saves the status of what is going on so it can be
restored when the service is done - there must be a way to return to user mode
8User-Kernel Interaction
user program
user mode
system call
kernel mode
trap to kernel mode
trap handler
system service routine
OS kernel
9IO Control (from device to OS)
device interrupts
CPU stops current operation, switches to kernel
mode, and saves current PC and other states on
kernel stack
CPU fetches proper vector from vector table and
b ranches to that address (to interrupt handler)
interrupt routine examines device database
and performs actions required by the interrupt
handler completes operation, restores saved
(interrupted) state and returns to user mode (or
calls scheduler to switch to another program)
10Process Management
- Process Management
- a process
- execution entity
- includes an execution context
- an instance of a program in execution (program is
only a file on the disk that is potentially
runnable) - many processes exists
- user programs
- batch jobs
- print spooler, name server, network listener
- OS schedules these processes
code stack PC registers
page tables resource counts .......
11Process
- Process states
- running has the CPU
- ready ready to be executed
- blocked waiting for some event to occur
create()
ready
timer interrupt
IO completion
scheduler
running
blocked
exit()
IO request
12Process Scheduling
- Choose a process to assign the CPU
- Activated by
- timer interrupt
- running process system calls
- other OS routines urgent event
- Scheduling queues
- ready queue
- IO queue (blocked)
- sleep queue
- ....
13Context Switching
- Timer
- how does OS prevent infinite loops?
- a timer is set to generates an interrupt in a
given time - before a user process begins, OS loads the timer
with a time to interrupt (more primitive hardware
generates interrupts periodically independent of
OS) - when the time arrives, the timer issues an
interrupt, and thus the user program stops and OS
regains control
14Context Switching (2)
- Context Switching
- Switching the CPU to another process
- save the state of the old process
- load the saved state of the new process
- System overhead
- 1 - 100 microseconds
- various hardware supports are available
- interrupt mechanism
- scheduling algorithm
- saving/restoring process states
- cache misses
- TLB misses
- overall overhead depends on the time slice
15Threads
- multiple execution entities within the same
address space - execution stack for each thread
- threads share data and code
- sharing is easy
- switching threads is cheaper
- synch betwn threads is easy
- two kinds of thread
- kernel level thread
- OS manages threads
- user level thread
- threads are transparent to OS
- a user level package manages threads
16Memory Management
- Memory Management
- primary memory is the space that is directly
accessible from CPU - program should be in memory to execute
- OS must
- allocate space for programs to run
- deallocates space when needed
- mapping virtual to real address (when virtual
memory is used) - decides how much space to allocate to a process,
and when a process should be removed from memory
17Virtual Memory
- Virtual Memory
- user program may be lager than the available
physical memory - with multiprogramming, there are many programs
that run concurrently - needs larger memory than the real memory
- methods
- overlay
- a program itself manages portions of allocated
memory - very difficult to develop a program
- paging
18Paging
19Address Translation
CPU
virtual address
VPN
offset
20Page Table
- Each entry is addressed by the VPN and the
contents is the RPN - Size may be large
- 32 bit address, 512 bytes/page, byte addressing
- offset field 9 bits, page field 23 bits
- total 223 8 Mega entries per process
- 6 bytes per entry, 32 processes 1536 MB
- Address translation must be fast
- needed for every memory access
- TLB is indispensable
- keep page tables in memory
21Disk Management
- Disk Management
- disk is the persistent memory, i.e., it endures
system failures (we hope) - OS does
- scheduling of disk requests
- disk arm movement
- error handling
- the line between this and the file system is
fuzzy
22File System
- File System
- raw disk is too crude to be used by user programs
- (even for OS)
- the file system provides logical objects and
logical operations on those objects - a file is the basic long-term storage entity a
file is a named collection of persistent
information that can be read or written - needs a way to locate information about a file
efficiently (humans forget too easily)
directories - a directory may be itself a file
- file system provides standard file operations
- the file system also provides general services,
e.g. - backup
- maintaining mapping information
- accounting and quotas
23Other Components of OS
- Protection System
- when many processes may run concurrently, all
resource objects need protection - memory
- processes
- files
- devices
- protection mechanism helps to detect errors as
well as to prevent malicious destruction - Command Interpreter
- interprets user command issued from a keyboard
- some systems have it as a standard part of OS
- other systems it is just a user program
(replaceable) - others do not have such thing they have iconic
interface - Accounting System
- keep track of resource usage
- used for enforce quotas, or to generate bills
24OS Architecture
- OS Structure
- So far, you have seen components of an OS
- the issues are
- how to organize all of them
- what are the entities and where are they
- how do these entities cooperate
- in reality,
- interaction between these components is very
complex - boundaries between them are very fuzzy
- but, we have to build an OS that is
- working
- efficient (performance)
- reliable
- extensible
25OS Architecture (contd)
- Traditional Structure
- everything in one process(program) monolithic
kernel - why monolithic?
- that's the way we have programmed so far
- fast interaction between components (procedure
call) - easy sharing no protection between OS components
- why not monolithic?
- hard to understand
- hard to modify
- unreliable a bug in anywhere causes an entire
system crash - hard to maintain
26OS Architecture (contd)
- Layered Structure
- it has been used in structuring many complex
systems - a layer is a virtual machine to the layer above
it - a layer should not be aware of a layer above it
- THE (Dijkstra)
- why layering?
- easy to focus on a small task
- easy to build a large system step by step
- why not layering?
- real systems are more complex than a simple
hierarchy - not flexible
- poor performance due to layer crossing
- In real world, systems are often modeled as
layered structures but not built that way
27OS Architecture
- Microkernel architecture
- a structure in vogue
- minimize that goes in the kernel
- implement the remaining things of OS as
user-level processes - what for?
- better reliability (focus on small part)
- ease of extension and customization
- any problem? poor performance
- interaction between OS components is not
procedure call anymore, it is inter-process
communication - Examples
- Hydra (CMU, 1970)
- MINIX (Tannenbaum)
- CMU March IBM Workplace, OSF/1. ...
- Chorus
- Microsoft Windows/NT (in a way)
28Windows/NT Structure