CS 105 Tour of the Black Holes of Computing Exceptional Control Flow Topics Exceptions Process hierarchy Shells Signals Control Flow Computers 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 system s physical control flow or flow of control Physical control flow Time 2 startup inst1 inst2 inst3 instn shutdown CS 105 Altering the Control Flow Up to now two mechanisms for changing control flow Jumps and branches react to changes in program state Call and return using stack discipline react to program state Insufficient for a useful system Difficult for the CPU to react to other changes in system state Data arrives from a disk or a network adapter Instruction divides by zero User hits control C at the keyboard System needs mechanisms for exceptional control flow 3 CS 105 Exceptional Control Flow Mechanisms for exceptional control flow exist 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 Combination of hardware and OS software Higher Level Mechanisms Process context switch Signals Nonlocal jumps setjmp longjmp ignored in this course Implemented by either OS software context switch and signals C language runtime library nonlocal jumps 4 CS 105 Exceptions An exception is a transfer of control to the OS in response to some event i e change in processor state User Process event current next OS exception exception processing by exception handler exception return optional Think of it as a hardware initiated function call 5 CS 105 Interrupt Vectors Exception numbers interrupt vector 0 1 2 n 1 code codefor for exception exceptionhandler handler00 Each type of event has a unique exception number k code codefor for exception handler exception handler11 Index into jump table a k a interrupt vector code codefor for exception exceptionhandler handler22 Jump table entry k points to a function exception handler Handler k is called each time exception k occurs code codefor for exception handler exception handlern 1 n 1 6 CS 105 Asynchronous Exceptions Interrupts Caused by events external to processor Indicated by setting the processor s interrupt pin s Handler returns to next instruction Examples I O interrupts Hitting control C or any key at the keyboard Arrival of packet from network Arrival of data sector from disk Hard reset interrupt Hitting reset button Soft reset interrupt Hitting control alt delete on a PC 7 CS 105 Synchronous Exceptions Caused by events that occur as 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 Either re executes faulting current instruction or aborts Aborts Unintentional and unrecoverable Examples parity error machine check Aborts current program or entire OS 8 CS 105 Trap Example Opening a File User calls open filename options 0804d070 libc open 804d082 cd 80 804d084 5b int pop 0x80 ebx Function open executes system call instruction int 0x80 OS must find or create file get it ready for reading or writing Returns integer file descriptor User Process int pop 9 OS exception Open file return CS 105 Fault Example 1 Memory Reference 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 try 80483b7 c7 05 10 9d 04 08 0d User Process event movl 0xd 0x8049d10 OS page fault return 10 movl int a 1000 main a 500 13 Create page and load into memory CS 105 Fault Example 2 int a 1000 main a 5000 13 Memory Reference User writes to memory location Address is not valid Page handler detects invalid address 80483b7 c7 05 60 e3 04 08 0d Sends SIGSEGV signal to user process User process exits with segmentation fault User Process event 11 movl movl 0xd 0x804e360 OS page fault Detect invalid address Signal process CS 105 Summarizing Exceptions Exceptions Events that require nonstandard control flow Generated externally interrupts or internally traps and faults OS decides how to handle 12 CS 105 ECF Exists at All Levels of a System Exceptions Hardware and operating system kernel software Concurrent processes Hardware timer and kernel software Signals Kernel software Non local jumps ignored in this class 13 Application code Evil in C C Python throw catch CS 105 Shell Programs A shell is an application program that runs programs on behalf of the user sh Original Unix Bourne shell csh BSD Unix C shell tcsh Enhanced C shell now deprecated bash Bourne Again shell int main char cmdline MAXLINE while 1 read printf Fgets cmdline MAXLINE stdin if feof stdin exit 0 21 Execution is a sequence of read evaluate steps evaluate eval cmdline CS 105 Simple Shell eval Function void eval char cmdline char argv MAXARGS argv for execvp int bg should the job run in bg or fg pid t pid process id bg parseline cmdline argv if builtin command argv if pid Fork 0 child runs user job execvp argv 0 argv printf s Command not found n argv 0 exit 0 22 if bg parent waits for fg job to terminate int status if waitpid pid status 0 0 unix error waitfg waitpid error else otherwise don t wait for bg job printf d s pid cmdline CS 105 Problem with Simple Shell Example Shell correctly waits for and reaps foreground jobs But what about background jobs Will become zombies when they terminate Will never be reaped because shell typically will not terminate Eventually you hit process limit and can t do any work Solution Reaping background jobs requires mechanism called a signal 23 CS 105 Signals A signal is a small message that notifies a process that an event of some type has occurred in the system ID Kernel abstraction for exceptions and interrupts Sent from kernel sometimes at request of another process to a process Different signals are identified by small integer ID s The only information in a signal is its ID and the fact that it arrived Name 2 SIGINT 9 SIGKILL Default Action Corresponding Event Terminate Interrupt from keyboard ctl c Terminate Kill program cannot override or ignore 11 SIGSEGV 14 SIGALRM Terminate Dump Segmentation violation Terminate Timer signal 17 SIGCHLD Ignore Child stopped or terminated 24 CS 105 Signal Concepts Sending a signal Kernel sends delivers a signal to a
View Full Document
Unlocking...