Exceptional Control Flow: Exceptions and Processes
Control Flow
Processors do only one thing: From startup to shutdown, a CPU simply reads and executes(interprets) a sequence of instructions, one at a time
To alter the control flow
- Jumps and branches (jmp, CE)
- Call and return (assemble code)
Not able to react to changes in system state:
- Data arrives from a disk or a network adapter
- Instruction divides by zero (or other wrong format)
- User hit Crtl+C at the keyboard
- System timer expires
Exceptional Control Flow
- Exceptions: Change in control flow in response to a system event (Low level, implemented by hardware with OS software)
- Process context switch (High level, implemented by OS software and hardware timer)
- Signals (High level, implemented by OS software)
- Nonlocal jumps (High level, implemented by C runtime library)
Exceptions
An exception is a transfer of control to the OS kernel in response to some event. (Divide by 0, arithmetic overflow, page fault, I/O request completes, typing Ctrl-C)
Asynchronous Exceptions (Interrupts)
- Caused by events external to the processor
- Indicate by setting the processor's
interrupt pin
- Handler returns to
next
instruction
- Indicate by setting the processor's
Timer interrupt, I/O interrupt from external device
Synchronous Exceptions
Caused by events that occur as a result of executing an instruction:
- Traps
- Intentional
- e.g. system calls, breakpoint traps, special instructions
- Returns control to
next
instruction
- Faults
- Unintentional by possible recoverable
- e.g. page faults(recoverable), protection faults(unrecoverable), floating point exceptions
- Either re-executes faulting ("current") instruction or aborts
- Abort
- Unintentional and unrecoverable
- e.g. illegal instruction, parity error, machine check
- Aborts current program
System Calls
Each x86-64 system call has a unique ID number (for identification)
Processes
A process is an instance of a running program.
- Each process is a logical control flow
- Two processes run concurrently if their flows overlap in time
- Otherwise, they are sequential (Single Core)
Syscall Error Handling
On error, Linux system-level functions typically return -1 and set global variable errno
to indicate cause.
if ((pid = fork()) < 0) {
fprintf(stderr, "fork error: %s\n", strerror(errno));
exit(0);
}