Exceptions and Processes!: Jennifer Rexford!
Exceptions and Processes!: Jennifer Rexford!
Exceptions and Processes!: Jennifer Rexford!
Jennifer Rexford!
2
Context of this Lecture!
Second half of the course!
Previously! Starting Now!
• Examples:!
• Application program:!
• Requests I/O!
• Requests more heap memory!
• Attempts integer division by 0!
• Attempts to access privileged memory! Synchronous!
• Accesses variable that is not
in real memory (see upcoming
“Virtual Memory” lecture)!
• User presses key on keyboard!
• Disk controller finishes reading data!
Asynchronous!
5
Exceptions Note!
• Note:!
! !Exceptions in OS ≠ exceptions in Java!
Implemented using!
try/catch!
and throw statements!
6
Exceptional Control Flow!
exception!
exception!
processing!
exception!
return!
(optional)!
7
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!
8
Classes of Exceptions!
9
(1) Interrupts!
Application! Exception!
program! handler!
13
Summary of Exception Classes!
14
Exceptions in Intel Processors!
Each exception has a number!
Some 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)!
15
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”!
/* client.c */
… A call of a system-level function,!
sbrk(1024); that is, a system call!
…
• Process!
• An instance of a program in execution!
18
Significance of Processes!
19
Private Control Flow: Illusion!
Process 1! Process 2!
Time!
Exception!
Return from exception!
Exception! Time!
Return from exception!
Exception!
Return from exception!
21
Context Switches!
• Context switch!
• The activity whereby the OS assigns the CPU to a
different process!
• Occurs during exception handling, at discretion of OS!
23
When Should OS Do Context Switch?!
• When a process is stalled waiting for I/O!
• Better utilize the CPU, e.g., while waiting for disk access!
Waiting!
25
Context Switch: What Context to Save?!
• Process state !
• New, ready, waiting, terminated!
• CPU registers !
• EIP, EFLAGS, EAX, EBX, …!
• Accounting information!
• Time limits, group ID, ...!
Process 1! Process 2!
00000000 00000000
Memory! Memory!
for! for!
Process! Process!
1! 2!
FFFFFFFF FFFFFFFF
unused!
unused!
FFFFFFFF FFFFFFFF
29
Summary!
• Exception: an abrupt change in control flow!
• Interrupts: asynchronous; e.g. I/O completion, hardware
timer!
• Traps: synchronous; e.g. app pgm requests more heap
memory, I/O!
• Faults: synchronous; e.g. seg fault!
• Aborts: synchronous; e.g. parity error!
34
Appendix: System-Level Functions!
Linux system-level functions for signal handling!