Exceptional Control Flow & Processes October 2, 2008Control FlowAltering the Control FlowExceptional Control FlowExceptionsInterrupt VectorsAsynchronous Exceptions (Interrupts)Synchronous ExceptionsTrap ExampleFault Example #1Fault Example #2ProcessesLogical Control FlowsConcurrent ProcessesUser View of Concurrent ProcessesContext Switchingfork: Creating New ProcessesFork Example #1Fork Example #2Fork Example #3Fork Example #4Fork Example #5exit: Ending a processZombiesZombie ExampleNonterminating Child Examplewait: Synchronizing with ChildrenSlide 28wait() Examplewaitpid(): Waiting for a Specific Processexec: Loading and Running ProgramsSlide 32SummarizingSummarizing (cont.)Exceptional Control Flow& ProcessesOctober 2, 2008Exceptional Control Flow& ProcessesOctober 2, 2008TopicsTopicsExceptionsProcesses and context switchesCreating 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 timeThis 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 branchesCall and returnBoth react to changes in program stateInsufficient 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 adapterinstruction divides by zerouser hits Ctrl-C at the keyboardSystem timer expiresSystem needs mechanisms for “exceptional control flow”System needs mechanisms for “exceptional control flow”– 4 –15-213, F’08Exceptional Control FlowExceptional 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 switchSignalsNonlocal jumps: setjmp()/longjmp()implemented by either:OS software (context switch and signals)C language runtime library: nonlocal jumps– 5 –15-213, F’08ExceptionsExceptionsAn 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 currentnext– 6 –15-213, F’08Interrupt VectorsInterrupt VectorsEach type of event has a unique exception number kIndex 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 processorIndicated by setting the processor’s interrupt pinhandler returns to “next” instructionExamples:Examples:I/O interruptshitting Ctrl-C at the keyboardarrival of a packet from a networkarrival of data from a diskHard reset interrupthitting the reset buttonSoft reset interrupthitting 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:TrapsIntentionalExamples: system calls, breakpoint traps, special instructionsReturns control to “next” instructionFaultsUnintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptionsEither re-executes faulting (“current”) instruction or abortsAbortsunintentional and unrecoverableExamples: parity error, machine checkAborts current program– 9 –15-213, F’08Trap ExampleTrap 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 . . .– 10 –15-213, F’08Fault 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 –15-213, F’08Fault Example #2Fault 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 process– 12 –15-213, F’08ProcessesProcessesDefinition: A Definition: A processprocess is 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 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
View Full Document