Exceptional Control FlowControl FlowAltering the Control FlowSlide 4ExceptionsInterrupt VectorsAsynchronous Exceptions (Interrupts)Synchronous ExceptionsTrap ExampleFault Example #1Fault Example #2Summarizing ExceptionsECF Exists at All Levels of a SystemShell ProgramsSimple Shell eval FunctionProblem with Simple Shell ExampleSignalsSignal ConceptsSignal Concepts (cont.)Slide 27Signal ConceptsReceiving SignalsProcess GroupsSending Signals with killSending Signals From the KeyboardExample of ctrl-c and ctrl-zSlide 34Default ActionsInstalling Signal HandlersSignal Handling ExampleSignal Handler FunkinessLiving With Nonqueuing SignalsSummaryExceptional Control FlowExceptional Control FlowTopicsTopicsExceptionsProcess hierarchyShellsSignalsexcept1.pptCS 105“Tour of the Black Holes of Computing”– 2 –CS 105Control FlowControl Flow<startup>inst1inst2inst3…instn<shutdown>Computers Do Only One ThingComputers Do Only One ThingFrom 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 flowTime– 3 –CS 105Altering the Control FlowAltering the Control FlowUp to now: two mechanisms for changing control flow:Up to now: two mechanisms for changing control flow:Jumps and branches - react to changes in program stateCall and return using stack discipline - react to program stateInsufficient for a useful systemInsufficient for a useful systemDifficult for the CPU to react to other changes in system state Data arrives from a disk or a network adapterInstruction divides by zeroUser hits control-C at the keyboardSystem needs mechanisms for “exceptional control System needs mechanisms for “exceptional control flow”flow”– 4 –CS 105Exceptional Control FlowExceptional Control FlowMechanisms for exceptional control flow exist at all levels of a computer systemLow-Level MechanismLow-Level MechanismExceptions Change in control flow in response to a system event (i.e., change in system state)Combination of hardware and OS softwareHigher-Level MechanismsHigher-Level MechanismsProcess context switchSignalsNonlocal jumps (setjmp/longjmp)—ignored in this courseImplemented by either:OS software (context switch and signals)C language runtime library: nonlocal jumps– 5 –CS 105ExceptionsAn An exceptionexception is a transfer of control to the OS in response is a transfer of control to the OS in response to some to some eventevent (i.e., change in processor state) (i.e., change in processor state)User Process OSexceptionexception processingby exception handlerexception return (optional)event currentnextThink of it as a hardware-initiated function callThink of it as a hardware-initiated function call– 6 –CS 105Interrupt VectorsInterrupt VectorsEach type of event has a unique exception number kIndex into jump table (a.k.a., interrupt vector)Jump table entry k points to a function (exception handler).Handler k is called each time 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– 7 –CS 105Asynchronous Exceptions (Interrupts)Asynchronous Exceptions (Interrupts)Caused by events external to processorCaused by events external to processorIndicated by setting the processor’s interrupt pin(s)Handler returns to “next” instruction.Examples:Examples:I/O interruptsHitting control-C (or any key) at the keyboardArrival of packet from networkArrival of data sector from diskHard-reset interruptHitting reset buttonSoft-reset interruptHitting control-alt-delete on a PC– 8 –CS 105Synchronous ExceptionsSynchronous ExceptionsCaused by events that occur as result of executing an Caused by events that occur as result of executing an instruction:instruction:TrapsIntentionalExamples: system calls, breakpoint traps, special instructionsReturns control to “next” instructionFaultsUnintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable)Either re-executes faulting (“current”) instruction or abortsAbortsUnintentional and unrecoverableExamples: parity error, machine checkAborts current program or entire OS– 9 –CS 105Trap ExampleTrap ExampleUser Process OSexceptionOpen filereturnintpopOpening a FileOpening a FileUser calls open(filename, options)Function open executes system-call instruction: int $0x80OS must find or create file, get it ready for reading or writingReturns integer file descriptor0804d070 <__libc_open>: . . . 804d082: cd 80 int $0x80 804d084: 5b pop %ebx . . .– 10 –CS 105Fault Example #1Fault Example #1User Process OSpage faultCreate page and load into memoryreturnevent movlMemory ReferenceMemory ReferenceUser writes to memory locationThat portion (page) of user’s memory is currently on diskPage handler must load page into physical memoryReturns to faulting instructionSuccessful on second tryint a[1000];main (){ a[500] = 13;} 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10– 11 –CS 105Fault Example #2Fault Example #2User Process OSpage faultDetect invalid addressevent movlMemory ReferenceMemory ReferenceUser writes to memory locationAddress is not validPage handler detects invalid addressSends SIGSEGV signal to user processUser process exits with “segmentation fault”int a[1000];main (){ a[5000] = 13;} 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360Signal process– 12 –CS 105Summarizing ExceptionsSummarizing ExceptionsExceptionsExceptionsEvents that require nonstandard control flowGenerated externally (interrupts) or internally (traps and faults)OS decides how to handle– 13 –CS 105ECF Exists at All Levelsof a SystemECF Exists at All Levelsof a SystemExceptionsExceptionsHardware and operating system kernel softwareConcurrent processesConcurrent processesHardware timer and kernel softwareSignalsSignalsKernel softwareNon-local jumps (ignored in this class)Non-local jumps (ignored in this class)Application codeEvil in CC++/Python throw/catch– 21 –CS
View Full Document