View
220
Download
3
Embed Size (px)
Citation preview
Operating System & Memory Hierarchy
i206 Fall 2010
John Chuang
Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst
John Chuang 2
Summary of Key Concepts
Data Storage:- Bit storage using transistors- Memory hierarchy- Locality of reference and caching
Operating System:- Process management- Context switch- Threads vs. processes- Synchronization and locks
John Chuang 3
Memory Hierarchy
Bits & BytesBinary Numbers
Number Systems
Gates
Boolean Logic
Circuits
CPU Machine Instructions
Assembly Instructions
Program Algorithms
Application
Memory
Data compression
Compiler/Interpreter
OperatingSystem
Data Structures
Analysis
I/O
Memory hierarchy
Design
Methodologies/Tools
Process
Truth tableVenn DiagramDeMorgan’s Law
Numbers, text,audio, video, image, …
Decimal, Hexadecimal, Binary
AND, OR, NOT, XOR, NAND, NOR,etc.
Register, CacheMain Memory,Secondary Storage
Context switchProcess vs. ThreadLocks and deadlocks
Op-code, operandsInstruction set arch
Lossless v. lossyInfo entropy & Huffman code Adders, decoders,
Memory latches, ALUs, etc.
DataRepresentation
Data
Data storage
Principles
ALUs, Registers,Program Counter, Instruction Register
Network
Distributed Systems Security
Cryptography
Standards & Protocols
Inter-processCommunication
Searching, sorting,Encryption, etc.
Stacks, queues,maps, trees, graphs, …
Big-O
UML, CRC
TCP/IP, RSA, …
ConfidentialityIntegrityAuthentication…
C/S, P2PCaching
sockets
Formal models
Finite automataregex
John Chuang 4
Storing1 Bit ofData
Flip-flop (also called a latch) is a circuit built from gates that can store one bit of data- Setting input A to 1 sets output to 1- Setting input B to 1 sets output to 0- While both input lines are 0, the most recently stored value is preserved
Adapted from Brookshear Fig 1.5
output
Input A
Input B
Example: This 8-bit memory cell can store a byte of data.
John Chuang 5
Data Storage
Where and how do we store data?
Brookshear Fig 2.13
John Chuang 6
Data Storage
In general, we want data to be located in close proximity of their use (the CPU in this case)
Why do we want to store data at so many different locations within a computer?
Considerations of- Storage capacity- Access latency- Cost- …- Patterns of data access
John Chuang 7
Memory Hierarchy
Image from http://www.howstuffworks.com/computer-memory1.htm
John Chuang 8
Storage Technologies
Characteristics- Solid State vs.
Mechanical- Volatile vs. Nonvolatile- Read only vs. Read-write
(RW)- Expense- Speed- Storage capacity (a
function of size & expense)
Types:- Registers: Volatile,
small, expensive, fast, solid state, RW
- DRAM (dynamic random access memory): Volatile, now large, expensive, fast, solid state, RW
- ROM (read-only memory): Nonvolatile, Read-only, cheap, fast
- Hard Disk: Nonvolatile, mechanical, RW, cheap, slow
- Flash Memory (aka SSD): Nonvolatile, solid state, RW, expensive, fast (Versus hard disk: silent, lighter, faster … but more expensive)
- Other external storage: CD-ROM, floppy disks, magnetic tape, …
John Chuang 9
Levels of a Memory Hierarchy(circa 2000)
http://www.ece.arizona.edu/~ece369/lec10S01/sld047.htm
John Chuang 10
Data Storage
In general, we want data to be located in close proximity of their use (the CPU in this case)
Why do we want to store data at so many different locations within a computer?
Considerations of- Storage capacity- Access latency- Cost- …- Patterns of data access
John Chuang 11
Caching Data access patterns
exhibit locality of reference- Recently referenced item
more likely to be referenced again
Caches store items that have been used recently for faster access
Caching can be done at various levels of the memory hierarchy
Image from http://cne.gmu.edu/modules/vm/orange/realprog.html
ALU Registers
R
W
R
W
R
W
L1/L2Cache
MainMemory
R
WDisk
Storage
John Chuang 12
OperatingSystem
Bits & BytesBinary Numbers
Number Systems
Gates
Boolean Logic
Circuits
CPU Machine Instructions
Assembly Instructions
Program Algorithms
Application
Memory
Data compression
Compiler/Interpreter
OperatingSystem
Data Structures
Analysis
I/O
Memory hierarchy
Design
Methodologies/Tools
Process
Truth tableVenn DiagramDeMorgan’s Law
Numbers, text,audio, video, image, …
Decimal, Hexadecimal, Binary
AND, OR, NOT, XOR, NAND, NOR,etc.
Register, CacheMain Memory,Secondary Storage
Context switchProcess vs. ThreadLocks and deadlocks
Op-code, operandsInstruction set arch
Lossless v. lossyInfo entropy & Huffman code Adders, decoders,
Memory latches, ALUs, etc.
DataRepresentation
Data
Data storage
Principles
ALUs, Registers,Program Counter, Instruction Register
Network
Distributed Systems Security
Cryptography
Standards & Protocols
Inter-processCommunication
Searching, sorting,Encryption, etc.
Stacks, queues,maps, trees, graphs, …
Big-O
UML, CRC
TCP/IP, RSA, …
ConfidentialityIntegrityAuthentication…
C/S, P2PCaching
sockets
Formal models
Finite automataregex
John Chuang 13
Abstract View of System Components
Source: Silberschatz, Galvin, Gagne
John Chuang 14
What is an Operating System? A software program that acts as an intermediary between a user of a computer and the computer hardware
Operating system functions:- Oversee operation of computer- Store and retrieve files- Schedule programs for execution- Execute programs
John Chuang 15
OS Components
Shell (command interpreter) Kernel
- Process manager- Memory manager- File manager- I/O systems manager- Networking- Scheduler/dispatcher- Etc.
shell
user
user
user
kernel
John Chuang 16
Process Management
A process is a program in execution- A process needs certain resources, including CPU time, memory, files, and I/O devices, to accomplish its task
The operating system is responsible for the following activities in connection with process management- Process creation and deletion- Process suspension and resumption- Provision of mechanisms for:
- process synchronization- process communication
John Chuang 17
Process State
As a process executes, it changes state- new: The process is being created.- running: Instructions are being executed.- waiting: The process is waiting for some event
to occur.- ready: The process is waiting to be assigned to
a process.- terminated: The process has finished execution.
Source: Silberschatz, Galvin, Gagne
John Chuang 18
Resource Sharing Many processes share hardware resources
- CPU, I/O (mouse, keyboard, monitor, disk, …) Processes must take turns
- Context switch (aka process switch) Operating system serves as coordinator/ scheduler
Brookshear Fig 3.6
John Chuang 19
Context Switch
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process
Process control block (PCB, on right) contains information associated with each process
Context-switch time is overhead; the system does no useful work while switching- A system is said to be “thrashing” when it spends more time performing context switches than useful work
Source: Silberschatz, Galvin, Gagne
John Chuang 20
Threads A thread (or lightweight process) is a basic unit of CPU utilization
Each thread has its own- program counter- stack- register set
The threads share:- text section (program)
- data section (global variables)
Source: Silberschatz, Galvin, Gagne
John Chuang 21
Threads versus Multiple Processes
Threads are cheaper to create than processes (~10-20x)
Switching to a different thread in same process is much cheaper than switching to a different process (~5-50x) since no context switching required
Threads within same process can share data and other resources more conveniently and efficiently (than Inter-Process Communication)
Threads within a process are not protected from one another
John Chuang 22
Synchronization
Scenario: two threads want to update count (current value is 4)- Thread 1: count = count + 1- Thread 2: count = count - 1
What is final value of count?- (a) 3- (b) 4- (c) 5- (d) any of the above- (e) none of the above
John Chuang 23
Synchronization
LOAD R1, XY LOAD R2, #1 ADD R3, R1, R2 STORE R3, XY
LOAD R4, XY LOAD R5, #-1 ADD R6, R4, R5 STORE R6, XY
John Chuang 24
Synchronization
LOAD R1, XY LOAD R2, #1 ADD R3, R1, R2 STORE R3, XY LOAD R4, XY LOAD R5, #-1 ADD R6, R4, R5 STORE R6, XY
LOAD R1, XY LOAD R4, XY LOAD R2, #1 LOAD R5, #-1 ADD R3, R1, R2 ADD R6, R4, R5 STORE R3, XY STORE R6, XY
John Chuang 25
Lesson
Even a seemingly simple operation like count++ is not atomic, and can be subject to race conditions
John Chuang 26
Synchronization Use locks to coordinate actions of competing processes
Also known as mutual exclusion (mutex)
Simplest form of lock is a semaphore- Process must acquire semaphore before entering critical section; release semaphore upon exit
initialize_lock() ... acquire_lock() try: change_shared_data() finally: release_lock()
John Chuang 27
Summary of Key Concepts
Data Storage:- Bit storage using transistors- Memory hierarchy- Locality of reference and caching
Operating System:- Process management- Context switch- Threads vs. processes- Synchronization and locks