Exceptional Control Flow
Exceptional Control Flow
control flow
• From the time you first apply power to a processor until the
time you shut it off, the program counter assumes a
sequence of values
a0, a1, . . . , an−1
• where each ak is the address of some corresponding
instruction Ik. Each transition from ak to ak+1 is called a
control transfer.
• A sequence of such control transfers is called the flow of
control, or control flow of the processor.
• The simplest kind of control flow is a “smooth” sequence
where each Ik and Ik+1 are adjacent in memory.
Control Flow
• Computer 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)
Altering the Control Flow
• Up to Now: two mechanisms for changing control flow:
– Jumps and branches
– Call and return using the stack discipline.
• Such instructions are necessary mechanisms that allow programs
to react to changes in internal program state represented by
program variables.
• But systems must also be able to react to changes in system state
that are not captured by internal program variables and are not
necessarily related to the execution of the program.
• example
– data arrives from a disk or a network adapter.
– Instruction divides by zero
– User hits ctl-c at the keyboard
– System timer expires ….
Exceptional control flow
• Modern systems react to these situations by making abrupt
changes in the control flow.
• In general, we refer to these abrupt changes as exceptional
control flow (ECF).
• Exceptional control flow occurs at all levels of computer system.
• For example:
• At the hardware level, events detected by the hardware trigger abrupt
control transfers to exception handlers.
• At the operating systems level, the kernel transfers control from one user
process to another via context switches.
• At the application level, a process can send a signal to another process
that abruptly transfers control to a signal handler in the recipient.
• An individual program can react to errors by sidestepping the usual stack
discipline and making nonlocal jumps to arbitrary locations in other
functions.
Exceptional control flow …
• As programmers, there are a number of reasons why it is
important for you to understand ECF:
• understand important systems concepts. ECF is the basic
mechanism that operating systems use to implement I/O,
processes, and virtual memory.
• understand how applications interact with the operating
system. Applications request services from the operating
system by using a form of ECF known as a trap or system
call.
• understand concurrency. ECF is a basic mechanism for
implementing concurrency in computer systems.
• understand how software exceptions work. Languages
such as C++ and Java provide software exception
mechanisms via try, catch, and throw statements.
Exceptions
• Exceptions are a form of exceptional control flow that are
implemented partly by the hardware and partly by the operating
system.
• An exception is a transfer of control to the OS in response to
some event (i.e., change in processor state)
Exception Handling
• when the processor detects that the event has occurred,
it makes an indirect procedure call (the exception),
through a jump table called an exception table, to an
operating system subroutine (the exception handler) that
is specifically designed to process this particular kind of
event.
• When the exception handler finishes processing, one of
three things happens, depending on the type of event
that caused the exception:
1. The handler returns control to the current instruction Icurr,
the instruction that was executing when the event occurred.
2. The handler returns control to Inext,, the instruction that
would have executed next had the exception not occurred.
3. The handler aborts the interrupted program.
Interrupt Vectors
• It is a data structure that associates a list of interrupt
handlers with a list of interrupt requests
Classes of Exceptions
• Exceptions can be divided into four classes:
interrupts, traps, faults, and aborts.
Asynchrones Exceptions (Interrupts)
• Caused by events 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
Synchronous Exceptions
• Caused by events that occur as a 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).
• Either re-executes faulting (“current”) instruction or aborts.
– Aborts
• unintentional and unrecoverable
• Examples: parity error, machine check.
• Aborts current program
Process
• A process is an instance of a running program.
– Not the same as “program” or “processor”
• Process provides each program with two key
abstractions:
– Logical control flow
• Each program seems to have exclusive use of the CPU.
– Private address space
• Each program seems to have exclusive use of main memory.
• How are these Illusions maintained?
– Process executions interleaved (multitasking)
– Address spaces managed by virtual memory system
Logical Control Flows
• Each process has its own logical control
flow
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.
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.
Summarizing
• Exceptions
– Events that require nonstandard control flow
– Generated externally (interrupts) or internally (traps and faults)
• Processes
– At any given time, system has multiple active processes
– Only one can execute at a time, though
– Each process appears to have total control of processor + private
memory space
Virtual Memory
register
CPU cache
• larger capacity
Main Memory • lower speed
• lower cost
Secondary Storage