Monday, June 4, 2012

Interrupts and Exceptions

Unix/Linux systems allow devices such as I/O peripherals or the system clock to interrupt the CPU asynchronously. On receipt of an interrupt, the kernel saves it's current context, determines the cause of interrupt and services it. 

So what do we mean by "kernel saves it's current context" here?  

When a system is executing a process, the system is said to be executing in the context of a process. When the kernel decides to execute another process, it does a context switch, so that the system can execute in the context of another process. The kernel services interrupts in the context of the interrupted process even though it may not have caused the interrupt. The interrupted process may have been executing in user mode or kernel mode. The kernel saves enough information about the interrupted process so that it can later resume it's execution. An Interrupt is handled in the context of current process(the interrupt is handled in kernel mode) and the kernel does not spawn or schedule a special process to handle interrupts. Thus while handling an interrupt no context switch happens.

What happens if multiple devices send interrupts at same time to the kernel OR if an interrupt  is received during the execution of a critical region of a process?
  • The hardware usually prioritizes the devices according to the order that the interrupts should be handled. So when the kernel receives an interrupt, it services higher priority interrupts  by blocking out lower priority interrupts. 
             Typical interrupt levels from lower priority(left) to higher priority (right)

             Software Interrupts ---> Terminals --> Network Devices --> Disk --> System Clock ---> Machine Errors
  • Setting processor execution levels to mask off interrupts while handling critical regions of a process - If kernel receives an interrupt while executing a critical region of code, say like manipulating a linked list which involves pointers, kernel will raise the processor execution level so that interrupts at that level and lower levels are masked off, allowing only higher-level interrupts. Critical regions of code are usually small and infrequent.
What is an exception and how does it differ from interrupt?

An exception condition refers to unexpected events caused by a process, such as addressing illegal memory, executing privileged instructions, dividing by zero, and so on. 
  • They are distinct from interrupts, which are caused by events that are external to a process. 
  • Exception occurs in the middle of execution of an instruction and the system attempts to restart the instruction after handling the exception; interrupts are considered to happen between the execution of two instructions, and the system continues with the next instruction after servicing the interrupt.
How to find the interrupts being processed in the system?

The field "in" in vmstat command under "system" gives the interrupts being processed in the system.

No comments:

Post a Comment