1 / 52

Introduction to Computer Systems 15-213/18-243, spring 2009 16 th Lecture, Mar. 17 th

Introduction to Computer Systems 15-213/18-243, spring 2009 16 th Lecture, Mar. 17 th. Instructors: Gregory Kesden and Markus Püschel. Signals. Kernel → Process Process → Process (using kill ) 32 types of signals Sent by updating bit in pending vector

anana
Download Presentation

Introduction to Computer Systems 15-213/18-243, spring 2009 16 th Lecture, Mar. 17 th

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. Introduction to Computer Systems15-213/18-243, spring 200916th Lecture, Mar. 17th Instructors: Gregory Kesden and Markus Püschel

  2. Signals • Kernel → Process • Process → Process (using kill) • 32 types of signals • Sent by updating bit in pending vector • You can write your own signal handlers

  3. Signal Handlers as Concurrent Flows Process A Process B Signal delivered user code (main) Icurr context switch kernel code user code (main) context switch kernel code Signal received user code (handler) kernel code Inext user code (main)

  4. Today • More on signals • Long jumps • Virtual memory (VM) • Overview and motivation • VM as tool for caching • VM as tool for memory management • VM as tool for memory protection • Address translation

  5. Sending Signals from the Keyboard • Typing ctrl-c (ctrl-z) sends a SIGINT (SIGTSTP) to every job in the foreground process group. • SIGINT – default action is to terminate each process • SIGTSTP – default action is to stop (suspend) each process Shell pid=10 pgid=10 Fore- ground job Back- ground job #1 Back- ground job #2 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 Background process group 32 Background process group 40 Child Child pid=21 pgid=20 pid=22 pgid=20 Foreground process group 20

  6. Example of ctrl-c and ctrl-z STAT (process state) Legend: First letter: S: sleeping T: stopped R: running Second letter: s: session leader +: foreground proc group See “man ps” for more details bluefish> ./forks 17 Child: pid=28108 pgrp=28107 Parent: pid=28107 pgrp=28107 <types ctrl-z> Suspended bluefish> ps w PID TTY STAT TIME COMMAND 27699 pts/8 Ss 0:00 -tcsh 28107 pts/8 T 0:01 ./forks 17 28108 pts/8 T 0:01 ./forks 17 28109 pts/8 R+ 0:00 ps w bluefish> fg ./forks 17 <types ctrl-c> bluefish> ps w PID TTY STAT TIME COMMAND 27699 pts/8 Ss 0:00 -tcsh 28110 pts/8 R+ 0:00 ps w

  7. Signal Handler Funkiness intccount = 0; void child_handler(int sig) { intchild_status; pid_tpid = wait(&child_status); ccount--; printf("Received signal %d from process %d\n", sig, pid); } void fork14() { pid_tpid[N]; inti, child_status; ccount = N; signal(SIGCHLD, child_handler); for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) { sleep(1); /* deschedule child */ exit(0); /* Child: Exit */ } while (ccount > 0) pause(); /* Suspend until signal occurs */ } • Pending signals are not queued • For each signal type, just have single bit indicating whether or not signal is pending • Even if multiple processes have sent this signal

  8. Living With Nonqueuing Signals • Must check for all terminated jobs • Typically loop with wait void child_handler2(int sig) { int child_status; pid_t pid; while ((pid = waitpid(-1, &child_status, WNOHANG)) > 0) { ccount--; printf("Received signal %d from process %d\n", sig, pid); } } void fork15() { . . . signal(SIGCHLD, child_handler2); . . . }

  9. Signal Handler Funkiness (Cont.) • Signal arrival during long system calls (say a read) • Signal handler interrupts read() call • Linux: upon return from signal handler, the read() call is restarted automatically • Some other flavors of Unix can cause the read()call to fail with an EINTERerror number (errno)in this case, the application program can restart the slow system call • Subtle differences like these complicate the writing of portable code that uses signals

  10. A Program That Reacts toExternally Generated Events (Ctrl-c) #include <stdlib.h> #include <stdio.h> #include <signal.h> void handler(int sig) { printf("You think hitting ctrl-c will stop the bomb?\n"); sleep(2); printf("Well..."); fflush(stdout); sleep(1); printf("OK\n"); exit(0); } main() { signal(SIGINT, handler); /* installs ctrl-c handler */ while(1) { } }

  11. A Program That Reacts to Internally Generated Events #include <stdio.h> #include <signal.h> int beeps = 0; /* SIGALRM handler */ void handler(int sig) { printf("BEEP\n"); fflush(stdout); if (++beeps < 5) alarm(1); else { printf("BOOM!\n"); exit(0); } } main() { signal(SIGALRM, handler); alarm(1); /* send SIGALRM to process in1 second */ while (1) { /* handler returns here */ } } linux> a.out <What happens??>

  12. A Program That Reacts to Internally Generated Events #include <stdio.h> #include <signal.h> int beeps = 0; /* SIGALRM handler */ void handler(int sig) { printf("BEEP\n"); fflush(stdout); if (++beeps < 5) alarm(1); else { printf("BOOM!\n"); exit(0); } } main() { signal(SIGALRM, handler); alarm(1); /* send SIGALRM to process in1 second */ while (1) { /* handler returns here */ } } linux> a.out BEEP BEEP BEEP BEEP BEEP BOOM! bass>

  13. Summary • Signals provide process-level exception handling • Can generate from user programs • Can define effect by declaring signal handler • Some caveats • Very high overhead • >10,000 clock cycles • Only use for exceptional conditions • Don’t have queues • Just one bit for each pending signal type

  14. Today • More on signals • Long jumps • Virtual memory (VM) • Overview and motivation • VM as tool for caching • VM as tool for memory management • VM as tool for memory protection • Address translation

  15. Nonlocal Jumps: setjmp/longjmp • Powerful (but dangerous) user-level mechanism for transferring control to an arbitrary location • Controlled to way to break the procedure call / return discipline • Useful for error recovery and signal handling • intsetjmp(jmp_bufbuf) • Must be called before longjmp • Identifies a return site for a subsequent longjmp • Called once, returns one or more times • Implementation: • Remember where you are by storing the current register context, stack pointer, and PC value in jmp_buf • Return 0

  16. setjmp/longjmp (cont) • void longjmp(jmp_bufbuf, inti) • Meaning: • return from the setjmp remembered by jump buffer bufagain ... • … this time returningi instead of 0 • Called after setjmp • Called once, but never returns • longjmp Implementation: • Restore register context (stack pointer, base pointer, PC value) from jump buffer buf • Set %eax(the return value) to i • Jump to the location indicated by the PC stored in jump bufbuf

  17. setjmp/longjmp Example #include <setjmp.h> jmp_bufbuf; main() { if (setjmp(buf) != 0) { printf("back in main due to an error\n"); else printf("first time through\n"); p1(); /* p1 calls p2, which calls p3 */ } ... p3() { <error checking code> if (error) longjmp(buf, 1) }

  18. Limitations of Nonlocal Jumps • Works within stack discipline • Can only long jump to environment of function that has been called but not yet completed Before longjmp After longjmp buf jmp_bufenv; P1() { if (setjmp(buf)) { /* long jump to here */ } else { P2(); } } P2() { . . . P2(); . . . P3(); } P3() { longjmp(buf, 1); } P1 P1 P2 P2 P2 P3

  19. P1 P1 P2 buf buf P2 At setjmp X P2 returns P1 buf P3 X At longjmp Limitations of Long Jumps (cont.) • Works within stack discipline • Can only long jump to environment of function that has been called but not yet completed jmp_bufenv; P1() { P2(); P3(); } P2() { if (setjmp(buf)) { /* long jump to here */ } } P3() { longjmp(buf, 1); }

  20. Ctrl-c Ctrl-c Putting It All Together: A Program That Restarts Itself When ctrl-c’d #include <stdio.h> #include <signal.h> #include <setjmp.h> sigjmp_bufbuf; void handler(int sig) { siglongjmp(buf, 1); } main() { signal(SIGINT, handler); if (!sigsetjmp(buf, 1)) printf("starting\n"); else printf("restarting\n"); while(1) { sleep(1); printf("processing...\n"); } } bass> a.out starting processing... processing... restarting processing... processing... restarting processing...

  21. Today • More on signals • Long jumps • Virtual memory (VM) • Overview and motivation • VM as tool for caching • VM as tool for memory management • VM as tool for memory protection • Address translation

  22. Virtual Memory (Previous Lectures) • Programs refer to virtual memory addresses • movl (%ecx),%eax • Conceptually very large array of bytes • Each byte has its own address • Actually implemented with hierarchy of different memory types • 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 • But why virtual memory? • Why not physical memory? 00∙∙∙∙∙∙0 FF∙∙∙∙∙∙F

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

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

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

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

  27. Address Spaces • Linear address space: Ordered set of contiguous non-negative integer addresses: {0, 1, 2, 3 … } • Virtual address space: Set of N = 2n virtual addresses {0, 1, 2, 3, …, N-1} • Physical address space: Set of M = 2m physical addresses {0, 1, 2, 3, …, M-1} • Clean distinction between data (bytes) and their attributes (addresses) • Each object can now have multiple addresses • Every byte in main memory: one physical address, one (or more) virtual addresses

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

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

  30. Why Virtual Memory (VM)? • Efficient use of limited main memory (RAM) • Use RAM as a cache for the parts of a virtual address space • some non-cached parts stored on disk • some (unallocated) non-cached parts stored nowhere • Keep only active areas of virtual address space in memory • transfer data back and forth as needed • Simplifies memory management for programmers • Each process gets the same full, private linear address space • Isolates address spaces • One process can’t interfere with another’s memory • because they operate in different address spaces • User process cannot access privileged information • different sections of address spaces have different permissions

  31. Today • More on signals • Long jumps • Virtual memory (VM) • Overview and motivation • VM as tool for caching • VM as tool for memory management • VM as tool for memory protection • Address translation

  32. VM as a Tool for Caching • Virtual memory: array of N = 2ncontiguous bytes • think of the array (allocated part) as being stored on disk • Physical main memory (DRAM) = cache for allocated virtual memory • Blocks are called pages; size = 2p Disk 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 2m-1 VP 2n-p-1 Uncached 2n-1 Virtual pages (VP's) stored on disk Physical pages (PP's) cached in DRAM

  33. Memory Hierarchy: Core 2 Duo Not drawn to scale L1/L2 cache: 64 B blocks ~4 MB ~4 GB ~500 GB L1 I-cache L2 unified cache Main Memory Disk 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): 30x Miss penalty (latency): 10,000x

  34. DRAM Cache Organization • DRAM cache organization driven by the enormous miss penalty • DRAM is about 10x slower than SRAM • Disk is about 10,000xslower 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

  35. Address Translation: Page Tables • A page table is an array of page table entries (PTEs) that maps virtual pages to physical pages. Here: 8 VPs • Per-process kernel data structure in DRAM 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 VP 6 VP 7

  36. Address Translation With a Page Table Virtual address • 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) • Physical page number (PPN) Physical page offset (PPO) Physical address

  37. Page Hit • Page hit: reference to VM word 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

  38. Page Miss • Page miss: reference to VM word 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 VP 6 VP 7

  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? Locality • Virtual memory works because of locality • At any point in time, programs tend to access a set of active virtual pages called the 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

  44. Today • More on signals • Long jumps • Virtual memory (VM) • Overview and motivation • VM as tool for caching • VM as tool for memory management • VM as tool for memory protection • Address translation

  45. VM as a Tool for Memory Management • Key idea: each process has its own virtual address space • It can view memory as a simple linear array • Mapping function scatters addresses through physical memory • Well chosen mappings simplify memory allocation and management Address translation 0 0 Physical Address Space (DRAM) Virtual Address Space for Process 1: VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 0 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... M-1 N-1

  46. VM as a Tool for Memory Management • Memory allocation • Each virtual page can be mapped to any physical page • A virtual page can be stored in different physical pages at different times • Sharing code and data among processes • Map virtual pages to the same physical page (here: PP 6) Address translation 0 0 Physical Address Space (DRAM) Virtual Address Space for Process 1: VP 1 VP 2 PP 2 ... N-1 (e.g., read-only library code) PP 6 0 Virtual Address Space for Process 2: PP 8 VP 1 VP 2 ... ... M-1 N-1

  47. Simplifying Linking and Loading Memory invisible to user code Kernel virtual memory • Linking • Each program has similar virtual address space • Code, stack, and shared libraries always start at the same address • Loading • execve() allocates virtual pages for .text and .data sections = creates PTEs marked as invalid • The .text and .data sections are copied, page by page, on demand by the virtual memory system 0xc0000000 User stack (created at runtime) %esp (stack pointer) Memory-mapped region for shared libraries 0x40000000 brk Run-time heap (created by malloc) Loaded from the executable file Read/write segment (.data, .bss) Read-only segment (.init, .text, .rodata) 0x08048000 Unused 0

  48. Today • More on signals • Long jumps • Virtual memory (VM) • Overview and motivation • VM as tool for caching • VM as tool for memory management • VM as tool for memory protection • Address translation

  49. VM as a Tool for Memory Protection • Extend PTEs with permission bits • Page fault handler checks these before remapping • If violated, send process SIGSEGV (segmentation fault) Physical Address Space SUP READ WRITE Address Process i: VP 0: No Yes No PP 6 VP 1: No Yes Yes PP 4 PP 2 VP 2: Yes Yes Yes PP 2 • • • • PP 4 PP 6 SUP READ WRITE Address Process j: PP 8 No Yes No PP 9 VP 0: • PP 9 Yes Yes Yes PP 6 VP 1: PP 11 No Yes Yes PP 11 VP 2:

  50. Today • More on signals • Long jumps • Virtual memory (VM) • Overview and motivation • VM as tool for caching • VM as tool for memory management • VM as tool for memory protection • Address translation

More Related