To answer in general terms, instead of going into gory details for Linux on x86_64 that probably obscure the concepts.
Processors typically throw an exception interrupt, such as dividing by zero or dereferencing a NULL pointer. These interrupts are captured, for example, when hardware interrupts interrupt the execution of the current program and return OS control, which then processes the event. Despite the fact that the actions are very dependent on the environment, usually the program can be terminated, all resources are freed (memory, open files) and optional core dumps / stacks created for debugging purposes in the developer's system.
The runtime can be configured so that an exception handler is called, perhaps the scripting language wants to catch an integer division by 0 or an integer overflow, and then throw a programming language exception or create diagnostics to help the programmer understand where and where why this happened. Another traditional opportunity is to receive a signal that can be captured by the application and processed, or lead to termination.
On some RISC processors, software traps in the OS will be launched to eliminate inconsistent data accesses, so reading memory will work, but with limited performance. In the past, traps were sometimes used to emulate specific instructions, but which were not implemented in hardware by a specific CPU model. I also saw hardware memory errors logged as the OS initiates the ECC memory recovery operation, although it is handled differently on x86.
System calls actually use the same mechanism to transition from a user space application to the OS kernel, which then processes the event, hence the generic term trap .
Rob11311 May 26 '14 at 23:04 2014-05-26 23:04
source share