Presentation is loading. Please wait.

Presentation is loading. Please wait.

University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

Similar presentations


Presentation on theme: "University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?"— Presentation transcript:

1 University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?

2 University of Washington 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) 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_t pid = 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 University of Washington Exec-ing a new program 3 Stack Code: /usr/bin/bash Data Heap Stack Code: /usr/bin/bash Data Heap Stack Code: /usr/bin/bash Data Heap Stack Code: /usr/bin/ls Data fork() : exec() : Very high-level diagram of what happens when you run the command ”ls” in a Linux shell: parent child

4 University of Washington execve : Loading and Running Programs int execve( char *filename, char *argv[], char *envp[] ) Loads and runs in current process:  Executable filename  With argument list argv  And environment variable list envp  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 env var strings unused Null-terminated cmd line arg strings envp[n] == NULL envp[n-1] envp[0] … Linker vars argv[argc] == NULL argv[argc-1] argv[0] … envp argc argv Stack bottom Stack frame for main Stack top 4

5 University of Washington 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 5 void cleanup(void) { printf("cleaning up\n"); } void fork6() { atexit(cleanup); fork(); exit(0); }

6 University of Washington 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 6

7 University of Washington 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 7

8 University of Washington wait Example 8 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); } HCBye CTBye

9 University of Washington 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 9

10 University of Washington 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 10

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

12 University of Washington Processes Definition: A process is 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: 12

13 University of Washington Processes Definition: A process is 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? 13

14 University of Washington Processes Definition: A process is 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! 14

15 University of Washington 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 15

16 University of Washington 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? 16 FF∙∙∙∙∙∙F 00∙∙∙∙∙∙0

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

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

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

20 University of Washington How would you solve those problems? 20

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

22 University of Washington 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 22

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

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

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

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

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

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

29 University of Washington Memory Hierarchy: Core 2 Duo 29 Disk Main Memory L2 unified cache L1 I-cache L1 D-cache CPUReg 2 B/cycle8 B/cycle16 B/cycle1 B/30 cyclesThroughput: Latency:100 cycles14 cycles3 cyclesmillions ~4 MB 32 KB ~4 GB~500 GB Not drawn to scale L1/L2 cache: 64 B blocks Miss penalty (latency): 33x Miss penalty (latency): 10,000x

30 University of Washington 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? 30

31 University of Washington 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 31

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

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

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

35 University of Washington Address Translation With a Page Table 35 Virtual page number (VPN)Virtual page offset (VPO) Physical page number (PPN) Physical page offset (PPO) Virtual address (VA) Physical address (PA) Valid Physical page number (PPN) Page table base register (PTBR) Page table Page table address for process 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

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

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

38 University of Washington 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 int a[1000]; main () { a[500] = 13; } 80483b7:c7 05 10 9d 04 08 0d movl $0xd,0x8049d10 User ProcessOS exception: page fault Create page and load into memory returns movl 38 Fault Example: Page Fault

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

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

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

42 University of Washington 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! null Memory resident page table (DRAM) Physical memory (DRAM) VP 7 VP 3 Virtual memory (disk) Valid 0 1 1 0 0 1 0 1 Physical page number or disk address PTE 0 PTE 7 PP 0 VP 2 VP 1 PP 3 VP 1 VP 2 VP 4 VP 6 VP 7 VP 3 Virtual address 42

43 University of Washington Why does it work? 43

44 University of Washington 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 meltdown where pages are swapped (copied) in and out continuously 44

45 University of Washington 45


Download ppt "University of Washington Today Lab 4 due Wednesday! HW 4 out today! What a process again?"

Similar presentations


Ads by Google