Exceptional Control Flow Oct 24, 2000Control flowAltering the control flowExceptional control flowSystem context for exceptionsExceptionsInterrupt vectorsAsynchronous exceptions (interrupts)Synchronous exceptionsProcessesLogical control flowsConcurrent processesUser view of concurrent processesContext switchingPrivate address spacesfork: Creating new processesexit: Destroying processeswait: Synchronizing with childrenexec: Running new programsLinux process hierarchyLinux Startup: Step 1Linux Startup: Step 2Linux Startup: Step 3Linux Startup: Step 4Example: Loading and running programs from a shellExample: Concurrent network serverSignalsA program that reacts to externally generated events (ctrl-c)A program that reacts to internally generated eventsNonlocal jumps: setjmp()/longjmp()setjmp/longjmp (cont)setjmp/longjmp examplePutting it all together: A program that restarts itself when ctrl-c’dExceptional Control FlowOct 24, 2000Topics•Exceptions•Process context switches•Signals•Non-local jumpsclass17.ppt15-213“The course that gives CMU its Zip!”CS 213 F’00– 2 –class17.pptControl flow<startup>inst1inst2inst3…instn<shutdown>From startup to shutdown, a CPU simply reads and executes (interprets) a sequence of instructions, one at a time.This sequence is the system’s physical control flow (or flow of control).Physical control flowTimeCS 213 F’00– 3 –class17.pptAltering the control flowSo far in class, we’ve discussed two mechanisms for changing the control flow:•jumps and branches•call and return using the stack discipline.•both react to changes in program state.These are insufficient for a useful system•difficult for the CPU to react to changes in system state. –data arrives from a disk or a network adapter.–instruction divides by zero–user hitting ctl-c at the keyboard–system timer expiresReal systems need mechanisms for “exceptional control flow”CS 213 F’00– 4 –class17.pptExceptional control flowMechanisms for exceptional control flow exists at all levels of a computer system.Low level mechanism:•exceptions –change in control flow in response to a system event (i.e., change in system state)•implemented as a combination of both hardware and OS softwareHigher 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.CS 213 F’00– 5 –class17.pptSystem context for exceptionsLocal/IO BusLocal/IO BusMemoryMemoryNetworkadapterNetworkadapterIDE diskcontrollerIDE diskcontrollerVideoadapterVideoadapterDisplayDisplayNetworkNetworkProcessorProcessorInterruptcontrollerInterruptcontrollerSCSIcontrollerSCSIcontrollerSCSI busSCSI busSerial port controllerSerial port controllerParallel portcontrollerParallel portcontrollerKeyboardcontrollerKeyboardcontrollerKeyboardKeyboardMouseMousePrinterPrinterModemModemdiskdisk cdromCS 213 F’00– 6 –class17.pptExceptionsAn exception is a transfer of control to the OS in response to some event (i.e. change in processor state)User Process OSexceptionexception processingby exception handlerexception return (optional)event currentnextCS 213 F’00– 7 –class17.pptInterrupt vectors1. Each type of event has a unique exception number k2. Jump table (interrupt vector)entry k points to a function(exception handler).3. Handler k is called each timeexception 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 numbersCS 213 F’00– 8 –class17.pptAsynchronous exceptions (interrupts)Caused by events (changes in state) external to the processor•Indicated by setting the processor’s interrupt pin•handler returns to “next” instruction.Examples:•I/O interrupts–hitting ctl-c at the keyboard–arrival of a packet from a network–arrival of a data sector from a disk•Hard reset interrupt–hitting the reset button•Soft reset interrupt–hitting ctl-alt-delete on a PCCS 213 F’00– 9 –class17.pptSynchronous exceptionsCaused by events (changes in state) that occur as a result of executing an instruction:•Traps–intentional–returns control to “next” instruction–Examples: system calls, breakpoint traps•Faults–unintentional but possibly recoverable –either re-executes faulting (“current”) instruction or aborts.–Examples: page faults (recoverable), protection faults (unrecoverable).•Aborts–unintentional and unrecoverable–aborts current program– Examples: parity error, machine check.CS 213 F’00– 10 –class17.pptProcessesDef: A process is an instance of a running program.•One of the most profound ideas in computer science.Process provides each program with two key abstractions:•Logical control flow–gives each program the illusion that it has exclusive use of the CPU.•Private address space–gives each program the illusion that has exclusive use of main memory.CS 213 F’00– 11 –class17.pptLogical control flowsTimeProcess A Process B Process CEach process has its own logical control flowCS 213 F’00– 12 –class17.pptConcurrent processesTwo processes run concurrently (are concurrent) if their flows overlap in time.Otherwise, they are sequential. Examples:•Concurrent: A & B, A&C•Sequential: B & CTimeProcess A Process B Process CCS 213 F’00– 13 –class17.pptUser view of concurrent processesControl flows for concurrent processes are physically disjoint in time.However, we can think of concurrent processes are running in parallel with each other.TimeProcess A Process B Process CCS 213 F’00– 14 –class17.pptContext switchingProcesses 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 processControl flow passes from one process to another via a context switch.Process AcodeProcess Bcodeuser codekernel codeuser codekernel codeuser codeTimecontext switchcontext switchCS 213 F’00– 15 –class17.pptPrivate address spacesEach process has its own private address space.kernel virtual memory(code, data, heap, stack)memory mapped region forshared librariesrun-time heap(managed by malloc)user stack(created at runtime)unused0%esp (stack pointer)memoryinvisible touser
View Full Document