Harvey Mudd CS 105 - Exceptional Control Flow

Unformatted text preview:

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 FlowTopicsTopicsExceptionsProcess hierarchyShellsSignalsCS 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 thing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 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 stateCall and return using stack discipline - react to program stateInsufficient for a useful systemInsufficient for a useful systemDifficult for the CPU to react to other changes in system state Data arrives from a disk or a network adapterInstruction divides by zeroUser 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 FlowMechanisms for exceptional control flow exist at all levels of a computer systemLow-Level MechanismLow-Level MechanismExceptions 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 MechanismsProcess context switchSignalsNonlocal jumps (setjmp/longjmp)—ignored in this courseImplemented 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 VectorsEach type of event has a unique exception number kIndex 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 processorIndicated by setting the processor’s interrupt pin(s)Handler returns to “next” instruction.Examples:Examples:I/O interruptsHitting control-C (or any key) at the keyboardArrival of packet from networkArrival of data sector from diskHard-reset interruptHitting reset buttonSoft-reset interruptHitting 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:TrapsIntentionalExamples: system calls, breakpoint traps, special instructionsReturns control to “next” instructionFaultsUnintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable)Either re-executes faulting (“current”) instruction or abortsAbortsUnintentional and unrecoverableExamples: parity error, machine checkAborts current program or entire OS– 9 –CS 105Trap ExampleTrap ExampleUser Process OSexceptionOpen filereturnintpopOpening a FileOpening a FileUser calls open(filename, options)Function open executes system-call instruction: int $0x80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 . . .– 10 –CS 105Fault Example #1Fault Example #1User Process OSpage faultCreate page and load into memoryreturnevent movlMemory ReferenceMemory 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,0x8049d10– 11 –CS 105Fault Example #2Fault Example #2User Process OSpage faultDetect invalid addressevent movlMemory ReferenceMemory ReferenceUser writes to memory locationAddress is not validPage handler detects invalid addressSends SIGSEGV 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 process– 12 –CS 105Summarizing ExceptionsSummarizing ExceptionsExceptionsExceptionsEvents that require nonstandard control flowGenerated 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 SystemExceptionsExceptionsHardware and operating system kernel softwareConcurrent processesConcurrent processesHardware timer and kernel softwareSignalsSignalsKernel softwareNon-local jumps (ignored in this class)Non-local jumps (ignored in this class)Application codeEvil in CC++/Python throw/catch– 21 –CS 105Shell


View Full Document

Harvey Mudd CS 105 - Exceptional Control Flow

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Download Exceptional Control Flow
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exceptional Control Flow and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exceptional Control Flow 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?