See also
Interrupts are signals sent to the CPU by external devices, normally I/O devices. They tell the CPU to stop its current activities and execute the appropriate part of the operating system.
There are three types of interrupts:
- Hardware Interupts are generated by hardware devices to signal that
they need some attention from the OS. They may have just received some data (e.g., keystrokes on the keyboard or an data on the ethernet card); or they have just completed a task which the operating system previous requested, such as transfering data between the hard drive and memory.
Software Interupts are generated by programs when they want to request a system call to be performed by the operating system.
Traps are generated by the CPU itself to indicate that some error or condition occured for which assistance from the operating system is needed.
Interrupts are important because they give the user better control over the computer. Without interrupts, a user may have to wait for a given application to have a higher priority over the CPU to be ran. This ensures that the CPU will deal with the process immediately.
See also
Audio about CPU Mode bits
There are two modes of execution, known as user mode and kernel or supervisor mode. User mode is restricted in that certain instructions cannot be executed, certain registers cannot be accessed, and I/O devices can not be accessed. Kernel mode has none of these restrictions. A system call will set the CPU to kernel mode, as will traps and interrupts. Application programs cannot do this.
Mode bit: Supervisor or User mode
A key point towards understanding how operating systems work is to understand what the CPU does when an interrupt occurs. The hardware of the CPU does the exact same thing for each interrupt, which is what allows operating systems to take control away from the current running user process. The switching of running processes to execute code from the OS kernel is called a context switch.
CPUs rely on the data contained in a couple registers to correctly handle interrupts. One register holds a pointer to the process control block of the current running process. This register is set each time a process is loaded into memory. The other register holds a pointer to a table containing pointers to the instructions in the OS kernel for interrupt handlers and system calls. The value in this register and contents of the table are set when the operating system is initialized at boot time.
The CPU performs the following actions in response to an interrupt:
The CPU uses a table and the interrupt vector to find OS the code to execute in response to interrupts. A software interrupt is shown here.
As the computer runs, processing switches between user processes and the operating system as hardware and software interrupts are received.