简体   繁体   中英

Signal vs Exceptions vs Hardware Interrupts vs Traps

I read this answer and I thought that I got a clear idea. But then this answer is confusing me again.

Can somebody please give me a clear picture of the differences between Signal, exception, hardware interrupts and traps?

Moreover, I would like to know which among these block CPU preemption of the kernel code?

Examples would be helpful.

Interrupts are generated by hardware for events external to the processor core. These are asynchronous in nature which means the processor is unaware of when the interrupt will be generated. These are also called hardware interrupts. Example: interrupt generated by keyboard to type a character on screen, or the timer interrupt.

Exceptions : Exceptions occur when the processor detects an error condition while executing an instruction and are classified as faults , traps , or aborts depending on the way they are reported and whether the instruction that caused the exception can be restarted without loss of program or task continuity. (Those technical terms are used on x86 at least, maybe other architectures or in general.) Example: Divide by zero, or a page fault.

Traps : are basically an instruction which tells the kernel to switch to kernel mode from user mode. Example: during a system call, a TRAP instruction would force kernel to execute the system call code inside kernel (kernel mode) on behalf of the process. Trap is a kind of exception.
The x86 int 0x80 "software interrupt" instruction is a trap, not like external interrupts. x86 uses a single table of handlers for both interrupts and exceptions; other ISAs may also do that.

Some people use this term more generally, as a synonym for "exception". eg you might say "MIPS add will trap on signed overflow, so compilers always use addu ."

Signals : signals are generated by kernel or by a process ( kill system call). They are eventually managed by the OS kernel, which delivers them to the target thread/process. Eg a divide by zero instruction would result in kernel delivering a SIGFPE signal (arithmetic exception) to the process that ran it. (For example, the x86 #DE fault is handled by the kernel, generating a software SIGFPE for the current process.)


Related:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM