Slide 1TodayControl FlowAltering the Control FlowExceptional Control FlowExceptionsInterrupt VectorsAsynchronous Exceptions (Interrupts)Synchronous ExceptionsTrap Example: Opening FileFault Example: Page FaultFault Example: Invalid Memory ReferenceException Table IA32 (Excerpt)TodayProcessesConcurrent ProcessesUser View of Concurrent ProcessesContext Switchingfork: Creating New ProcessesUnderstanding forkFork Example #1Fork Example #2Fork Example #3Fork Example #4Fork Example #5exit: Ending a processZombiesZombie ExampleNonterminating Child Examplewait: Synchronizing with Childrenwait: Synchronizing with Childrenwait() Examplewaitpid(): Waiting for a Specific Processexecve: Loading and Running Programsexecve ExampleSummarySummary (cont.)Carnegie Mellon1Exceptional Control Flow: Exceptions and Processes15-213: Introduction to Computer Systems12th Lecture, Oct. 5, 2010Instructors: Randy Bryant and Dave O’HallaronCarnegie Mellon2TodayExceptional Control FlowProcessesCarnegie Mellon3Control Flow<startup>inst1inst2inst3…instn<shutdown>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 flowTimeCarnegie Mellon4Altering the Control FlowUp to now: two mechanisms for changing control flow:Jumps and branchesCall and returnBoth react to changes in program stateInsufficient for a useful system: Difficult 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”Carnegie Mellon5Exceptional Control FlowExists at all levels of a computer systemLow level mechanismsExceptions change in control flow in response to a system event (i.e., change in system state)Combination of hardware and OS softwareHigher level mechanismsProcess context switchSignalsNonlocal jumps: setjmp()/longjmp()Implemented by either:OS software (context switch and signals)C language runtime library (nonlocal jumps)Carnegie Mellon6ExceptionsAn exception is a transfer of control to the OS in response to some event (i.e., change in processor state)Examples: div by 0, arithmetic overflow, page fault, I/O request completes, Ctrl-CUser Process OSexceptionexception processingby exception handler• return to I_current•return to I_next•abortevent I_currentI_nextCarnegie Mellon7012...n-1Interrupt VectorsEach type of event has a unique exception number kk = index into exception table (a.k.a. interrupt vector)Handler k is called each time exception k occursExceptionTablecode for exception handler 0code for exception handler 1code forexception handler 2code for exception handler n-1...Exception numbersCarnegie Mellon8Asynchronous Exceptions (Interrupts)Caused by events external to the processorIndicated by setting the processor’s interrupt pinHandler returns to “next” instructionExamples: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 PCCarnegie Mellon9Synchronous ExceptionsCaused by events that occur as a result of executing 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 exceptionsEither re-executes faulting (“current”) instruction or abortsAbortsunintentional and unrecoverableExamples: parity error, machine checkAborts current programCarnegie Mellon10Trap Example: Opening 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 . . .User Process OSexceptionopen filereturnsintpopCarnegie Mellon11Fault Example: Page FaultUser 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,0x8049d10User Process OSexception: page faultCreate page and load into memoryreturnsmovlCarnegie Mellon12Fault Example: Invalid Memory ReferencePage 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,0x804e360User Process OSexception: page faultdetect invalid addressmovlsignal processCarnegie Mellon13Exception Table IA32 (Excerpt)Exception Number Description Exception Class0 Divide error Fault13 General protection fault Fault14 Page fault Fault18 Machine check Abort32-127 OS-defined Interrupt or trap128 (0x80) System call Trap129-255 OS-defined Interrupt or trapCheck Table 6-1:http://download.intel.com/design/processor/manuals/253665.pdfCarnegie Mellon14TodayExceptional Control FlowProcessesCarnegie Mellon15ProcessesDefinition: A process is an instance of a running program.One of the most profound ideas in computer scienceNot the same as “program” or “processor”Process provides each program with two key abstractions:Logical control flowEach program seems to have exclusive use of the CPUPrivate virtual address spaceEach program seems to have exclusive use of main memoryHow are these Illusions maintained?Process executions interleaved (multitasking) or run on separate coresAddress spaces managed by virtual memory systemwe’ll talk about this in a couple of weeksCarnegie Mellon16Concurrent ProcessesTwo processes run concurrently (are concurrent) if their flows overlap in timeOtherwise, they are sequentialExamples (running on single core):Concurrent: A & B, A & CSequential: B & CProcess A Process B Process CTimeCarnegie Mellon17User View of Concurrent ProcessesControl flows for concurrent processes are
View Full Document