15 213 The course that gives CMU its Zip Exceptional Control Flow Oct 24 2000 Topics class17 ppt Exceptions Process context switches Signals Non local jumps Control flow 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 class17 ppt startup inst1 inst2 inst3 instn shutdown 2 CS 213 F 00 Altering the control flow So 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 expires Real systems need mechanisms for exceptional control flow class17 ppt 3 CS 213 F 00 Exceptional control flow Mechanisms 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 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 class17 ppt 4 CS 213 F 00 System context for exceptions Keyboard Keyboard Processor Processor Interrupt Interrupt controller controller Mouse Mouse Keyboard Keyboard controller controller Modem Modem Serial Serialport port controller controller Printer Printer Parallel Parallelport port controller controller Local IO Local IOBus Bus Memory Memory IDE IDEdisk disk controller controller SCSI SCSI controller controller Video Video adapter adapter Network Network adapter adapter Display Display Network Network SCSI SCSIbus bus disk disk class17 ppt cdrom 5 CS 213 F 00 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 class17 ppt 6 CS 213 F 00 Interrupt vectors Exception numbers code codefor for exception handler exception handler00 interrupt vector 0 1 2 code codefor for exception handler exception handler11 code codefor for exception handler exception handler22 1 Each type of event has a unique exception number k 2 Jump table interrupt vector entry k points to a function exception handler n 1 code codefor for exception exceptionhandler handlern 1 n 1 class17 ppt 7 3 Handler k is called each time exception k occurs CS 213 F 00 Asynchronous 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 PC class17 ppt 8 CS 213 F 00 Synchronous exceptions Caused 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 class17 ppt 9 CS 213 F 00 Processes Def 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 class17 ppt 10 CS 213 F 00 Logical control flows Each process has its own logical control flow Process A Process B Process C Time class17 ppt 11 CS 213 F 00 Concurrent processes Two processes run concurrently are concurrent if their flows overlap in time Otherwise they are sequential Examples Concurrent A B A C Sequential B C Process A Process B Process C Time class17 ppt 12 CS 213 F 00 User 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 other Process A Process B Process C Time class17 ppt 13 CS 213 F 00 Context 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 switch Process A code Process B code user code kernel code Time context switch user code kernel code context switch user code class17 ppt 14 CS 213 F 00 Private address spaces Each process has its own private address space 0xffffffff kernel virtual memory code data heap stack 0xc0000000 0x40000000 user stack created at runtime read write segment data bss 0 class17 ppt esp stack pointer memory mapped region for shared libraries run time heap managed by malloc 0x08048000 memory invisible to user code read only segment init text rodata brk loaded from the executable file unused 15 CS 213 F 00 fork Creating new processes int fork void creates a new process child process that is identical to the calling process parent process returns 0 to the child process returns child s pid to the parent process if fork 0 printf hello from child n else printf hello from parent n class17 ppt 16 Fork is interesting and often confusing because it is called once but returns twice CS 213 F 00 exit Destroying processes void exit int status exits a process atexit registers functions to be executed upon exit void cleanup void printf cleaning up n main atexit cleanup if fork 0 printf hello from child n else printf hello from parent n exit class17 ppt 17 CS 213 F 00 wait Synchronizing with children int wait int child status suspends current process until one of its children terminates return value the pid of the child process that terminated if child status NULL then the object it points to will be set to a status indicating why the child process terminated main int child status if fork 0 printf
View Full Document