1 1 Exceptions and Processes!The material for this lecture is drawn from!Computer Systems: A Programmerʼs Perspective (Bryant & OʼHallaron) Chapter 8!2 Goals of this Lecture!• Help you learn about:!• Exceptions!• The process concept!… and thereby…!• How operating systems work!• How application programs interact with operating systems and hardware!The process concept is one of the most important concepts in systems programming!2 3 Context of this Lecture!Second half of the course!Previously! Starting Now!C Language!Assembly Language!Machine Language!Application Program!Operating System!Hardware!language!levels!tour!service!levels!tour!Application programs, OS,!and hardware interact!via exceptions!4 Motivation!Question:!• Executing program thinks it has exclusive control of the CPU!• But multiple executing programs must share one CPU (or a few CPUs)!• How is that illusion implemented?!Question:!• Executing program thinks it has exclusive use of all of memory!• But multiple executing programs must share one memory!• How is that illusion implemented?!Answers: Exceptions…!3 5 Exceptions!• Exception!• An abrupt change in control flow in response to a change in processor state!• Examples:!• Application program:!• Requests I/O!• Requests more heap memory!• Attempts integer division by 0!• Attempts to access privileged memory!• Accesses variable that is not$in real memory (see upcoming $“Virtual Memory” lecture)!• User presses key on keyboard!• Disk controller finishes reading data!Synchronous!Asynchronous!6 Exceptions Note!• Note:!! !Exceptions in OS ≠ exceptions in Java!Implemented using!try/catch!and throw statements!4 7 Exceptional Control Flow!Application!program!Exception handler!in operating system!exception!exception!processing!exception!return!(optional)!8 Exceptions vs. Function Calls!• Exceptions are similar to function calls!• Control transfers from original code to other code!• Other code executes!• Control returns to original code!• Exceptions are different from function calls!• Processor pushes additional state onto stack!• E.g. values of all registers!• Processor pushes data onto OSʼs stack, not application pgmʼs stack!• Handler runs in privileged mode, not in user mode!• Handler can execute all instructions and access all memory!• Control might return to next instruction!• Control sometimes returns to current instruction!• Control sometimes does not return at all!!5 9 Classes of Exceptions!• There are 4 classes of exceptions…!10 (1) Interrupts!Cause: Signal from I/O device!Examples:! User presses key! Disk controller finishes reading/writing data!Application!program!Exception!handler!(1) CPU interrupt!pin goes high!(2) After current instr !finishes, control passes!to handler!(3) Handler runs!(4) Handler returns!control to next instr!6 11 (2) Traps!Cause: Intentional (application program requests OS service)!Examples:! Application program requests more heap memory! Application program requests I/O!Traps provide a function-call-like interface between application and OS!Application!program!Exception!handler!(1) Application!pgm traps!(2) Control passes to!handler!(3) Handler runs!(4) Handler returns!control to next instr!12 (3) Faults!Cause: Application program causes (possibly) recoverable error!Examples:! Application program accesses privileged memory (seg fault)! Application accesses data that is not in real memory (page fault)!Application!program!Exception!handler!(1) Current instr!causes a fault!(2) Control passes!to handler!(3) Handler runs!(4) Handler returns!control to current instr,!or aborts!7 13 (4) Aborts!Cause: Non-recoverable error!Example:! Parity check indicates corruption of memory bit (overheating, cosmic ray!, etc.)!Application!program!Exception!handler!(1) Fatal hardware!error occurs!(2) Control passes!to handler!(3) Handler runs!(4) Handler aborts!execution!14 Summary of Exception Classes!Class! Cause! Asynch/Synch! Return Behavior!Interrupt! Signal from I/O$device!Asynch! Return to next instr!Trap! Intentional! Sync! Return to next instr!Fault! (Maybe) recoverable error!Sync! (Maybe) return to current instr!Abort! Non-recoverable error!Sync! Do not return!8 15 Exceptions in Intel Processors!Exception #! Exception!0! Fault: Divide error!13! Fault: Segmentation fault!14! Fault: Page fault (see “Virtual Memory” lecture)!18! Abort: Machine check!32-127! Interrupt or trap (OS-defined)!128! Trap!129-255! Interrupt or trap (OS-defined)!Each exception has a number!Some exceptions in Intel processors:!16 Traps in Intel Processors!• To execute a trap, application program should:!• Place number in EAX register indicating desired functionality!• Place parameters in EBX, ECX, EDX registers!• Execute assembly language instruction “int 128”!• Example: To request more heap memory…!movl $45, %eax movl $1024, %ebx int $128 In Linux, 45 indicates request!for more heap memory!Request is for 1024 bytes!Causes trap!9 17 System-Level Functions!• For convenience, traps are wrapped in system-level functions!• Example: To request more heap memory…!/* unistd.h */ void *sbrk(intptr_t increment); … /* unistd.s */ Defines sbrk() in assembly lang Executes int instruction … /* client.c */ … sbrk(1024); … A call of a system-level function,!that is, a system call!sbrk() is a!system-level!function!See Appendix for list of some Linux system-level functions!18 Processes!• Program!• Executable code!• Process!• An instance of a program in execution!• Each program runs in the context of some process!• Context consists of:!• Process ID!• Address space!• TEXT, RODATA, DATA, BSS, HEAP, and STACK!• Processor state!• EIP, EFLAGS, EAX, EBX, etc. registers!• Etc.!10 19 Significance of Processes!• Process is a profound abstraction in computer science!• The process abstraction provides application pgms with two key illusions:!• Private control flow!• Private address space!20 Private Control Flow: Illusion!Process 1! Process 2!Hardware and OS give each application process the!illusion that it is the only process running on the CPU!Time!11 21 Private Control Flow: Reality!Process 1! Process 2!All application processes -- and the OS process
View Full Document