University of New Mexico1Exceptional Control Flow: Exceptions and ProcessesECE 335: Integrated Software Systems Instructor:Edward NavaUniversity of New Mexico2Today Exceptional Control Flow ProcessesUniversity of New Mexico3Control 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 time This sequence is the CPU’s control flow (or flow of control)Physical control flowTimeUniversity of New Mexico4Altering the Control Flow Up to now: two mechanisms for changing control flow: Jumps and branches Call and returnBoth react to changes in program state Insufficient for a useful system: Difficult to react to changes in system state data arrives from a disk or a network adapter instruction divides by zero user hits Ctrl-C at the keyboard System timer expires System needs mechanisms for “exceptional control flow”University of New Mexico5Exceptional Control Flow Exists at all levels of a computer system Low level mechanisms Exceptions change in control flow in response to a system event (i.e., change in system state) Combination of hardware and OS software Higher level mechanisms Process context switch Signals Nonlocal jumps: setjmp()/longjmp() Implemented by either: OS software (context switch and signals) C language runtime library (nonlocal jumps)University of New Mexico6Exceptions An 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_nextUniversity of New Mexico7012...n-1Interrupt Vectors Each type of event has a unique exception number k k = 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 numbersUniversity of New Mexico8Asynchronous Exceptions (Interrupts) Caused by events external to the processor Indicated by setting the processor’s interrupt pin Handler returns to “next” instruction Examples: I/O interrupts hitting Ctrl-C at the keyboard arrival of a packet from a network arrival of data from a disk Hard reset interrupt hitting the reset button Soft reset interrupt hitting Ctrl-Alt-Delete on a PCUniversity of New Mexico9Synchronous Exceptions Caused by events that occur as a result of executing an instruction: Traps Intentional Examples: system calls, breakpoint traps, special instructions Returns control to “next” instruction Faults Unintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable), floating point exceptions Either re-executes faulting (“current”) instruction or aborts Aborts unintentional and unrecoverable Examples: parity error, machine check Aborts current programUniversity of New Mexico10Trap Example: Opening File User calls: open(filename, options) 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. . .User Process OSexceptionopen filereturnsintpopUniversity of New Mexico11Fault Example: Page Fault 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,0x8049d10User Process OSexception: page faultCreate page and load into memoryreturnsmovlUniversity of New Mexico12Fault Example: Invalid Memory Reference 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,0x804e360User Process OSexception: page faultdetect invalid addressmovlsignal processUniversity of New Mexico13Exception Table IA32 (Excerpt)ExceptionNumberDescriptionException Class0Divide errorFault13General protection faultFault14Page faultFault18Machine checkAbort32-127OS-definedInterrupt or trap128 (0x80)System callTrap129-255OS-definedInterrupt or trapCheck Table 6-1:http://download.intel.com/design/processor/manuals/253665.pdfUniversity of New Mexico14Today Exceptional Control Flow ProcessesUniversity of New Mexico15Processes Definition: A process 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 abstractions: Logical control flow Each program seems to have exclusive use of the CPU Private virtual address space Each program seems to have exclusive use of main memory How are these Illusions maintained? Process executions interleaved (multitasking) or run on separate cores Address spaces managed by virtual memory system we’ll talk about this in a couple of weeksUniversity of New Mexico16Concurrent Processes Two processes run concurrently (are concurrent) if their flows overlap in time Otherwise, they are sequential Examples (running on single core): Concurrent: A & B, A & C Sequential: B & CProcess A Process B Process CTimeUniversity of New Mexico17User View of Concurrent Processes Control flows for concurrent processes are physically disjoint in time However, we can think of concurrent processes are running in parallel with each otherTimeProcess A Process B Process CUniversity of New Mexico18Context Switching Processes are managed by a shared chunk of OS code called the kernel Important: the kernel is not a separate process, but rather runs as part of some user process Control flow passes from one process to another via a context switchProcess A Process Buser codekernel codeuser codekernel codeuser codecontext switchcontext switchTimeUniversity of New Mexico19fork: Creating New Processes int fork(void)
View Full Document