Interrupts and exceptions

2019-01-16 09:21发布

I've seen several question on here about exceptions, and some of them hint at interrupts as exceptions, but none make the connection clear.

  • What is an interrupt?

  • What is an exception? (please explain what exceptions are for each language you know, as there are some differences)

  • When is an exception an interrupt and vice-versa?

11条回答
一夜七次
2楼-- · 2019-01-16 10:04

In general, an interrupt is a hardware implemented trap of some sort. You register a handler for a specific interrupt (division by 0, data available on a peripheral, timer expired) and when that event happens, all processing system-wide halts, you quickly process the interrupt, and things continue on. These are usually implented in a device driver or the kernel.

An exception is a software implemented way of handling errors in code. You set up a handler for specific (or general) exceptions. When an exception occurs, the languages run-time will start unwinding the stack until it reaches a handler for that specific handler. At that point you can handle the exception and continue on, or quit your program.

查看更多
叼着烟拽天下
3楼-- · 2019-01-16 10:05

Keeping things simple...

When you are done handling an interrupt, you (normally) return to what you were doing before you were interrupted.

Handling an exception involves throwing away successive layers of what you are currently working on until you bubble up to a point where the exception can be handled (caught).

While handling an interrupt, you may decide to throw an exception, but that doesn't mean you have to consider the interrupt itself as an exception. Exceptions do not "interrupt" (since that would imply the possibility of returning to what you were doing just before you were interrupted); rather they "abort" (some subset of) your current activity.

And, as noted several times already, interrupts are usually triggered by outside entities such as hardware or users (such as a mouse click or keystroke like CTRL-C) while exceptions are generated (thrown) synchronously by software detecting a "problem" or "exceptional condition".

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-16 10:07

Interrupts indicate that something external to the processor core requires it's attention. It interrupts the normal flow of the program, executes an Interrupt Service Routine (ISR) and generally returns to where it was before the interrupt occurred.

There are lots of variation on this basic theme: interrupts might be generated by software, another task might get the CPU after the ISR, etc.. The key point is that interrupts can occur at any time for a reason the code/CPU has no control over.

An exception is a bit trickier to define because it has potentially three levels of meaning:

Hardware Exceptions

Certain processors (PowerPC for example) define exceptions to indicate that some sort of unusual condition has occurred: System Reset, Invalid Address, some virtual address translation cache miss, etc...

These exceptions are also used to implement breakpoints and system calls. In this case, they act almost like interrupts.

OS Exceptions

Some of the hardware exceptions will the handled by the OS. For example, your program accesses invalid memory. This will cause a hardware exception. The OS has a handler for that exception, and odds are that the OS will send a signal to your application (SIGSEGV for example) denoting there is a problem.

If your program has a signal handler installed, the signal handler will run and hopefully deal with the situation. If you don't have a signal handler, the program can be terminated or suspended.

I would consider window's Structured Exception Handlers (SEH) to be this type of exceptions.

Software Exceptions

Some languages like Java, C++ and C# have the concept of software exceptions, where the language provides for the handling of unforeseen or unusual conditions related to the operation of the program. In this case, an exception is raised at some point in the code and some code higher up on the program execution stack would "catch" the exception and execute. This is what try/catch blocks do.

查看更多
再贱就再见
5楼-- · 2019-01-16 10:11

When you are talking about interrupts and exceptions you are generally talking close to hardware level code and interrupts and exceptions are often implemented in part by hardware and part in software.

An interrupt is an event in hardware (or manually fired in assembly) that is associated with a vector of handlers that can be used to handle the interrupt's event, be it IO Completion, IO Error (Disk Memory Failure), IO Event (Mouse Move for example). The interrupts can give rise to exceptions often when some unexpected interrupt occurs.

An exception is an unexpected behavior, most often when using the hardware these come from an interrupt and are handled separately in the software using an interrupt handler. Programming languages as we see them almost always disguise this as a control structure of some kind.

查看更多
女痞
6楼-- · 2019-01-16 10:12

Interrupts are expected to occur regularly (although sometimes they are not regular).. they interrupt the cpu because something important just happened and it needs to be taken care of immediately.

Exceptions are supposed to be exceptions to the rule; these are thrown by software because something unexpected happened and this is your chance to try to do something about it, or at the very least crash gracefully.

查看更多
登录 后发表回答