Really. The x86 architecture has a concept known as interrupts . Some interrupts are triggered by hardware (and others can be triggered by software).
The kernel registers "handlers" that take care of this. If you are in kernel development, here is a tutorial that might help: http://www.osdever.net/bkerndev/Docs/gettingstarted.htm (note: some of these things can be quite complex and cover some topics that are not strictly part of your question)
When the interrupt starts, the code that is currently executing stops. Instead, the processor will execute the handler, and as soon as the handler is executed, return to the code that was executed before the interrupt was launched. For the application, it is as if the interrupt never occurred at all.
We can combine interrupt handling with, say, a hardware clock (such as a PIT chip) to get the desired result.
You can also check out http://wiki.osdev.org/PIT (again, keep in mind that some of these things can be tricky if you are just starting a topic).
Using IRQ for proactive multitasking
An IRQ timer can also be used to perform proactive multitasking. To give the current task in progress for a while to start, set a threshold value for an example of 3 ticks. Use the global variable as before, but go from 0, and when that variable reaches 3, switch tasks. How you do this is up to you.
So:
- The kernel says, โWhen the time runs out, I want this code to executeโ by registering the appropriate interrupt handler.
- The timer expires, causing an interrupt. Then the code in which the kernel is registered is executed.
- The kernel takes care of it, saves all the necessary data (such as the state of the registers and the address of the executable code) from the stopped process and gives control to another process.
- Later, when the kernel wants the first process to continue, it uses all the saved data to restore the process to its previous state. He then tells the CPU to continue executing the code from which he stopped.
luiscubal
source share