Basics of How Operating Systems Work
Role of Interrupts
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:
1. Hardware Interrupts 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 a 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.
2. Software Interrupt are generated by programs when they want to request a system call to be performed by the operating system.
3. Traps are generated by the CPU itself to indicate that some error or condition occurred for which assistance from the operating system is needed.
CPU Execution Mode
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 cannot 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
*Can execute all machine instructions<br>
Can reference all memory locations*
Can only execute a subset of instructions
Can only reference a subset of memory locations.
CPU Response to Interrupts
A key point towards understanding how operating systems work is to understand what the CPU does when an interrupts 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 the 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:
1. Using the pointer to the current process control block, the state and all register values for the process are saved for user when the process is later restarted.
2. The CPU mode bit is switched to supervisory mode.
3. Using the pointer to the interrupt handler table and the interrupt vector, the location of the kernel code to execute is determined. The interrupt vector is the IRQ for hardware interrupts(read from an interrupt controller register) and an argument to the interrupt assembly language instruction for software interrupts.
4. Processing is switched to the appropriate portion of the kernel.