Abstract


2 Types


Signal (Software Interrupt

Hardware interrupts (外中断)

Benefits


  • We don’t need to get CPU to keep Polling for response which may take a long time to produce. Thus wasted computation

Cons


Sidenote

Interrupts should complete quickly to prevent the below problems

Generally can’t be interrupted

  • Allowed to run to completion. For example, if a keyboard’s interrupt is interrupted, we may lose the input from the keyboard
  • However, there are exceptions

Some Callback Function don’t work

Examples


Turn off interrupt in XV6-RISCV

// XV6-RISCV kernel codes, spinlock.c
 
// ...
void
push_off(void)
{
  // ..
  intr_off();
  // ...
}
// ...
  • We can see from code snippet below, to turn the interrupt is setting the second bit from right on the sstatus register
// XV6-RISCV kernel codes, riscv.h
 
// ...
#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable
 
// ...
// disable device interrupts
static inline void
intr_off()
{
  w_sstatus(r_sstatus() & ~SSTATUS_SIE);
}
// ...
// XV6-RISCV kernel codes, riscv.h
 
// ...
static inline void 
w_sstatus(uint64 x)
{
  asm volatile("csrw sstatus, %0" : : "r" (x));
}
// ..

Terminologies


Synchronous

Asynchronous