Sunday 13 November 2011

What are interrupts? in C programming

What are interrupts?

First of all, there are hardware interrupts and there are software interrupts. Interrupts provide a way for
different hardware and software “pieces” of the computer to talk to each other. That is their purpose, but what are they, and how do they perform this communication?

The CPU (Central Processing Unit), which in the PC’s case is an Intel or clone 80x86 processor, has several pins on it that are used to interrupt the CPU from its current task and make it perform some other task. Connected to each interrupt pin is some piece of hardware (a timer, for example) whose purpose is to applya specific voltage on that CPU interrupt pin. When this event occurs, the processor stops executing the software it is currently executing, saves its current operating state, and “handles” the interrupt. The processor has already been loaded with a table that lists each interrupt number, and the program that is supposed to be executed when that particular interrupt number occurs.

Take the case of the system timer. As part of the many tasks it has to perform, the PC must maintain the time of day. Here’s how it works: A hardware timer interrupts the CPU 18 times every second. The CPU stops what it is doing and looks in the interrupt table for the location of the program whose job it is to maintain the system timer data. This program is called an interrupt handler, because its job is to handle the interrupt that occurred. In this case, the CPU looks up interrupt eight in the table because that happens to be the system timer interrupt number. The CPU executes that program (which stores the new timer data into system memory) and then continues where it left off. When your program requests the time of day, this data is formatted into the style you requested and is passed on to you. This explanation greatly oversimplifies how the timer interrupt works, but it is an excellent example of a hardware interrupt.

The system timer is just one of hundreds of events (as interrupts are sometimes called) that occur via the interrupt mechanism. Most of the time, the hardware is not involved in the interrupt process. What I mean by that is that software frequently uses an interrupt to call another piece of software, and hardware doesn’t have to be involved. DOS and the BIOS are two prime examples of this. When a program opens a file, reads or writes data to it, writes characters to the screen, gets a character from the keyboard, or even asks for the time of day, a software interrupt is necessary to perform each task. You might not know that this is happening because the interrupts are buried inside the innocuous little functions you call (such as getch(), fopen(), and ctime()). In C, you can generate an interrupt using the int86() or int86x() functions. The int86() and int86x() functions require an argument that is the interrupt number you want to generate. When you call one of these functions, the CPU is interrupted as before and checks the interrupt table for the proper program to execute.

In these cases, typically a DOS or BIOS program is executed. Table XIV.6 lists many of the common
interrupts that you can call to set or retrieve information about the computer. Note that this is not a complete list and that each interrupt you see can service hundreds of different functions.

Common PC interrupts.
--------------------------------------------------------------------------------------------------------------
Interrupt (hex)                 Description
---------------------------------------------------------------------------------------------------------------
5                                     Print Screen Services
10                                   Video Services (MDA, CGA, EGA, VGA)
11                                   Get Equipment List
12                                   Get Memory Size
13                                    Disk Services
14                                   Serial Port Services
15                                    Miscellaneous Function Services
16                                    Keyboard Services
17                                    Printer Services
1A                                   Time of Day Services
21                                    DOS Functions
2F                                    DOS Multiplex Services
33                                    Mouse Services
67                                    EMS Services
-----------------------------------------------------------------------------------------------------------------

Now that you know what an interrupt is, you might realize that while your computer is just sitting idle, it is probably processing dozens of interrupts a second, and it is often processing hundreds of interrupts each  second when it’s working hard. FAQ XX.12 includes an example of a program that enables you to write your own interrupt handlers so that you can have two programs talk to each other through the interrupt. Check it out if you find this stuff fascinating.

Cross Reference:

XX.12: How can I pass data from one program to another?

No comments:

Post a Comment