Exceptional 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 andexecutes (interprets) a sequence of instructions, oneat a time.This sequence is the system’s physical control flow (orflow of control).Physical control flowTimeCS 213 F’00– 3 –class17.pptAltering the control flowSo far in class, we’ve discussed two mechanisms forchanging 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 “exceptionalcontrol flow”CS 213 F’00– 4 –class17.pptExceptional control flowMechanisms for exceptional control flow exists at alllevels of a computer system.Low level mechanism:• exceptions–change in control flow in response to a system event (i.e., change insystem 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 responseto some event (i.e. change in processor state)User Process OSexceptionexception processingby exception handlerexception return (optional)eventcurrentnextCS 213 F’00– 7 –class17.pptInterrupt vectors1. Each type of event hasa 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 theprocessor• 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 aresult 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 keyabstractions:• 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) iftheir 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 physicallydisjoint in time.However, we can think of concurrent processes arerunning 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 codecalled the kernel• Important: the kernel is not a separate process, but rather runs aspart of some user processControl flow passes from one process to another via acontext 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 codebrk0xc00000000x080480000x40000000read/write segment(.data, .bss)read-only segment(.init, .text, .rodata)loaded from the executable file0xffffffffCS 213 F’00– 16 –class17.pptfork: Creating new processesint fork(void)• creates a new process (child process) that is identical to the callingprocess (parent process)• returns 0 to the child process• returns child’s pid to the parent processif (fork() == 0) { printf(“hello from child\n”);} else { printf(“hello from parent\n”);}Fork is interesting(and often confusing)because it is calledonce but returns twiceCS 213 F’00– 17 –class17.pptexit: Destroying processes void exit(int status)• exits a process• atexit() registers functions to be executed upon exitvoid cleanup(void) { printf(“cleaning up\n”);}main() { atexit(cleanup); if (fork() == 0) { printf(“hello from child\n”); } else {
View Full Document