Class 16: Exceptional Control Flow I (ECF) Todays topics: 1. Exceptions 2. Processes ************ Introduction ************ * From startup to shutdown, CPU simply reads and executes (interprets) a sequence of instructions: Physical control flow I_1 (a_1) I_2 (a_2) ... I_n control transfer: a_k -> a_k+1 control flow: Sequence of control transfers * Smooth control flow: sequential instructions * Abrupt changes to control flow: jump, call, ret * React to changes in program state * System must also be able to react to changes in the *system* state * hardware timer expires * disk finishes DMA transfer * network packet arrives * Exceptional control flow (ECF): * abrupt changes to control flow caused by changes in system state. * ECF occurs at all levels of a system * Exceptions (hardware and OS software) * Processes (OS software) * Signals (OS software and application) * Nonlocal jumps (Application) * ECF is basic mechanism for concurrency: * If activity 2 (A2) starts before activity 1 (A1) ends, then A1 and A2 are *concurrent*. Key idea: interleaved execution is identical to parallel execution. Draw some timing diagrams: ************* 1. Exceptions ************* Draw system picture on board. Exception: control transfer to OS in response to some event (change in processor state): Application program OS | | | exception event I_curr ------------> Exception processing I_next <------------ | | ******************** Types of exceptions: ******************* * Interrupts (Asynchronous exceptions) * Event: processor interrupt pin goes high * hitting ctrl-c * packet arrival * DMA complete * hitting reset button * device sets pin high and puts interrupt number on bus interupt number indexes jump table of exception handlers 2. Synchronous exceptions * Event: occurs when excecution instruction * Traps * Intentional * Ex: system calls (open, read, exit) * Faults * Unintentional but possibly recoverable * Ex: page faults (recoverable), protection faults (unrecoverable) * If recoverable, re-execute the faulting instruction * Aborts * Unintentional and unrecoverable * example: parity error, machine check * aborts current program ********* Processes ********* Process: instance of a running program One of the profound ideas of computer science * Process provides each program with two key abstractions: 1. logical control flow 2. private address space 1. Logical control flows (multi-tasking): ProcA ProcB ProcC | | | | OS implements multi-tasking using context switch 2. Private address space: draw VM picture * Creating processes using fork: int fork(void) * creates new process (child) that is identical to calling process (parent), except for PID * returns 0 to child * return child's PID to parent * Called once, return twice! if (fork() == 0) { /* child */ ... exit(0) } ... /* parent */ Show code/ecf/forks.c examples * Running programs with exec() int execl(char *path, char *arg0, char *arg1, 0) * loads and runs executable at path with args arg0, ... * arg0 is typically the name of the executable * arg1 begins real args * returns -1 if error, otherwise doesn't return! Called once, returns never! Show code/ecf/execs examples