What all cant be there in an Interrupt service rou

2019-08-27 19:11发布

问题:

I know that an ISR needs to be very quick and should handle an interrupt very quickly. But I do not understand the reason for the same. Why should this condition be met? Moreover, in order to do so are there any restrictions on what all can an ISR code have? Typically, what all should not be included in the code of an ISR?

Thanks

回答1:

I know that an ISR needs to be very quick and should handle an interrupt very quickly. But I do not understand the reason for the same

The most stringent requirement is that an ISR can't block - it can't sleep. So you can't use semaphores for example.

Each interrupt has something to do with hardware. Think of a network device. It has a really tiny memory and yet it receives frames at high speed. Your had better copy quickly stuff from it into main memory, or the frames will overwhelm the device and it will spill frames.



回答2:

When an ISR executes, no threads can execute on the same CPU, so thread scheduling is affected and the longer it executes, the bigger impact. Depending on the implementation (software and hardware), all interrupts (or lower priority interrupts) may be disabled while an ISR executes, which means if an ISR takes a lot of time to complete, other interrupts may not be handled fast enough, meaning there will be latencies or even missed interrupts.

There are a few other things to take into account:

  1. using locks in ISRs
  2. touching paged memory in ISRs

If you acquire a lock in an ISR that can be already locked, you may enter an infinite loop if there's no other ISR or CPU to release that lock.

If you touch a page of memory that's swapped to the disk, you may run into a similar situation as the page fault handler may not be able to bring that page from the disk to the memory if it does that in a thread and that thread cannot execute because of being lower priority compared to ISRs and being never scheduled.