Operating System & Memory Hierarchy i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst
John Chuang2 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 Chuang3 Memory Hierarchy Bits & Bytes Binary Numbers Number Systems Gates Boolean Logic Circuits CPU Machine Instructions Assembly Instructions Program Algorithms Application Memory Data compression Compiler/ Interpreter Operating System Data Structures Analysis I/O Memory hierarchy Design Methodologies/ Tools Process Truth table Venn Diagram DeMorgan’s Law Numbers, text, audio, video, image, … Decimal, Hexadecimal, Binary AND, OR, NOT, XOR, NAND, NOR, etc. Register, Cache Main Memory, Secondary Storage Context switch Process vs. Thread Locks and deadlocks Op-code, operands Instruction set arch Lossless v. lossy Info entropy & Huffman code Adders, decoders, Memory latches, ALUs, etc. Data Representation Data storage Principles ALUs, Registers, Program Counter, Instruction Register Network Distributed Systems Security Cryptography Standards & Protocols Inter-process Communication Searching, sorting, Encryption, etc. Stacks, queues, maps, trees, graphs, … Big-O UML, CRC TCP/IP, RSA, … Confidentiality Integrity Authentication … C/S, P2P Caching sockets Formal models Finite automata regex
John Chuang4 Storing 1 Bit of Data 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 Chuang5 Data Storage Where and how do we store data? Brookshear Fig 2.13
John Chuang6 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 Chuang7 Memory Hierarchy Image from
John Chuang8 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 Chuang9 Levels of a Memory Hierarchy (circa 2000)
John Chuang10 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 Chuang11 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 ALU Registers R W R W R W L1/L2 Cache Main Memory R W Disk Storage
John Chuang12 Operating System Bits & Bytes Binary Numbers Number Systems Gates Boolean Logic Circuits CPU Machine Instructions Assembly Instructions Program Algorithms Application Memory Data compression Compiler/ Interpreter Operating System Data Structures Analysis I/O Memory hierarchy Design Methodologies/ Tools Process Truth table Venn Diagram DeMorgan’s Law Numbers, text, audio, video, image, … Decimal, Hexadecimal, Binary AND, OR, NOT, XOR, NAND, NOR, etc. Register, Cache Main Memory, Secondary Storage Context switch Process vs. Thread Locks and deadlocks Op-code, operands Instruction set arch Lossless v. lossy Info entropy & Huffman code Adders, decoders, Memory latches, ALUs, etc. Data Representation Data storage Principles ALUs, Registers, Program Counter, Instruction Register Network Distributed Systems Security Cryptography Standards & Protocols Inter-process Communication Searching, sorting, Encryption, etc. Stacks, queues, maps, trees, graphs, … Big-O UML, CRC TCP/IP, RSA, … Confidentiality Integrity Authentication … C/S, P2P Caching sockets Formal models Finite automata regex
John Chuang13 Abstract View of System Components Source: Silberschatz, Galvin, Gagne
John Chuang14 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 Chuang15 OS Components Shell (command interpreter) Kernel -Process manager -Memory manager -File manager -I/O systems manager -Networking -Scheduler/dispatcher -Etc. shell user kernel
John Chuang16 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 Chuang17 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 Chuang18 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 Chuang19 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 Chuang20 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 Chuang21 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 Chuang22 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 Chuang23 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 Chuang24 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 Chuang25 Lesson Even a seemingly simple operation like count++ is not atomic, and can be subject to race conditions
John Chuang26 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 Chuang27 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