In an Event-Driven OS, the occurrence of an event is usually signaled by an interrupt from either the hardware or software. Hardware may trigger an interrupt at any time by sending a signal to the CPU, usually by way of the system bus. Software may trigger an interrupt by executing a special operation called a system call (ex: in I/O operation). System call is also called monitor call.
When the CPU is interrupted, it stops what it is doing and immediately transfer execution to a fixed location. The fixed location usually contains the starting address where the service routine for the interrupt is located. The interrupt service routine executes; on completion, the CPU resumes the interrupted computation.
As you see above, interrupt cause a Context switch. It’s always cost resource for saving and restore CPU’s state, thus make an si
- Hardware interrupts are issued by hardware devices like disks, network cards, keyboards, clocks, etc. Each device or set of devices will have its IRQ (Interrupt ReQuest) line. Based on the IRQ the CPU will dispatch the request to the appropriate hardware driver. (Hardware drivers are usually subroutines within the kernel rather than a separate process.)If hardware needs to get the CPU to do something, it causes the CPU to interrupt its attention to the code it is running. The CPU will push its current state on a stack so that it can later return to what it was doing. The interrupt could stop: a running program; the kernel code handling another interrupt; or the idle process
- Typically software interrupts are requests for I/O (Input or Output). These will call kernel routines which will schedule the I/O to occur. For some devices the I/O will be done immediately, but disk I/O is usually queued and done at a later time. Depending on the I/O being done, the process may be suspended until the I/O completes, causing the kernel scheduler to select another process to run. I/O may occur between processes, and the processing is usually scheduled in the same manner as disk I/O. This is why buffered I/O is preferred than byte stream I/O in programming: It require much fewer system calls.
You may imagine interrupt’s demonstration somewhat like Exception in programming. Indeed, Interrupts and Exceptions both alter program flow and thus cause context switch. I wrote an article about this in Java. But there are three significant differences between them:
- Exception is synchronous while interrupt is asynchronous
- Interrupt has more priority than exception. And because interrupt usually executes with the highest priority, it makes a significant overhead. Again: Think twice before write an exception handler in programming.
- Exception is software generated while interrupt is mostly hardware generated.