common functions of interrupts in cpu code example

Example: what is the purpose of interrupts in os

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.