Exceptional Control FlowPart IOct. 17, 2002Exceptional Control FlowPart IOct. 17, 2002TopicsTopicsn Exceptionsn Process context switchesn Creating and destroying processesclass16.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’02Control FlowControl Flow<startup>inst1inst2inst3…instn<shutdown>Computers do Only One ThingComputers do Only One Thingn From startup to shutdown, a CPU simply reads and executes(interprets) a sequence of instructions, one at a time.n This sequence is the system’s physical control flow (or flowof control).Physical control flowTime– 3 –15-213, F’02Altering the Control FlowAltering the Control FlowUp to Now: two mechanisms for changing control flow:Up to Now: two mechanisms for changing control flow:n Jumps and branchesn Call and return using the stack discipline.n Both react to changes in program state.Insufficient for a useful systemInsufficient for a useful systemn Difficult for the CPU to react to changes in system state.l data arrives from a disk or a network adapter.l Instruction divides by zerol User hits ctl-c at the keyboardl System timer expiresSystem needs mechanisms for “exceptional controlSystem needs mechanisms for “exceptional controlflow”flow”– 4 –15-213, F’02Exceptional Control FlowExceptional Control Flown Mechanisms for exceptional control flow exists at all levelsof a computer system.Low level MechanismLow level Mechanismn exceptionsl change in control flow in response to a system event (i.e.,change in system state)n Combination of hardware and OS softwareHigher Level MechanismsHigher Level Mechanismsn Process context switchn Signalsn Nonlocal jumps (setjmp/longjmp)n Implemented by either:l OS software (context switch and signals).l C language runtime library: nonlocal jumps.– 5 –15-213, F’02System context for exceptionsSystem context for exceptionsLocal/IO BusLocal/IO BusMemoryMemoryNetworkadapterNetworkadapterIDE diskcontrollerIDE diskcontrollerVideoadapterVideoadapterDisplayDisplayNetworkNetworkProcessorProcessorInterruptcontrollerInterruptcontrollerSCSIcontrollerSCSIcontrollerSCSI busSCSI busSerial port controllerSerial port controllerParallel portcontrollerParallel portcontrollerKeyboardcontrollerKeyboardcontrollerKeyboardKeyboardMouseMousePrinterPrinterModemModemdiskdisk CDROM– 6 –15-213, F’02ExceptionsAn An exceptionexception is a transfer of control to the OS in response is a transfer of control to the OS in responseto some to some eventevent (i.e., change in processor state) (i.e., change in processor state)User Process OSexceptionexception processingby exception handlerexception return (optional)eventcurrentnext– 7 –15-213, F’02Interrupt VectorsInterrupt Vectorsn Each type of event has aunique exception number kn Index into jump table (a.k.a.,interrupt vector)n Jump table entry k points toa function (exceptionhandler).n Handler k is called eachtime exception 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 numbers– 8 –15-213, F’02Asynchronous Exceptions (Interrupts)Asynchronous Exceptions (Interrupts)Caused by events external to the processorCaused by events external to the processorn Indicated by setting the processor’s interrupt pinn handler returns to “next” instruction.Examples:Examples:n I/O interruptsl hitting ctl-c at the keyboardl arrival of a packet from a networkl arrival of a data sector from a diskn Hard reset interruptl hitting the reset buttonn Soft reset interruptl hitting ctl-alt-delete on a PC– 9 –15-213, F’02Synchronous ExceptionsSynchronous ExceptionsCaused by events that occur as a result of executing anCaused by events that occur as a result of executing aninstruction:instruction:n Trapsl Intentionall Examples: system calls, breakpoint traps, special instructionsl Returns control to “next” instructionn Faultsl Unintentional but possibly recoverablel Examples: page faults (recoverable), protection faults(unrecoverable).l Either re-executes faulting (“current”) instruction or aborts.n Abortsl unintentional and unrecoverablel Examples: parity error, machine check.l Aborts current program– 10 –15-213, F’02Trap ExampleTrap ExampleUser Process OSexceptionOpen filereturnintpopOpening a FileOpening a Filen User calls open(filename, options)l Function open executes system call instruction intn OS must find or create file, get it ready for reading or writingn Returns integer file descriptor0804d070 <__libc_open>: . . . 804d082: cd 80 int $0x80 804d084: 5b pop %ebx . . .– 11 –15-213, F’02Fault Example #1Fault Example #1User Process OSpage faultCreate page and loadinto memoryreturneventmovlMemory ReferenceMemory Referencen User writes to memory locationn That portion (page) of user’s memoryis currently on diskn Page handler must load page intophysical memoryn Returns to faulting instructionn Successful on second tryint a[1000];main (){ a[500] = 13;} 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10– 12 –15-213, F’02Fault Example #2Fault Example #2User Process OSpage faultDetect invalid addresseventmovlMemory ReferenceMemory Referencen User writes to memory locationn Address is not validn Page handler detects invalid addressn Sends SIGSEG signal to user processn User process exits with “segmentation fault”int a[1000];main (){ a[5000] = 13;} 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360Signal process– 13 –15-213, F’02ProcessesProcessesDef: A Def: A processprocess is an instance of a running program. is an instance of a running program.n One of the most profound ideas in computer science.n Not the same as “program” or “processor”Process provides each program with two keyProcess provides each program with two keyabstractions:abstractions:n Logical control flowl Each program seems to have exclusive use of the CPU.n Private address spacel Each program seems to have exclusive use of main memory.How are these Illusions maintained?How are these Illusions maintained?n Process executions interleaved (multitasking)n Address spaces managed by virtual memory system– 14 –15-213, F’02Logical Control FlowsLogical Control FlowsTimeProcess A Process B Process CEach process has its own logical control flow– 15 –15-213,
View Full Document