Exceptional Control Flow I Oct 18, 2001Control flowAltering the Control FlowExceptional control flowSystem context for exceptionsExceptionsInterrupt vectorsAsynchronous exceptions (interrupts)Synchronous exceptionsTrap ExampleFault Example #1Fault Example #2ProcessesLogical control flowsConcurrent processesUser view of concurrent processesContext switchingPrivate address spacesfork: Creating new processesFork Example #1Fork Example #2Fork Example #3Fork Example #4Fork Example #5exit: Destroying processZombiesZombie ExampleNonterminating Child Examplewait: Synchronizing with childrenWait ExampleWaitpidWait/Waitpid Example Outputsexec: Running new programsSummarizingExceptional Control Flow IOct 18, 2001Topics•Exceptions•Process context switches•Creating and destroying processesclass16.ppt15-213“The course that gives CMU its Zip!”CS 213 F’01– 2 –class16.pptControl flow<startup>inst1inst2inst3…instn<shutdown>From startup to shutdown, a CPU simply reads and executes (interprets) a sequence of instructions, one at a time.This sequence is the system’s physical control flow (or flow of control).Physical control flowTimeCS 213 F’01– 3 –class16.pptAltering the Control FlowWe’ve discussed two mechanisms for changing the control flow:•Jumps and branches•Call and return using the stack discipline.•Both react to changes in program state.Insufficient for a useful system•difficult for the CPU to react to changes in system state. –data arrives from a disk or a network adapter.–instruction divides by zero–user hits ctl-c at the keyboard–system timer expiresSystem needs mechanisms for “exceptional control flow”CS 213 F’01– 4 –class16.pptExceptional control flowMechanisms for exceptional control flow exists at all levels of a computer system.Low level mechanism:•exceptions –change in control flow in response to a system event (i.e., change in system state)•Implemented as a combination of both hardware and OS softwareHigher level mechanisms:•process context switch•signals•nonlocal jumps (setjmp/longjmp)•Implemented by either:–OS software (context switch and signals).–C language runtime library: nonlocal jumps.CS 213 F’01– 5 –class16.pptSystem context for exceptionsLocal/IO BusLocal/IO BusMemoryMemoryNetworkadapterNetworkadapterIDE diskcontrollerIDE diskcontrollerVideoadapterVideoadapterDisplayDisplayNetworkNetworkProcessorProcessorInterruptcontrollerInterruptcontrollerSCSIcontrollerSCSIcontrollerSCSI busSCSI busSerial port controllerSerial port controllerParallel portcontrollerParallel portcontrollerKeyboardcontrollerKeyboardcontrollerKeyboardKeyboardMouseMousePrinterPrinterModemModemdiskdisk CDROMCS 213 F’01– 6 –class16.pptExceptionsAn exception is a transfer of control to the OS in response to some event (i.e., change in processor state)User Process OSexceptionexception processingby exception handlerexception return (optional)event currentnextCS 213 F’01– 7 –class16.pptInterrupt vectors1. Each type of event has a unique exception number k2. Jump table (interrupt vector)entry k points to a function(exception handler).3. Handler k is called each timeexception k occurs. interruptvector012...n-1code for exception handler 0code for exception handler 0code for exception handler 1code for exception handler 1code forexception handler 2code forexception handler 2code for exception handler n-1code for exception handler n-1...Exception numbersCS 213 F’01– 8 –class16.pptAsynchronous exceptions (interrupts)Caused by events (changes in state) external to the processor•Indicated by setting the processor’s interrupt pin•handler returns to “next” instruction.Examples:•I/O interrupts–hitting ctl-c at the keyboard–arrival of a packet from a network–arrival of a data sector from a disk•Hard reset interrupt–hitting the reset button•Soft reset interrupt–hitting ctl-alt-delete on a PCCS 213 F’01– 9 –class16.pptSynchronous exceptionsCaused by events (changes in state) that occur as a result of executing an instruction:•Traps–intentional–returns control to “next” instruction–Examples: system calls, breakpoint traps•Faults–unintentional but possibly recoverable –either re-executes faulting (“current”) instruction or aborts.–Examples: page faults (recoverable), protection faults (unrecoverable).•Aborts–unintentional and unrecoverable–aborts current program– Examples: parity error, machine check.CS 213 F’01– 10 –class16.pptTrap ExampleUser Process OSexceptionOpen filereturnintpopOpening a File•User calls open(filename, options)–Function open executes system call instruction int•OS must find or create file, get it ready for reading or writing•Returns integer file descriptor0804d070 <__libc_open>: . . . 804d082: cd 80 int $0x80 804d084: 5b pop %ebx . . .CS 213 F’01– 11 –class16.pptFault Example #1User Process OSpage faultCreate page and load into memoryreturnevent movlMemory Reference•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•Successful on second tryint a[1000];main (){ a[500] = 13;} 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10CS 213 F’01– 12 –class16.pptFault Example #2User Process OSpage faultDetect invalid addressevent movlMemory Reference•User writes to memory location•Address is not valid•Page handler detects invalid address•Sends SIGSEG signal to user process•User process exits with “segmentation fault”int a[1000];main (){ a[5000] = 13;} 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360Signal processCS 213 F’01– 13 –class16.pptProcessesDef: A process is an instance of a running program.•One of the most profound ideas in computer science.Process provides each program with two key abstractions:•Logical control flow–gives each program the illusion that it has exclusive use of the CPU.•Private address space–gives each program the illusion that has exclusive use of main memory.How is this illusion maintained?•Process executions interleaved (multitasking)•Address spaces managed by virtual memory systemCS 213 F’01– 14 –class16.pptLogical control flowsTimeProcess A Process B Process CEach process has its own logical control flowCS 213 F’01– 15 –class16.pptConcurrent processesTwo processes run
View Full Document