Unix & Linux: How is an Interrupt handled in Linux? (4 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-19

Unix & Linux: How is an Interrupt handled in Linux?


The Question: I just know that Interrupt is a hardware signal assertion caused in a processor
pin. But I would like to know how Linux OS handles it.
What all are the things that happen when an interrupt occurs?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 40 people ==
Here's a high-level view of the low-level processing. I'm describing a simple
typical architecture, real architectures can be more complex or differ in ways
that don't matter at this level of detail.
When an http://en.wikipedia.org/wiki/Interrupt occurs, the processor looks if
interrupts are masked. If they are, nothing happens until they are unmasked.
When interrupts become unmasked, if there are any pending interrupts, the
processor picks one.
Then the processor executes the interrupt by branching to a particular address
in memory. The code at that address is called the http://en.wikipedia.org/wiki/
Interrupt_handler. When the processor branches there, it masks interrupts (so
the interrupt handler has exclusive control) and saves the contents of some
registers in some place (typically other registers).
The interrupt handler does what it must do, typically by communicating with the
peripheral that triggered the interrupt to send or receive data. If the
interrupt was raised by the timer, the handler might trigger the OS scheduler,
to switch to a different thread. When the handler finishes executing, it
executes a special return-from-interrupt instruction that restores the saved
registers and unmasks interrupts.
The interrupt handler must run quickly, because it's preventing any other
interrupt from running. In the Linux kernel, interrupt processing is divided in
two parts:
 * The "top half" is the interrupt handler. It does the minimum necessary,
 typically communicate with the hardware and set a flag somewhere in
 kernel memory.
 * The "bottom half" does any other necessary processing, for example
 copying data into process memory, updating kernel data structures, etc.
 It can take its time and even block waiting for some other part of the
 system since it runs with interrupts enabled.
As usual on this topic, for more information, read http://lwn.net/images/pdf/
LDD3/; chapter_10 is about interrupts.

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Zelda (https://unix.stackexchange.com/users/52518), user ZarathustrA (https://unix.stackexchange.com/users/56622), user wag (https://unix.stackexchange.com/users/3069), user trukna (https://unix.stackexchange.com/users/48340), user Sen (https://unix.stackexchange.com/users/2871), user Gilles 'SO- stop being evil' (https://unix.stackexchange.com/users/885), and the Stack Exchange Network (http://unix.stackexchange.com/questions/5788). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.