1 / 45

Today

Today. Lab 4 due Wednesday! HW 4 out today! What a process again?. Fork-Exec. fork-exec model: fork() creates a copy of the current process execve() replaces the current process’ code & address space with the code for a different program

mora
Download Presentation

Today

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today • Lab 4 due Wednesday! • HW 4 out today! What a process again?

  2. Fork-Exec • fork-exec model: • fork() creates a copy of the current process • execve() replaces the current process’ code & address space with the code for a different program • There is a whole family of exec calls – see exec(3) and execve(2) // Example arguments: path="/usr/bin/ls”, // argv[0]="/usr/bin/ls”, argv[1]="-ahl", argv[2]=NULL void fork_exec(char *path, char *argv[]) { pid_tpid = fork(); if (pid != 0) { printf("Parent: created a child %d\n”, pid); } else { printf("Child: exec-ing new program now\n"); execv(path, argv); } printf("This line printed by parent only!\n"); }

  3. Exec-ing a new program Very high-level diagram of what happens when you run the command ”ls” in a Linux shell: fork(): child Stack Stack Stack Stack child parent exec(): Heap Heap Heap Data Data Data Data Code: /usr/bin/bash Code: /usr/bin/ls Code: /usr/bin/bash Code: /usr/bin/bash

  4. execve:Loading and Running Programs Stack bottom Null-terminated envvar strings • intexecve( char *filename, char *argv[], char *envp[]) • Loads and runs in current process: • Executable filename • With argument list argv • And environment variable listenvp • Env. vars: “name=value” strings(e.g. “PWD=/homes/iws/pjh”) • execve does not return (unless error) • Overwrites code, data, and stack • Keeps pid, open files, a few other items Null-terminated cmd line arg strings unused envp[n] == NULL envp[n-1] … envp[0] argv[argc] == NULL argv[argc-1] … argv[0] Linker vars envp argv argc Stack frame for main Stack top

  5. exit: Ending a process • void exit(int status) • Exits a process • Status code: 0 is used for a normal exit, nonzero for abnormal exit • atexit()registers functions to be executed upon exit void cleanup(void) { printf("cleaning up\n"); } void fork6() { atexit(cleanup); fork(); exit(0); }

  6. Zombies • Idea • When process terminates, it still consumes system resources • Various tables maintained by OS • Called a “zombie” • A living corpse, half alive and half dead • Reaping • Performed by parent on terminated child • Parent is given exit status information • Kernel discards process • What if parent doesn’t reap? • If any parent terminates without reaping a child, then child will be reaped by init process (pid == 1) • But in long-running processes we need explicit reaping • e.g., shells and servers

  7. wait: Synchronizing with Children • int wait(int *child_status) • Suspends current process (i.e. the parent) until one of its children terminates • Return value is the pid of the child process that terminated • On successful return, the child process is reaped • If child_status!= NULL, then the int that it points to will be set to a status indicating why the child process terminated • There are special macros for interpreting this status – see wait(2) • If parent process has multiple children, wait() will return when any of the children terminates • waitpid() can be used to wait on a specific child process

  8. HC Bye CT Bye wait Example void fork_wait() { int child_status; pid_t child_pid; if (fork() == 0) { printf("HC: hello from child\n"); } else { child_pid = wait(&child_status); printf("CT: child %d has terminated\n”, child_pid); } printf("Bye\n"); exit(0); }

  9. Process management summary • fork gets us two copies of the same process (but fork() returns different values to the two processes) • execve has a new process substitute itself for the one that called it • Two-process program: • First fork() • if (pid == 0) { //child code } else { //parent code } • Two different programs: • First fork() • if (pid == 0) { execve() } else { //parent code } • Now running two completely different programs • wait/ waitpid used to synchronize parent/child execution and to reap child process

  10. Summary • Processes • At any given time, system has multiple active processes • Only one can execute at a time, but each process appears to have total control of the processor • OS periodically “context switches” between active processes • Implemented using exceptional control flow • Process management • fork-exec model

  11. Data & addressing Integers & floats Machine code & C x86 assembly programming Procedures & stacks Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C Roadmap C: Java: Car c = new Car(); c.setMiles(100); c.setGals(17); float mpg = c.getMPG(); car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: get_mpg: pushq %rbp movq %rsp, %rbp ... popq %rbp ret OS: Machine code: 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111 Computer system:

  12. Processes • Definition: A processis an instance of a running program • One of the most important ideas in computer science • Not the same as “program” or “processor” • Process provides each program with two key abstractions:

  13. Processes • Definition: A processis an instance of a running program • One of the most important ideas in computer science • Not the same as “program” or “processor” • Process provides each program with two key abstractions: • Logical control flow • Each process seems to have exclusive use of the CPU • Private virtual address space • Each process seems to have exclusive use of main memory • How are these illusions maintained?

  14. Processes • Definition: A processis an instance of a running program • One of the most important ideas in computer science • Not the same as “program” or “processor” • Process provides each program with two key abstractions: • Logical control flow • Each process seems to have exclusive use of the CPU • Private virtual address space • Each process seems to have exclusive use of main memory • How are these illusions maintained? • Process executions interleaved (multi-tasking) – last time • Address spaces managed by virtual memory system – today!

  15. Virtual Memory (VM) • Overview and motivation • VM as tool for caching • Address translation • VM as tool for memory management • VM as tool for memory protection

  16. Virtual Memory (Previous Lectures) • Programs refer to virtual memory addresses • movl (%ecx),%eax • Conceptually memory is just a very large array of bytes • Each byte has its own address • System provides address space private to particular “process” • Allocation: Compiler and run-time system • Where different program objects should be stored • All allocation within single virtual address space • What problems does virtual memory solve? FF∙∙∙∙∙∙F 00∙∙∙∙∙∙0

  17. Problem 1: How Does Everything Fit? 64-bit addresses: 16 Exabyte Physical main memory: Few Gigabytes ? And there are many processes ….

  18. Problem 2: Memory Management Physical main memory Process 1 Process 2 Process 3 … Process n stack heap .text .data … What goes where? x

  19. Problem 3: How To Protect Physical main memory Process i Process j Problem 4: How To Share? Physical main memory Process i Process j

  20. How would you solve those problems?

  21. Indirection • “Any problem in computer science can be solved by adding another level of indirection” • Without Indirection • With Indirection Name Thing Name Thing Thing

  22. Indirection • Indirection: the ability to reference something using a name, reference, or container instead the value itself. A flexible mapping between a name and a thing allows changing the thing without notifying holders of the name. • Without Indirection • With Indirection • Examples: Domain Name Service (DNS) name->IP address, phone system (e.g., cell phone number portability), snail mail (e.g., mail forwarding), 911 (routed to local office), DHCP, call centers that route calls to available operators, etc. Name Thing Name Thing Thing

  23. Each process gets its own private virtual address space Solves the previous problems Solution: Level Of Indirection Virtual memory mapping Process 1 Physical memory Virtual memory Process n

  24. Address Spaces • Virtual address space: Set of N = 2n virtual addresses {0, 1, 2, 3, …, N-1} • Physical address space: Set of M = 2m physical addresses (n > m) {0, 1, 2, 3, …, M-1} • Every byte in main memory: one physical address; zero, one, or more virtual addresses

  25. Mapping Physical Memory A virtual address can be mapped to either physical memory or disk. Think of memory as a cache for the whole address space. Virtual Address Disk

  26. Used in “simple” systems like embedded microcontrollers in devices like cars, elevators, and digital picture frames A System Using Physical Addressing Main memory 0: 1: 2: Physical address (PA) 3: CPU 4: 4 5: 6: 7: 8: ... M-1: Data word

  27. Used in all modern desktops, laptops, servers One of the great ideas in computer science A System Using Virtual Addressing Main memory 0: CPU Chip 1: 2: Virtual address (VA) Physical address (PA) 3: MMU CPU 4: 4 4100 5: 6: 7: 8: ... M-1: Data word

  28. VM and the Memory Hierarchy • Think ofvirtual memoryas an array of N = 2n contiguous bytes stored on a disk • Then physical main memory (DRAM) is used as a cachefor the virtual memory array • The cache blocks are called pages(size is P = 2p bytes) Virtual memory Physical memory 0 VP 0 Unallocated 0 PP 0 VP 1 Cached Empty PP 1 Uncached Unallocated Empty Cached Uncached Empty PP 2m-p-1 Cached M-1 VP 2n-p-1 Uncached N-1 Virtual pages (VPs) stored on disk Physical pages (PPs) cached in DRAM

  29. Memory Hierarchy: Core 2 Duo Not drawn to scale L1/L2 cache: 64 B blocks ~4 MB ~4 GB ~500 GB Disk L1 I-cache L2 unified cache Main Memory 32 KB CPU Reg • L1 • D-cache Throughput: 16 B/cycle 8 B/cycle 2 B/cycle 1 B/30 cycles Latency: 3 cycles 14 cycles 100 cycles millions Miss penalty (latency): 33x Miss penalty (latency): 10,000x

  30. DRAM Cache Organization • DRAM cache organization driven by the enormous miss penalty • DRAM is about 10x slower than SRAM • Disk is about 10,000x slower than DRAM • (for first byte; faster for next byte) • Consequences? • Block size? • Associativity? • Write-through or write-back?

  31. DRAM Cache Organization • DRAM cache organization driven by the enormous miss penalty • DRAM is about 10x slower than SRAM • Disk is about 10,000x slower than DRAM • (for first byte; faster for next byte) • Consequences • Large page (block) size: typically 4-8 KB, sometimes 4 MB • Fully associative • Any VP can be placed in any PP • Requires a “large” mapping function – different from CPU caches • Highly sophisticated, expensive replacement algorithms • Too complicated and open-ended to be implemented in hardware • Write-back rather than write-through

  32. How do we perform the VA -> PA translation? Indexing into the “DRAM Cache” Main memory 0: CPU Chip 1: 2: Virtual address (VA) Physical address (PA) 3: MMU CPU 4: 4 4100 5: 6: 7: 8: ... M-1: Data word

  33. Address Translation: Page Tables • A page table (PT) is an array of page table entries (PTEs) that maps virtual pages to physical pages. Physical memory (DRAM) Physical page number or disk address PP 0 VP 1 Valid VP 2 PTE 0 0 null VP 7 1 PP 3 VP 4 1 0 1 Virtual memory (disk) 0 null 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 • How many page tables are in the system? VP 6 VP 7

  34. Address Translation: Page Tables • A page table (PT) is an array of page table entries (PTEs) that maps virtual pages to physical pages. Physical memory (DRAM) Physical page number or disk address PP 0 VP 1 Valid VP 2 PTE 0 0 null VP 7 1 PP 3 VP 4 1 0 1 Virtual memory (disk) 0 null 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 • How many page tables are in the system? • One per process VP 6 VP 7

  35. Address Translation With a Page Table Virtual address (VA) • Page table base register • (PTBR) Virtual page number (VPN) Virtual page offset (VPO) Page table Page table address for process Valid Physical page number (PPN) Valid bit = 0: page not in memory (page fault) In most cases, the hardware (the MMU) can perform this translation on its own, without software assistance • Physical page number (PPN) Physical page offset (PPO) Physical address (PA)

  36. Page Hit • Page hit: reference to VM byte that is in physical memory Physical memory (DRAM) Physical page number or disk address Virtual address PP 0 VP 1 Valid VP 2 PTE 0 0 null VP 7 1 PP 3 VP 4 1 0 1 Virtual memory (disk) 0 null 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7

  37. Page Fault • Page fault: reference to VM byte that is NOT in physical memory Physical memory (DRAM) Physical page number or disk address Virtual address PP 0 VP 1 Valid VP 2 PTE 0 0 null VP 7 1 PP 3 VP 4 1 0 1 Virtual memory (disk) 0 null 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 What happens when a page fault occurs? VP 6 VP 7

  38. Fault Example: Page Fault int a[1000]; main () { a[500] = 13; } • User writes to memory location • That portion (page) of user’s memory is currently on disk • Page handler must load page into physical memory • Returns to faulting instruction: mov is executed again! • Successful on second try 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10 User Process OS exception: page fault movl Create page and load into memory returns

  39. Handling Page Fault • Page miss causes page fault (an exception) Physical memory (DRAM) Physical page number or disk address Virtual address PP 0 VP 1 Valid VP 2 PTE 0 0 null VP 7 1 PP 3 VP 4 1 0 1 Virtual memory (disk) 0 null 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7

  40. Handling Page Fault • Page miss causes page fault (an exception) • Page fault handler selects a victim to be evicted (here VP 4) Physical memory (DRAM) Physical page number or disk address Virtual address PP 0 VP 1 Valid VP 2 PTE 0 0 null VP 7 1 PP 3 VP 4 1 0 1 Virtual memory (disk) 0 null 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7

  41. Handling Page Fault • Page miss causes page fault (an exception) • Page fault handler selects a victim to be evicted (here VP 4) Physical memory (DRAM) Physical page number or disk address Virtual address PP 0 VP 1 Valid VP 2 PTE 0 0 null VP 7 1 PP 3 VP 3 1 1 0 Virtual memory (disk) 0 null 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7

  42. Handling Page Fault • Page miss causes page fault (an exception) • Page fault handler selects a victim to be evicted (here VP 4) • Offending instruction is restarted: page hit! Physical memory (DRAM) Physical page number or disk address Virtual address PP 0 VP 1 Valid VP 2 PTE 0 0 null VP 7 1 PP 3 VP 3 1 1 0 Virtual memory (disk) 0 null 0 PTE 7 1 VP 1 Memory resident page table (DRAM) VP 2 VP 3 VP 4 VP 6 VP 7

  43. Why does it work?

  44. Why does it work? Locality • Virtual memory works well because of locality • Same reason that L1 / L2 / L3 caches work • The set of virtual pages that a program is “actively” accessing at any point in time is called its working set • Programs with better temporal locality will have smaller working sets • If (working set size < main memory size): • Good performance for one process after compulsory misses • If (SUM(working set sizes) > main memory size): • Thrashing:Performance meltdownwhere pages are swapped (copied) in and out continuously

More Related