Exceptional Control FlowPart IMarch 4, 2008TopicsTopicsExceptionsProcess context switchesCreating and destroying processesclass14.ppt15-213“The course that gives CMU its Zip!”215-213, S’08Control Flow<startup>inst1inst2inst3…instn<shutdown>Computers do only one thing:Computers 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 flowTime315-213, S’08Altering the Control FlowUp to now: two mechanisms for changing control Up to now: two mechanisms for changing control flow:flow:1. Jumps and branches2. Call and return using the stack discipline.Both react to choices the program makes voluntarilyInsufficient for a useful systemInsufficient for a useful systemDifficult for the CPU to react to changes in system state. Data arrives from a disk or a network adapter.Instruction divides by zeroUser hits Control-C at the keyboardInterval timer expires (time for this processor to run another program)System needs mechanisms for “exceptional control System needs mechanisms for “exceptional control flow”flow”415-213, S’08Exceptional Control FlowMechanisms for exceptional control flow exists at all levels of a computer system.Low 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 switchSignalsNon-local jumps (setjmp()/longjmp())Implemented by either:OS software (context switch and signals).C language runtime library: non-local jumps.515-213, S’08System context for exceptionsLocal/IO BusMemoryNetworkadapterIDE diskcontrollerVideoadapterDisplayNetworkProcessorInterruptcontrollerSCSIcontrollerSCSI busSerial port controllersParallel portcontrollerTimerKeyboard Mouse PrinterModemdiskdisk CDROMUSB PortsSuper I/O Chip615-213, S’08ExceptionsAn An exceptionexception is a transfer of control to the OS in is a transfer of control to the OS in response to some response to some eventevent (i.e., change in processor (i.e., change in processor state)state)User Process OSexceptionexception processingby exception handlerexception return (optional)event currentnext715-213, S’08Interrupt 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 1code forexception handler 2code for exception handler n-1...Exception numbers815-213, S’08Asynchronous Exceptions (Interrupts)Caused by events external to the processorCaused by events external to the processorIndicated by setting the processor’s interrupt pinhandler returns to “next” instruction.Examples:Examples:I/O interruptshitting ctl-c at the keyboardarrival of a packet from a networkarrival of a data sector from a diskHard reset interrupthitting the reset buttonSoft reset interrupthitting ctl-alt-delete on a PC915-213, S’08Synchronous ExceptionsCaused by events that occur as a result of executing Caused by events that occur as a result of executing an instruction:an instruction:TrapsIntentionalExamples: system calls, breakpoint traps, special instructionsReturns control to “next” instructionFaultsUnintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptions.Either re-executes faulting (“current”) instruction or aborts.Abortsunintentional and unrecoverableExamples: parity error, machine check.Aborts current program1015-213, S’08Trap ExampleUser Process OSexceptionOpen filereturnintpopOpening a FileOpening a FileUser calls open(filename, options)Function open executes system call instruction intOS 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 . . .1115-213, S’08Fault 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,0x8049d101215-213, S’08Fault Example #2User Process OSpage faultDetect invalid addressevent movlInvalid Memory ReferenceInvalid Memory 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 process1315-213, S’08ProcessesDefinition: A Definition: A processprocess is an instance of a running is an instance of a running program.program.One of the most profound ideas in computer science.Not the same as “program” or “processor”Process provides each program with two key Process provides each program with two key abstractions:abstractions:Logical control flowEach program seems to have exclusive use of the CPU.Private address spaceEach program seems to have exclusive use of main memory.How are these Illusions maintained?How are these Illusions maintained?Process executions interleaved (multitasking)Address spaces managed by virtual memory system1415-213, S’08Logical Control FlowsTimeProcess A Process B Process CEach process has its own logical control flow1515-213, S’08Concurrent ProcessesTwo processes Two processes run concurrentlyrun concurrently ( (are concurrent)are concurrent) if if their flows overlap in time.their flows overlap in time.Otherwise, they are Otherwise, they are sequential.sequential. Examples:Examples:Concurrent: A & B, A & CSequential: B & CTimeProcess A Process B Process C1615-213, S’08User View of Concurrent ProcessesControl flows for concurrent processes are physically Control flows for concurrent processes are physically
View Full Document