Four Fundamental Operating System Concepts Sam Kumar CS 162: Operating Systems and System Programming Lecture 2

Four Fundamental Operating System Concepts Sam Kumar CS 162: Operating Systems and System Programming Lecture 2

Four Fundamental Operating System Concepts Sam Kumar CS 162: Operating Systems and System Programming Lecture 2 https://inst.eecs.berkeley.edu/~cs162/su20 Read: A&D, 2.1-7 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 1 Recall: What is an Operating System? • Special layer of software that provides application software access to hardware resources • Convenient abstraction of complex hardware devices • Protected access to shared resources • Security and authentication appln appln • Communication appln OS Hardware 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 2 Recall: OS Abstracts the Underlying Hardware Compiled Program System Libs Process: Execution environment with restricted rights provided by OS Threads Address Spaces Files Sockets Operating System ISA Networks Processor PgTbl Memory & TLB Storage Hardware OS Mem I/O Ctrlr 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 3 Recall: OS Protects Processes and the Kernel Segmentation fault Compiled (core dumped) Compiled Program 1 Program 2 System Libs System Libs Process 1 Process 2 Compiler Threads Address Spaces Files Sockets Threads Address Spaces Files Sockets Operating System ISA Networks Processor PgTbl Memory & TLB Storage Hardware OS Mem I/O Ctrlr 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 4 Recall: What is an Operating System? • Referee • Manage protection, isolation, and sharing of resources • Resource allocation and communication • Illusionist • Provide clean, easy-to-use abstractions of physical resources • Infinite memory, dedicated machine • Higher level objects: files, users, messages • Masking limitations, virtualization • Glue • Common services • Storage, Window system, Networking • Sharing, Authorization • Look and feel 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 5 Today: Four Fundamental OS Concepts • Thread: Execution Context • Program Counter, Registers, Execution Flags, Stack • Address Space (with Translation) • Program’s view of memory is distinct from physical machine • Process: Instance of a Running Program • Address space + one or more threads + … • Dual-Mode Operation and Protection • Only the “system” can access certain resources • Combined with translation, isolates programs from each other 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 6 OS Bottom Line: Run Programs 0xFFF… Program Source Executable OS int main() { … ; Compiler Memory Editor } data stack and Linker OS Loader instructions heap foo.c a.out data • Create OS “PCB”, address space, stack and heap instructions • Load instruction and data segments of executable file into memory 0x000… • “Transfer control to program” PC: • Provide services to program registers • While protecting OS and program Processor 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 7 OS Bottom Line: Run Programs 0xFFF… Program Source Executable OS int main() { … ; Compiler Memory Editor } data stack and Linker OS Loader instructions heap foo.c a.out data • Create OS “PCB”, address space, stack and heap instructions • Load instruction and data segments of executable file into memory Creates a process 0x000… • “Transfer control to program” from a program PC: • Provide services to program registers • While protecting OS and program Processor 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 8 Review (61C): How Programs Execute Processor next Memory PC: Instruction fetch instruction Decode decode Registers Execute ALU data 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 9 Review (61C): How Programs Execute Addr 232-1 • Execution sequence: … • Fetch Instruction at PC Data1 • Decode R0 Data0 • Execute (possibly using registers) … Inst237 • Write results to registers/mem R31 Fetch Inst236 F0 • PC = Next Instruction(PC) Exec … … • Repeat F30 Inst4 PC Inst3 PC Inst2 PC Inst1 PC Inst0 PC Addr 0 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 10 Today: Four Fundamental OS Concepts • Thread: Execution Context • Program Counter, Registers, Execution Flags, Stack • Address Space (with Translation) • Program’s view of memory is distinct from physical machine • Process: Instance of a Running Program • Address space + one or more threads + … • Dual-Mode Operation and Protection • Only the “system” can access certain resources • Combined with translation, isolates programs from each other 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 11 Key OS Concept: Thread • Definition: A single, unique execution context • Program counter, registers, stack • A thread is the OS abstraction for a CPU core • A “virtual CPU” of sorts • Registers hold the root state of the thread: • Including program counter – pointer to the currently executing instruction • The rest is “in memory” • Registers point to thread state in memory: • Stack pointer to the top of the thread’s (own) stack 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 12 Illusion of Multiple Processors vCPU1 vCPU2 vCPU3 • Threads are virtual cores • Multiple threads: Multiplex hardware in time • A thread is executing on a processor when it Shared Memory is resident in that processor's registers On a single physical CPU: • Each virtual core (thread) has PC, SP, Registers • vCPU1 vCPU2 vCPU3 vCPU1 vCPU2 Where is it? • On the real (physical) core, or Time • Saved in memory – called the Thread Control Block (TCB) 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 13 OS Object Representing a Thread • Traditional term: Thread Control Block (TCB) • Holds contents of registers when thread is not running… • … And other information the kernel needs to keep track of the thread and its state. 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 14 Registers: RISC-V → x86 Load/Store Arch with software conventions Complex mem-mem arch with specialized registers and “segments” • In CS 61C you learned RISC-V • In section tomorrow you’ll learn x86 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 15 Illusion of Multiple Processors vCPU1 vCPU2 vCPU3 • At T1: vCPU1 on real core • At T2: vCPU2 on real core Shared Memory • What happened? On a single physical CPU: • OS ran [how?] T1T2 • Saved PC, SP, … in vCPU1’s thread control block • Loaded PC, SP, … from vCPU2’s thread control block vCPU1 vCPU2 vCPU3 vCPU1 vCPU2 Time • This is called context switch 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 16 Very Simple Multiprogramming • All vCPUs share non-CPU resources • Memory, I/O Devices • Each thread can read/write memory • Including data of others • And the OS! • Unusable? • This approach is used in: • Very early days of computing • Embedded applications • MacOS 1-9/Windows 3.1 (switch only with voluntary yield) • Windows 95-ME 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 17 Today: Four Fundamental OS Concepts • Thread: Execution Context • Program Counter, Registers, Execution Flags, Stack • Address Space (with Translation) • Program’s view of memory is distinct from physical machine • Process: Instance of a Running Program • Address space + one or more threads + … • Dual-Mode Operation and Protection • Only the “system” can access certain resources • Combined with translation, isolates programs from each other 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 18 Key OS Concept: Address Space • Program operates in an address space that is distinct from the physical memory space of the machine 0x000… Processor Memory Translator Registers 0xFFF… 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 19 Address Space • Definition: Set of accessible addresses and 0x000… the state associated with them • 232 = ~4 billion on a 32-bit machine Code • What happens when you read or write to an Static Data address? Heap • Perhaps acts like regular memory • Perhaps causes I/O operation • (Memory-mapped I/O) Stack • Causes program to abort (segfault)? • Communicate with another program 0xFFF… • … 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 20 Typical Address Space Structure 0x000… PC: Code SP: Static Data Heap Processor registers Stack 0xFFF… 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 21 What can hardware do to help the OS protect itself from programs? And programs from each other? 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 22 Base and Bound (no Translation) • Requires relocation 0000… Code • Can the program touch OS? Static Data • Can it touch other programs? Heap Base Address Original Program Stack 1000… 0000… code ≥ 1000… 0010… Code Static Data Program 1010… Static Data heap address Heap stack Bound < 0100… Stack 1100… 0100… FFFF… 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 23 Base and Bound (with Translation) 0000… • Can the program touch OS? Code • Can it touch other programs? Static Data Heap • Fragmentation still an issue! Original Program Stack 0000… Base Address code 1000… 0010… 1000… Code Static Data Program 0010… Static Data heap 1010… address Heap stack Bound < 0100… Stack 1100… 0100… FFFF… 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 24 Paged Virtual Address Space • What if we break the entire virtual address space into equal-size chunks (i.e., pages) and have a base and bound for each? • All pages same size, so easy to place each page in memory! • Hardware translates address using a page table • Each page has a separate base • The “bound” is the page size • Special hardware register stores pointer to page table 6/23/2020 Kumar CS 162 at UC Berkeley, Summer 2020 25 Paged Virtual Address Space Memory Processor Registers <Page #> Page Table <Frame Addr> instruction <Page Offset> Page (eg, 4 kb) <Virtual Address> = <Page #> <Page Offset> PT Addr • Instructions operate on virtual addresses • Translated at runtime to physical addresses

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    62 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us