In this lesson, you will learn how your CPU detects low-level hardware errors and translates them into the signals your process receives.
From Silicon to Software
When a process executes an instruction, it relies on the CPU to perform the operation. Sometimes, the CPU encounters a situation it cannot resolve—such as trying to access memory that doesn't belong to the process or attempting to execute a bit pattern that isn't a valid machine instruction. These are Hardware Exceptions.
At the hardware level, the CPU stops execution and triggers an interrupt to the Linux kernel. The kernel recognizes this "exception" as a fatal error for the currently running process. However, instead of simply killing the process instantly, the kernel acts as a translator: it maps these hardware-level faults into standard Linux signals.
The Translation Process
The kernel maintains a mapping between CPU exception types and specific signals. Here are the two most common examples:
- Segmentation Violation: If your code tries to read or write to an invalid memory address, the CPU’s Memory Management Unit (MMU) throws a fault. The kernel receives this and sends a
SIGSEGVto your process. - Illegal Instruction: If your program jumps to a memory address that contains garbage data instead of valid code, the CPU fails to decode the instruction. The kernel sends a
SIGILLto your process.
Consider this snippet of code that forces a hardware exception:
int *ptr = NULL;
*ptr = 42; // The CPU attempts to write to address 0
When this runs, the CPU detects the invalid write. The kernel catches the hardware fault and sends SIGSEGV to the process. By default, this signal terminates the program, often dumping "core" (a memory snapshot) to the disk for debugging.
Because these events are delivered as signals, you have the theoretical ability to catch them using sigaction()—though doing so after a memory corruption is rarely safe, as the state of your program is already compromised.
Key Takeaway: Hardware exceptions are CPU-level errors that the kernel intercepts and converts into signals, allowing the operating system to manage process crashes consistently.