4

So I wanted to know the differences between these two. I know software interrupts are sometimes referred to as exceptions, which makes the differences between the two somewhat confusing. Asking this entire question from a program level perspective; wouldn't an exception just be an illegal action whereas a software interrupt may not be?

Firstly, am I understanding this much correctly and furthermore are there other differences that can be drawn between the two?

9

Allow me to expand on @Tymski's nice answer.


Let's start with a review of hardware interrupts.

These are can occur at any time (assuming the are enabled) and are thus asynchronous to the current execution stream. The CPU accepts hardware interrupts by listening to external lines in parallel with instruction stream execution. When an interrupt request is detected on these external lines the processor allows execution to continue to advance until a good breaking point. For example, a pipelined processor will allow the instructions already started to finish, though not starting any new instructions. When it is ready, the CPU will start interrupt processing. This can be a relatively complex process, also sometimes, or some parts thereof, referred to as a context switch. User mode execution ceases. Privileged mode is entered (i.e. the OS). The current context is saved -- CPU registers holding user mode values, mostly; the location to save is usually somewhere in the privileged context. The processor then gives flow of control to a low level interrupt handler. The privileged context interrupt handler then determines the next stream to execute, such as an appropriate somewhat higher level interrupt handler.


A software interrupt is very similar in mechanism, with the main difference being that it occurs by the execution of a software interrupt instruction, sometimes called a trap. So, these occur synchronously to the currently executing instruction stream. The same general context switch from user mode to privileged mode is performed borrowing the same hardware, which is one reason it is called an interrupt. These traps are typically used for user mode code to accomplish system calls.


A software exception can refer to the same thing, except rather than being triggered by software interrupt instruction, it is triggered by abnormal condition detected by the CPU in the current instruction stream execution, such as null pointer dereference or integer divide by zero.


An exception, the term used alone, usually refers so a programming language mechanism for detecting and handling synchronous errors in the current thread. These can involve software exception (e.g. be triggered by the CPU for certain instructions), or can be detected by explicit test inserted into the execution code by a compiler or JIT, or by user or library software that explicitly throws.


As @Tymski says, these terms are often interchanged, sometimes erroneously, but also sometimes due to context.

  • What helpful answers by both @Tymski and yourself. Thank you! Would I be right in understanding that a software interrupt due would only "talk" to the kernel itself because it will only be used at a lower level, whereas exceptions may have many levels of communication (i.e. with the compiler itself) at an application level? – bkbles Oct 24 '16 at 11:41
  • 1
    Yes, I think so, if I understand the follow-up. Of course a software interrupt could cause an exception in the OS (e.g. bad parameter), which ultimately might return control to a user mode handler.. – Erik Eidt Oct 24 '16 at 15:12
  • So if I was to summarise differences: (1) Exceptions can only be for "illegal" actions vs Software Interrupt can be for "illegal" or "legal" actions (2) Software Interrupts talk only to kernel vs Exceptions will deal with compiler/exception handlers in code (3) Software Interrupts used at low level programming vs Exceptions used at higher level programming. Would there be anything else I can take away as differences? – bkbles Oct 26 '16 at 15:55
  • 1
    (1) Yes. Exceptions, both language level and instruction level, are for aborting the current flow of control due to a specific (e.g. illegal) attempted action on invalid state, and invoking a handler. (2) Yes, though the kernel may be involved in (language level) exceptions under the hood. – Erik Eidt Oct 26 '16 at 16:30
2

While an interrupt and an exception conceptually are similar, they exist on different levels. Having said that, to confuse all of us, the terms are sometimes used interchangeably.

I think of interrupts as low level (close to hardware) and exceptions as high level (the way we write code in high level languages like Java or C#).

In other words, I see it simply like this:

  • if you are a low level programmer (and write drivers for example) you deal with interrupts.
  • if you are an application programmer and program in high level languages you will talk about exceptions and exception handling.

This is somewhat similar to a difference between a routine and a function - they mean the same thing, but are used by different kinds of engineers.

  • 1
    This is not entirely correct. When you execute a div instruction where the divisor is 0, for example, the processor raises a divide-by-zero exception which is a kind of interrupt. – user253751 Oct 23 '16 at 23:47
  • 1
    @immibis I'm not sure if your comment is relevant to my explanation. In application code the programmer would deal with a divide-by-zero exception and it's not really relevant if that was thrown by the processor as "an interrupt" and then translated or by another programmer checking the divisor and manually throwing with "throw new DivideByZeroException". – tymtam Oct 24 '16 at 1:33
  • But if the asker wanted an answer about application code, s/he wouldn't have asked about interrupts because those don't exist in application code. – user253751 Oct 24 '16 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.