Exceptional Control Flow: Exceptional Control Flow Exceptions and Processes Processes
Total Page:16
File Type:pdf, Size:1020Kb
Carnegie Mellon Carnegie Mellon Today Exceptional Control Flow: Exceptional Control Flow Exceptions and Processes Processes 15‐213 / 18‐213: Introduction to Computer Systems 13th Lecture, Feb 26, 2013 Instructors: Seth Copen Goldstein, Anthony Rowe, and Greg Kesden 1 2 Carnegie Mellon Carnegie Mellon Control Flow Altering the Control Flow Processors do only one thing: Up to now: two mechanisms for changing control flow: . From startup to shutdown, a CPU simply reads and executes . Jumps and branches (interprets) a sequence of instructions, one at a time . Call and return . This sequence is the CPU’s control flow (or flow of control) Both react to changes in program state Physical control flow Insufficient for a useful system: Difficult to react to changes in system state <startup> . data arrives from a disk or a network adapter inst1 . instruction divides by zero inst Time 2 . user hits Ctrl‐C at the keyboard inst3 . System timer expires … instn <shutdown> System needs mechanisms for “exceptional control flow” 3 4 Carnegie Mellon Carnegie Mellon Exceptional Control Flow ECF Exists at All Levels of a System Exists at all levels of a computer system Exceptions Low level mechanisms . Hardware and operating system kernel software . Exceptions Process Context Switch This Lecture . change in control flow in response to a system event . Hardware timer and kernel software (i.e., change in system state) Signals . Combination of hardware and OS software . Kernel software and application software Higher level mechanisms Nonlocal jumps . Process context switch Next Lecture . Application code . Signals What about . Nonlocal jumps: setjmp()/longjmp() try/catch/throw? . Implemented by either: . OS software (context switch and signals) . C language runtime library (nonlocal jumps) 5 6 Carnegie Mellon Carnegie Mellon Exceptions Exception Tables An exception is a transfer of control to the OS in response to Exception some event (i.e., change in processor state) numbers code for Each type of event has a User Process OS exception handler 0 unique exception number k Exception code for event I_current exception Table exception handler 1 k = index into exception table I_next exception processing 0 (a.k.a. interrupt vector) by exception handler 1 code for almost 2 • return to I_current ... exception handler 2 Handler k is called each time • return to I_next n-1 ... exception k occurs • abort code for exception handler n‐1 Examples: div by 0, arithmetic overflow, page fault, I/O request completes, Ctrl‐C 7 8 Carnegie Mellon Carnegie Mellon Asynchronous Exceptions (Interrupts) Synchronous Exceptions Caused by events that occur as a result of executing an Caused by events external to the processor instruction: . Indicated by setting the processor’s interrupt pin . Traps . Handler returns to “next” instruction . Intentional . Examples: system calls, breakpoint traps, special instructions Examples: . Returns control to “next” instruction . I/O interrupts . Faults . hitting Ctrl‐C at the keyboard . Unintentional but possibly recoverable . arrival of a packet from a network . Examples: page faults (recoverable), protection faults . arrival of data from a disk (unrecoverable), floating point exceptions . Hard reset interrupt . Either re‐executes faulting (“current”) instruction or aborts . hitting the reset button . Aborts . Soft reset interrupt . unintentional and unrecoverable . hitting Ctrl‐Alt‐Delete on a PC . Examples: parity error, machine check . Aborts current program 9 10 Carnegie Mellon Carnegie Mellon Trap Example: Opening File Fault Example: Page Fault int a[1000]; User calls: open(filename, options) User writes to memory location main () { Function open executes system call instruction int That portion (page) of user’s memory a[500] = 13; 0804d070 <__libc_open>: is currently on disk } . 804d082: cd 80 int $0x80 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10 804d084: 5b pop %ebx . User Process OS User Process OS exception: page fault movl exception Create page and int returns pop Different privilege load into memory open file levels! returns Page handler must load page into physical memory Returns to faulting instruction Different privilege OS must find or create file, get it ready for reading or writing Successful on second try levels! Returns integer file descriptor 11 12 Carnegie Mellon Carnegie Mellon Fault Example: Invalid Memory Reference Exception Table IA32 (Excerpt) int a[1000]; main () { Exception Number Description Exception Class a[5000] = 13; 0 Divide error Fault } 13 General protection fault Fault 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 14 Page fault Fault 18 Machine check Abort User Process OS 32‐127 OS‐defined Interrupt or trap exception: page fault 128 (0x80) System call Trap movl 129‐255 OS‐defined Interrupt or trap detect invalid address signal process Check Table 6‐1: Page handler detects invalid address http://download.intel.com/design/processor/manuals/253665.pdf Sends SIGSEGV signal to user process Different privilege User process exits with “segmentation fault” levels! 13 14 Carnegie Mellon Carnegie Mellon Altering the Control Flow (revisited) Today Up to now: two mechanisms for changing control flow: Exceptional Control Flow . Jumps and branches Is there an alternative? Processes . Call and return - For general purpose processors? Both react to changes in program state - For embedded processors? Insufficient for a useful system: Difficult to react to changes in system state . data arrives from a disk or a network adapter . instruction divides by zero . user hits Ctrl‐C at the keyboard . System timer expires System needs mechanisms for “exceptional control flow” 15 16 Carnegie Mellon Carnegie Mellon Processes Concurrent Processes Definition: A process is an instance of a running program. Two processes run concurrently (are concurrent) if their . One of the most profound ideas in computer science flows overlap in time . Not the same as “program” or “processor” Otherwise, they are sequential Examples (running on single core): Process provides each program with two key abstractions: . Concurrent: A & B, A & C . Logical control flow . Sequential: B & C . Each program seems to have exclusive use of the CPU . Private virtual address space Process AProcess BProcess C . Each program seems to have exclusive use of main memory Time How are these Illusions maintained? . Process executions interleaved (multitasking) or run on separate cores . Address spaces managed by virtual memory system . we’ll talk about this in a couple of weeks 17 18 Carnegie Mellon Carnegie Mellon User View of Concurrent Processes Context Switching Processes are managed by a shared chunk of OS code Control flows for concurrent processes are physically called the kernel disjoint in time . Important: the kernel is not a separate process, but rather runs as part of some user process However, we can think of concurrent processes as Control flow passes from one process to another via a running in parallel with each other context switch Process AProcess B Process AProcess BProcess C Time user code kernel code context switch Time user code kernel code context switch user code 19 20 Carnegie Mellon Carnegie Mellon fork: Creating New Processes Understanding fork Process n Child Process m int fork(void) pid_t pid = fork(); pid_t pid = fork(); . creates a new process (child process) that is identical to the calling if (pid == 0) { if (pid == 0) { process (parent process) printf("hello from child\n"); printf("hello from child\n"); } else { } else { . returns 0 to the child process printf("hello from parent\n"); printf("hello from parent\n"); } } . returns child’s pid (process id) to the parent process pid_t pid = fork(); pid_t pid = fork(); pid_t pid = fork(); if (pid == 0) { if (pid == 0) { if (pid == 0) { pid = m printf("hello from child\n"); pid = 0 printf("hello from child\n"); printf("hello from child\n"); } else { } else { printf("hello from parent\n"); printf("hello from parent\n"); } else { } } printf("hello from parent\n"); } pid_t pid = fork(); pid_t pid = fork(); if (pid == 0) { if (pid == 0) { printf("hello from child\n"); printf("hello from child\n"); Fork is interesting (and often confusing) because } else { } else { printf("hello from parent\n"); printf("hello from parent\n"); it is called once but returns twice } } hello from parent Which one is first? hello from child 21 22 Carnegie Mellon Carnegie Mellon Fork Example #1 Fork Example #2 Parent and child both run same code Two consecutive forks . Distinguish parent from child by return value from fork void fork2() Start with same state, but each has private copy { . Including shared output file descriptor printf("L0\n"); Bye . Relative ordering of their print statements undefined fork(); L1 Bye printf("L1\n"); Bye fork(); void fork1() L0 L1 Bye { printf("Bye\n"); int x = 1; } pid_t pid = fork(); if (pid == 0) { printf("Child has x = %d\n", ++x); } else { printf("Parent has x = %d\n", --x); } printf("Bye from process %d with x = %d\n", getpid(), x); } 23 24 Carnegie Mellon Carnegie Mellon Fork Example #3 Fork Example #4 Three consecutive forks Nested forks in parent void fork3() Bye void fork4() { { L2 Bye printf("L0\n"); printf("L0\n"); fork(); Bye if (fork() != 0) { printf("L1\n"); L1 L2 Bye printf("L1\n"); Bye fork(); Bye if (fork() != 0) { printf("L2\n"); L2 Bye printf("L2\n"); Bye fork(); fork(); Bye printf("Bye\n"); Bye } L0 L1 L2 Bye } L0 L1 L2 Bye } printf("Bye\n"); } 25 26 Carnegie Mellon Carnegie Mellon Fork Example #5 exit: Ending a process Nested forks in children void exit(int status) . exits a process void fork5() . Normally return with status 0 { printf("L0\n"); . atexit() registers functions to be executed upon exit if (fork() == 0) { printf("L1\n"); Bye if (fork() == 0) { L2 Bye void cleanup(void) { printf("L2\n"); printf("cleaning up\n"); L1 Bye fork(); } } L0 Bye } void fork6() { printf("Bye\n“); atexit(cleanup); } fork(); exit(0); } 27 28 Carnegie Mellon Carnegie Mellon void fork7() Zombies Zombie { if (fork() == 0) { Idea /* Child */ Example printf("Terminating Child, PID = %d\n", . When process terminates, still consumes system resources getpid()); exit(0); .