Page 1Exceptional Control Flow& ProcessesOctober 10, 2007Exceptional Control Flow& ProcessesOctober 10, 2007TopicsTopics Exceptions Process context switches Creating and destroying processeslecture-13.ppt15-213“The course that gives CMU its Zip!”–2–15-213, F’07Control FlowControl Flow<startup>inst1inst2inst3…instn<shutdown>Processors do only one thing:Processors 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 CPU’s control flow (or flow of control).Physical control flowTime–3–15-213, F’07Altering 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 Call and returnBoth react to changes in program stateInsufficient for a useful systemInsufficient for a useful system Difficult for the CPU to react to changes in system state z data arrives from a disk or a network adapterz Instruction dereferences a NULL or otherwise invalid pointerz User hits ctl-c at the keyboardz System timer expiresSystem needs mechanisms for System needs mechanisms for ““exceptional control exceptional control flowflow””–4–15-213, F’07Exceptional Control FlowExceptional Control Flow Mechanisms for exceptional control flow exists at all levels of a computer system.Low level MechanismLow level Mechanism exceptions z 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) Implemented by either:z OS software (context switch and signals).z C language runtime library: nonlocal jumps.–5–15-213, F’07ExceptionsExceptionsAn An exceptionexceptionis 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 currentnext–6–15-213, F’07Interrupt 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 numbersPage 2–7–15-213, F’07Asynchronous Exceptions (Interrupts)Asynchronous Exceptions (Interrupts)Caused by events external to the processorCaused by events external to the processor Indicated by setting the processor’s interrupt pin handler returns to “next” instructionExamples:Examples: I/O interruptsz hitting ctl-c at the keyboardz arrival of a packet from a networkz arrival of data from a disk Hard reset interruptz hitting the reset button–8–15-213, F’07Synchronous ExceptionsSynchronous ExceptionsCaused by events that occur as a result of executing an Caused by events that occur as a result of executing an instruction:instruction: Trapsz Intentionalz Examples: system calls, breakpoint traps, special instructionsz Returns control to “next” instruction Faultsz Unintentional but possibly recoverable z Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptionsz Either re-executes faulting (“current”) instruction or aborts Abortsz unintentional and unrecoverablez Examples: parity error, machine checkz Aborts current program–9–15-213, F’07Trap ExampleTrap ExampleUser Process OSexceptionOpen filereturnintpopOpening a FileOpening a File User calls open(filename, options)z 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 $0x80804d084: 5b pop %ebx. . .–10–15-213, F’07Fault 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–15-213, F’07Fault Example #2Fault Example #2User Process OSpage faultDetect invalid addressevent movlInvalid Memory ReferenceInvalid Memory 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 process–12–15-213, F’07ProcessesProcessesDefinition: A Definition: A processprocessis an instance of a running program.is an instance of a running 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 flowz Each program seems to have exclusive use of the CPU. Private address spacez Each 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 systemz (topic for later in term)Page 3–13–15-213, F’07Logical Control FlowsLogical Control FlowsEach process has its own logical control flow–14–15-213, F’07Concurrent ProcessesConcurrent 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 & C Sequential: B & CTimeProcess A Process B Process C–15–15-213, F’07User View of Concurrent ProcessesUser View of Concurrent ProcessesControl
View Full Document