Você está na página 1de 49

Final Review

Fred Kuhns

Washington
WASHINGTON UNIVERSITY IN ST LOUIS
Computer Systems
• Computer Systems
– Hardware and software combine to solve specific problems
– Two software categories: 1) Application software, 2) System software
• Goals of an OS
– User Environment: Execution environment, Error detection and handling,
Protection and security, Fault tolerance and failure recovery
– Resource Management: Time and space management; Synchronization and
deadlock handling; Accounting and status information
• Driving Concepts
– Abstraction: manage complexity, create an extended machine.
– Virtualization: Permits controlled sharing and isolation (enables process
model and isolation)
– Resource management: implicit/explicit allocation and policy control.
Performance critical, competing goals of maximizing utilization, minimizing
overhead, providing fair/predictable access to system resources.
• Resource Sharing: apparent (logical) concurrency or true (parallel)
concurrency
– abstract machines transparently share resources
– concurrent programs may also elect to explicitly share resources
– Space-multiplexed orTime-multiplexed
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 2
Common Abstractions: Process and Resource
An application consists of one or more processes, a set of resources and state
Resources
Processes
File F0
Process P0
File F1
Process P1
... ...
File FM
Process PN
memory Ri
...
CPU
display Rj

Operating System

other
CPU DRAM DISKS NET other
other
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 3
Computations, Processes and Threads

• Computation: sequential execution, implements algorithm,


applied to data set.
• Program: encoding of an algorithm using a programming
language (i.e. C or C++).
• program requests resources from OS, example system call
interface.
• Process: program in execution and embodies all resources
and state of a running program (CPU, memory, files,...)
• Modern process model separates execution context from
other resources.
– Thread: Dynamic object representing an execution path and
computational state. Remaining resources are shared amongst
threads in a process

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 4


Implementing Operating Systems
• Common functions:
– Device management
– Process, thread and resource management
– Memory management
– File management
• Issues in OS design
– Performance – should be low overhead
– Exclusive use of resources
• protection and security – sharing, authorizing, authenticating
– correctness – does what it is supposed to do
– maintainability – universal goal
– commercial – they are used by people and organizations
– standards and open systems – conforming to standards
• Common implementation mechanisms
– Protection and processor modes
– Trusted control program – kernel
– Method for processes to request services of the OS – system calls

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 5


Policy and Mechanism
• A recurring theme in OS design is ensuring exclusive use
of system resources (real and abstract)
• Administrators and developers define policies to define
resource sharing and isolation
• Mechanisms implement these policies
• Common mechanisms:
– hardware enforced processor modes to control execution of
privileged instructions (such as for I/O or memory)
– Core OS modules are trusted and have access to protected
operations and all resources. This kernel of the OS is responsible
for enforcing policies.
– Well defined and protected mechanism for requesting services
of the OS. There are two approaches: system calls or message
passing.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 6
OS Techniques
• Controlling access to hardware resources is done using
hardware supported privilege levels, typically two: user
and system

• Privileged operations, resource management, protection


enforcement (isolation), and sharing performed by a
trusted control program: the Kernel

• Users request OS services using a well defined interface


that validates user and notifies OS of request: two
common methods:
– System calls: trap changes mode and executes privileged code in
context of calling process
– Message passing: interface constructs message and sends to
another system (i.e.
Fred Kuhns (01/17/09)
privileged)
CS422 process
– Operating Systems Concepts 7
An Example: an OS Kernel
• Trusted program that runs directly on the hardware
• Loaded at boot time and initializes system,
– creates some initial system processes.
– remains in memory and manages the system
• Resource manager/mediator - process is key abstraction.
– Time share (time-slice) the CPU,
– coordinate access to peripherals,
– manage virtual memory.
– Synchronization primitives.
• Well defined entry points:
– syscalls, exceptions or interrupts.
• Performs privileged operations.
• Kernel Entry
– Synchronous - kernel performs work on behalf of the process:
• System call interface (UNIX API): central component of the UNIX API
• Hardware exceptions - unusual action of process
– Asynchronous - kernel performs tasks that are possibly unrelated to current process.
• Hardware interrupts - devices assert hardware interrupt mechanism to notify kernel of events which
require attention (I/O completion, status change, real-time clock etc)
– System processes - scheduled by OS (swapper and pagedaemon)
• Synchronization within Kernel
– Traditional: Kernel is re-entrant but with only one thread/processes active at any given time,
Nonpreemptive. Blocking operations, Masking interrupts
– Modern: threaded kernels, synchronization primitives within kernel, locking granularity is
issue

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 8


Zooming in on Computer Architecture
CPU Memory 0
PC .. 1
IR MAR .
Reg N
Reg 1 MBR Instruction
Reg 0
I/O AR
Instruction
execution Instruction
..
unit I/O BR
.
Data
Devices
Data
Data
.. Data
..
Buffers .
N
PC - Program Counter MBR - Memory Buffer Register
IR - Instruction Register I/O AR - Input/Output Address Register
MAR - Memory Address Register I/O BE - Input/Output Buffer Register
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 9
Instruction Cycle with Interrupts

Fetch Cycle Execute Cycle Interrupt Cycle

Interrupts
Disabled

Interrupts
Enabled
Fetch Next Execute Check for &
START
Instruction Instruction Process Int

HALT

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 10


Interrupts – devices notify CPU of some event

Processor
Device table
dispatcher clock handler
(interrupt handler) X

Bus

command status rt-counter

Timer

What about multiple interrupts?

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 11


OS and I/O Hardware Management
• Two fundamental operations: 1) Processing data, 2) Perform I/O
• Large diversity in I/O devices, capabilities and performance
• Goal: provide simple/consistent interface, efficient use, max
concurrency
• Mechanisms: device drivers provide standard interface to I/O devices
• Kernel I/O tasks: Scheduling, Buffering, Caching, Spooling,
Reservations
• Common concepts: Port, Bus, Controller
• Accessing a device: Direct I/O instructions and Memory-mapped I/O
• Common Techniques:
– Synchronous: Direct I/O with polling, aka Programmed I/O
– Asynchronous: Interrupt Driven I/O
– Direct memory access (DMA)
– Memory Mapped I/O
• Process I/O interface models: Blocking, Nonblocking and Asynchronous
• Improving performance: Reduce context switch, data copy; use DMA;
joint scheduling of multiple resources.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 12
Process Model
• Define process: ?
• Traditional unit of control for synchronization, sharing, communication
and deadlock control
• Explicit versus implicit resource allocations
• Know state diagrams
• Understand how process execution is interleaved on the system CPU(s)
• Be able to describe a context switch
• Understand process creation and termination
• Cooperating processes
– Independent processes cannot affect or be affected by one another
– Cooperating processes can affect or be affected by one another
– Advantages of process cooperation: Information sharing, Computation
speed-up, Modularity, Convenience
• Dangers of process cooperation
– Data corruption, deadlocks, increased complexity
– Requires processes to synchronize their processing

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 13


Example: Process Representation
System Memory Process P2 State
Hardware State (registers)
init P0
Program counter

Kernel Process Table



P0: HW state; resources
Memory base register
Process P3 …
Process State (logical)
P2: HW state; resources
Open file table
Process P1 …
Pending Requests
PN: HW state; resources


Process P2
Memory mappings

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 14


5 State Model – More realistic
admit dispatch
New Ready Running
pause

event terminate
wait

Blocked
Exit

• New: The process is being created.


• Running: Instructions being executed.
• Blocked (aka waiting): Must wait for some event to occur.
• Ready: Runnable but waiting to be assigned to a processor.
• Exit : The process has finished execution.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 15


Suspending a Process

suspend

admit dispatch
New Ready Running
preempt
suspend terminate
admit event wait
activate
suspend
Ready Blocked
Blocked Exit
Suspend event Suspend activate
Suspended Processes
(possibly on backing store)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 16


3 Process Example
entity

P1

P2

P3

System ...
(dispatch)

time

Running Ready Blocked

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 17


Process Scheduling
• Long-term scheduler: job scheduler
– selects which processes should be brought into the ready queue.
– invoked infrequently (seconds, minutes)
– controls the degree of multiprogramming

• Medium-term scheduler
– allocates memory for process.
– invoked periodically or as needed.

• Short-term scheduler: CPU scheduler


– selects which process should be executed next and allocates CPU.
– invoked frequently (ms)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 18


IPC (Message Passing)
• Purposes: Data Transfer, Sharing Data, Event notification, Resource
Sharing and Synchronization, Process Control
• Mechanisms: message passing and shared memory
• Message passing systems have no shared variables.
• Two operations for fixed or variable sized message:
– send(message)
– receive(message)
• Communicating processes must establish a communication link and
exchange messages via send and receive
• Communication link: physical or logical
• implementation methods for logical communication links::
1. Direct or Indirect communications
2. Symmetric or Asymmetric communications
3. Automatic or Explicit buffering
4. Send-by-copy or send-by-reference
5. fixed or variable sized messages
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 19
Communication Methods
• Direct : Processes must name each other explicitly : Symmetric v.
asymmetric addressing
– Properties of communication link: automatic link establishment, at
most one link between processes and must know peers ID.
– Disadvantages: a process must know the name or ID
• Indirect : Use mailboxes (aka ports) with unique ID
– Properties of communication link: must share mailbox, >2 processes
per mailbox or > 1 mailbox between processes.
– Ownership: process versus system mailbox, ownership and read
permissions.
• Synchronous versus asynchronous:
– blocking send, nonblocking send, blocking receive, nonblocking
receive
• Buffering: Messaging system must temporarily buffer messages.
Zero capacity, Bounded capacity, Unbounded capacity

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 20


Multiprocessors
• Advantages : performance and fault tolerance
• Classifications: tightly or loosely coupled
• Memory access schemes: UMA, NUMA and NORMA
• Cache consistency problem

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 21


Typical SMP System

500MHz CPU CPU CPU CPU


cache MMU cache MMU cache MMU cache MMU

Issues: System/Memory Bus


• Memory contention Main 50ns I/O
• Limited bus BW INT Bridge
Memory subsystem
• I/O contention
• Cache coherence System Functions ether
(timer, BIOS, reset)
scsi
Typical I/O Bus: video
• 33MHz/32bit (132MB/s)
• 66MHz/64bit (528MB/s)
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 22
Threads
• Dynamic object representing an execution path
and computational state.
– Effectiveness of parallel computing depends on the performance of
the primitives used to express and control parallelism
– Useful for expressing the intrinsic concurrency of a program
regardless of resulting performance
• Benefits and drawbacks for the models we studied: User
threads, Kernel threads and Scheduler Activations

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 23


Threading Models

P P P P P P

Many-to-One One-to-One Many-to-Many

• User threads := Many-to-One


• kernel threads := One-to-One
• Mixed user and kernel := Many-to-Many

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 24


Solaris Threading Model (Combined)

Process 1 Process 2

user
L L L L Int kthr

kernel ......
...

P P P
hardware
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 25
CPU Scheduling
• Understand CPU-I/O burst cycle
• Components: short-term scheduler, dispatcher
• Criteria: CPU utilization, Throughput, Turnaround time, Waiting
time, Response time
• Algorithms: FIFO, Shortest-Job-First, Priority-based,
Round-robin, Multilevel Queue, Multilevel feedback
queue
• Priority-based scheduling, static versus dynamic
priorities
– Solution to starvation problem?
• Preemptive versus nonpreemptive schemes
• Typical goals: Interactive Systems, Batch Systems,
Real-time Systems
• timers and clock handler
• Priority inversion

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 26


Histogram of CPU-burst Times

From Silberschatz, Galvin and Gagne, “Operating Systems Concepts”, 6th eidtion, Wiley, 2002.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 27


Concurrency: Origins and problems
• Context
– Processes need to communicate
– Kernel communication/controlling hardware resources (for example I/O processing)
• Issues:
– How is information exchanged between processes (shared memory or messages)?
– How to prevent interference between cooperating processes (mutual exclusion)?
– How to control the sequence of process execution (conditional synchronization)?
– How to manage concurrency within the OS kernel?
• Problems:
– Execution of the kernel may lead to concurrent access to state
– Deferred processing pending some event
– Processes explicitly sharing resource (memory)
• Problems with shared memory
– Concurrent programs may exhibit a dependency on process/thread execution sequence
or processor speed (neither are desirable!)
– race condition – who’s first, and who goes next. affects program results.
– There are two basic issues resulting from the need to support concurrency:
– Mutual exclusion: ensure that processes/threads do not interfere with one another,
i.e. there are no race conditions. In other words, program constraints (assumptions)
are not violated.
– Conditional synchronization. Processes/threads must be able to “wait” for the data
to arrive or constraints (assertions) to be satisfied.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 28


Race Conditions - Example
• There are 4 cases for x:
– case 1: task A runs to completion first
loading y=0, z=0. Example 1
x=0+0=0 int y = 0, z = 0;
thread A { x = y + z}
– case 2: Task B runs and sets y to 1, thread B {y = 1; z = 2}
then Task A runs loading y=1 and z=0.
x=1+0=1 Results:
x = {0, 1, 2, 3}
– case 3: Task A runs loading y=0, then load y into R0
Task B runs to completion, then Task A load z into R1
runs loading z=2.
x=0+2=2 set R0 = R0 + R1
set R0 -> x
– case 4: Task B runs to completion, then
Task A runs loading y=1, z=2,
x=1+2=3

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 29


Critical Section Problem
• Entry/exit protocol satisfies:
• Mutual Exclusion: At most one
process may be active within the
critical section CS. Task A {
• Absence of deadlock (livelock): if two while (True) {
or more processes attempt to enter entry protocol;
CS, one will eventually succeed. critical section;
exit protocol;
• Absence of Unnecessary delay:
non-critical section;
Processes neither within nor
}
competing for CS (terminated or
}
executing in Non-CS) can not block
another process from entering CS.
• Eventual entry (no starvation, more a
function of scheduling): if a
processes is attempting to enter CS
it will eventually be granted access.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 30
Necessary Conditions for Deadlock
• 1) Mutual exclusion:
– One process holds a resource in a non-sharable mode.
– Other processes requesting resource must wait for resource to be
released.

• 2) Hold-and-wait:
– A process must hold at least one allocated resource while awaiting one or
more resources held by other processes.

• 3) No preemption:
– Resources not forcibly removed from a process holding it, the holding
process must voluntarily released it.

• 4) Circular wait
– a closed chain of processes exists, such that each process holds at least
one resource needed by the next process in the chain.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 31
Example of a Resource Allocation Graph
R1 R2 R1 R2 R1 R2

P1 P2 P3 P1 P2 P3 P1 P2 P3

R3 R4 R3 R4 R3 R4
Initial: P3 requests R3 P2 releases R1,
No Deadlock (Deadlock) P3 requests R3
(No Deadlock:
• If graph contains no cycles ⇒ Why not?)
– no deadlock.
• If graph contains a cycle ⇒
– if only one instance per resource type, then deadlock.
– if several instances per resource type, possibility of deadlock.
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 32
Approaches to Deadlock Handling
• Ensure system never enters a deadlocked state: Use
protocol to prevent or avoid deadlock:
– deadlock prevention scheme - ensure that at least one of the
necessary conditions cannot hold.
– deadlock avoidance scheme - requires the OS know in advance the
resource usage requirements of all processes. Then for each
request the OS decides if it could lead to a deadlock before
granting.
• Allow the system to enter a deadlock state and then
recover (detection and recovery).
– system implements an algorithm for deadlock detection, if so then
recover
• Assume deadlocks never occur in the system
– used by most operating systems, including UNIX.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 33


Recall the Von Neumann Architecture

Central Processing Unit (CPU)

Arithmetic-Logical Unit
(ALU) Control Unit

Device Controller
Primary Memory Device Controller
Device
Device Controller
Device
Device Controller
Device
Device

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 34


Memory Management
• Understand the principle of locality (temporal and spatial locality)
• Understand impact of Stride-k reference patterns
• Understand cache
• Memory Manager Functions: Allocate memory to processes, Map
process address space to allocated memory, Minimize access
times while limiting memory requirements
• Understand program creation steps and typical process address
map: compile, link and load
• Partitioning schemes and fragmentation
– varia ble sizes and placement algorithms: best-fit, worst-fit, next-
fit, first-fit.
• Placement and relocation
• Paging and segmentation

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 35


Cache/Primary Memory Structure
Set Cache
Memory Number
Address V Tag Block
0 Set 0 V Tag Block
1 ...
2 Block
3
V Tag Block
Set S-1 V Tag Block

E lines per set


S = 2s sets in the cache
B = 2b data Bytes per line
M = 2m = max memory address
t = m – (s+b) tag bits per line
1 valid bit per line
Block (may also require a dirty bit)
Cache size = C = B * E * S
2n - 1 m address bits (= t + s + b)
Word Length t bits s bits b bits

Address
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 36
Typical Process Address Space
Low Address
(0x00000000)
Text (shared)

Initialized Data
Unitialized Data
Process
Address space Heap (Dynamic)

stack (dynamic)

High Address Environment


(0x7fffffff)

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 37


Memory Management: Requirements
• Relocation
– Why/What:
• programmer does not know where the program will be placed in memory when
it is executed
• while the program is executing, it may be swapped to disk and returned to
main memory at a different location
– Consequences/Constraints:
• memory references must be translated in the code to actual physical memory
address
• Protection - Protection and Relocation are interrelated
– Why/What:
• Protect process from interference by other processes
• processes require permission to access memory in another processes address
space.
– Consequences/Constraints:
• impossible to check addresses in programs since the program could be
relocated
• must be checked at run time
• Sharing - Sharing and Relocation are interrelated
– allow several processes to access the same data
– allow multiple programs to share the same program text

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 38


Variable Partition Placement Algorithm
alloc 16K block
8K 8K

12K First Fit 12K

22K
6K
Last Best Fit
allocated 18K
block (14K) 2K

8K 8K
6K 6K

Allocated block
14K Free block 14K
Next Fit

36K
20K
Before After
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 39
Hardware Support for Relocation
Relative address

Process Control
Base Register Block

Adder Program
Absolute
Bounds Register Comparator address

Data
Interrupt to
operating system

Stack

Process image in
main memory

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 40


An Example: Paged Virtual Memory
Working set
Physical
P1 virtual address space P2 virtual
address space Frame 0 address space
page0 page0
Frame 1
page1 page1
Frame 2
page2 page2
Frame 3
page3 page3
Frame 4
Frame 5
pagen pagen
Frame 6
resident Frame 7
Address
Non-resident Translation
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 41
Example Paging System

CPU
Unitialized data
DRAM Stack and heap
Low Address
(0x00000000) Text (shared) Allocated virtual pages
Initialized Data
app1 Unitialized Data Swap
Address space Heap (Dynamic) Disk
UFS
High Address stack (dynamic)
(0x7fffffff) Environment

Text and
initialized data app1

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 42


File Mapping - read/write interface
VM Approach Traditional Approach
Process P1 Process P1

process process

mmap(): Address space


read/write: Copy Copy
Virtual Memory System Buffer Cache
P1 pages

Copy Copy

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 43


Paging and Segmented Architectures
• Memory references are dynamically translated into physical
addresses at run time
– A process may be swapped in and out of main memory such that it
occupies different regions
• A process may be broken up into pieces that do not need to be
located contiguously in main memory
– All pieces of a process do not need to be loaded in main memory during
execution
• Example Program execution:
– Resident set: Operating system brings into main memory portions of the
memory space
– If process attempts to access a non-resident page then the MMU raises
an exception causing the OS to block process waiting for page to be
loaded.
– Steps for loading a process memory pages:
• Operating system issues a disk I/O Read request
• Another process is dispatched to run while the disk I/O takes place
• An interrupt is issued when disk I/O complete which causes the operating
system to place the affected process in the Ready state
Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 44
Know the following
• Define thrashing
• working set model
• Three policies used with paging systems: fetch,
placement and replacement
• Understand the MMU operation
• Know the static and dynamic paging algorithms
– static: Optimal, Not recently used, First-in, First-out and
Second chance, Clock algorithm, Least Recently Used, Least
Frequently Used
– Dynamic: working set algorithm
• How does page buffering help?

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 45


Address Translation Overview
MMU
CPU Virtual
address TLB
physical
cache
address
context table pointer
context

Page tables

X bits Y bits
Virtual address virtual page number offset in page

Page Table Entry (PTE) M R control bits frame number


Z bits

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 46


Example 1-level address Translation
Virtual address
20 bits 12 bits DRAM
virtual page number offset in page Frames
offset within frame
Frame X
X offset

offset with table physical address of frame

add
start of page table PTE
M R control bits frame number

(Process) Page Table


current page table register

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 47


Secondary Storage
• See homework problems
• Know the disk scheduling algorithms: FCFS, SSTF,
SCAN (elevator algorithm), C-SCAN, LOOK and C-
LOOK

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 48


Remaining material
• Review Class Notes and Homework Problems
• FS, Networking, Remote FS and RPC.

Fred Kuhns (01/17/09) CS422 – Operating Systems Concepts 49

Você também pode gostar