Page 1Exceptional Control Flow& ProcessesOctober 2, 2008Exceptional Control Flow& ProcessesOctober 2, 2008TopicsTopics Exceptions Processes and context switches Creating and destroying processeslecture-11.ppt15-213“The course that gives CMU its Zip!”–2–15-213, F’08Control 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’08Altering 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 divides by zeroz user hits Ctrl-C at the keyboardz System timer expiresSystem needs mechanisms for System needs mechanisms for ““exceptional control flowexceptional control flow””–4–15-213, F’08Exceptional 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 jumpsPage 2–5–15-213, F’08ExceptionsExceptionsAn 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’08Interrupt VectorsInterrupt Vectors Each type of event has a unique exception number k Index into jump table (a.k.a., interrupt vector) Entry k points to a function (exception handler) Handler k is called each time exception k occursinterruptvector012...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–15-213, F’08Asynchronous 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 Ctrl-C at the keyboardz arrival of a packet from a networkz arrival of data from a disk Hard reset interruptz hitting the reset button Soft reset interruptz hitting Ctrl-Alt-Delete on a PC–8–15-213, F’08Synchronous 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 programPage 3–9–15-213, F’08Trap 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’08Fault 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’08Fault 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 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–15-213, F’08ProcessesProcessesDefinition: 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 (we’ll talk about this in a couple of weeks)Page 4–13–15-213, F’08Logical Control FlowsLogical Control FlowsEach process has its own logical control flow–14–15-213, F’08Concurrent ProcessesConcurrent ProcessesTwo processes Two processes run concurrentlyrun concurrently((are concurrent)are concurrent)if if their flows overlap in timetheir flows overlap in timeOtherwise, they are Otherwise, they are sequentialsequentialExamples:Examples: Concurrent: A & B, A & C Sequential: B & CTimeProcess A Process B Process C–15–15-213, F’08User View of Concurrent ProcessesUser
View Full Document