- Consider the case when data is to be input from the outside world.
One approach is to execute a small code fragment to manage the transfer of data in from the outside world e.g. a peripheral. But when should this code fragment be run?
- In a polling system, the computer periodically executes (or polls) the peripheral device of interest and inputs data when it is available. This means data can only be input if the peripheral device is polled.
- In an interrupt driven system, the peripheral triggers the execution of the previously mentioned code fragment when it has data ready. We say interrupt because the handling of this data transfer interrupts normal program execution. The original task (blue) has execution interrupted while a task switching occurs (green) and then the interrupt service routine runs (red) to do I/O. Later, the original task resumes.
- As the system has more than one peripheral, it will need more than one interrupt service routine available. Generally, a separate interrupt service routine (ISR) is provided for each peripheral that needs to trigger some CPU activity so an array of function pointers holds the start address of each of the ISRs provided i.e. void (
- This trigger mechanism allows a computer response to an external event - what about internal events? Most computers are also able to trigger special event handling in software by executing a special instruction called software interrupt or trap.
- The operating system gets the control of the CPU (which may be busy waiting for an event or be in a busy loop) when either an external or an internal event (or an exception) occurs.
- external events
- Character typed at console
- Completion of an I/O operation (controller is ready to do more work)
- Timer: to make sure operating system eventually gets control.
- Hardware failure
- An interrupt is the notification of an (external) event that occurs in a way that is asynchronous with the current activity of the processor. Exact occurrence time of an interrupt is not
known and it is not predictable
- internal events
- System call
- Error item (e.g., arithmetic overflow, division by zero, illegal instruction, addressing violation)
- Page fault, reference outside user's memory space
- A trap is the notification of an (internal) event that occurs while a program is executing, therefore is synchronous with the current activity of the processor. Traps are immediate and are usually predictable since they occur while executing (or as a result of) a machine instruction.
- Interrupt Cycle
- Fetch next instruction
- Execute instruction
- Check for interrupt
- If no interrupts, fetch the next instruction
- If an interrupt is pending, divert to the interrupt handler
- Systems that generate interrupts have different priorities for various interrupts; i.e., when two interrupts occur simultaneously, one is serviced ``before'' the other.
- When a new ``higher priority'' interrupt occurs while lesser interrupt is being serviced, the current handler is ``suspended'' until the new interrupt is processed. This is called the ``nesting of
- When interruption of an interrupt handler is undesirable, other interrupts can be ``masked'' (inhibited) temporarily
- Interrupt handling by ``words''. When the CPU receives an interrupt, it is forced to a different context (kernel's) and the following occur:
- The current state of the CPU (PSW) is saved in some specific location
- The interrupt information is stored in another specified location
- The CPU resumes execution at some other specific location-the interrupt service routine
- After servicing the interrupt, the execution resumes at the saved point of the interrupted program
- Although the details of the above differ from one machine to another, the basic idea remains the same: the CPU suspends its (current) execution and services the interrupt.
- Modern languages such as C++ and JAVA allow the programmer to write their own exception handlers. You are writing a special function that can return no result (since it is not so much ``called'' as ``triggered''); and the computing environment is allowing you to store the start address of your exception handler in the array
- As the computer designers have total control over which event handlers can be accessed by users and which ones are reserved for their use, the exception handling mechanism is also a good way to allow a user program to make a request for a resource from the operating system.
We will come across special operations such as a system call or monitor call which are implemented by the exception handling mechanism and provide controlled access to system resources.