How does the premium work in x86 architecture?

I am struggling to understand what is happening with advantage. Referring to Wikipedia:

In the calculation, a premise (a more correct lead) is the action of temporarily interrupting a task performed by a computer system, without the need for its cooperation, and with the intention to resume the task later. This change is called a context switch. This is usually accomplished using a privileged task or part of the system known as the proactive scheduler , which can preempt or interrupt and then resume other tasks in the system.

So basically, they say that the scheduler can interrupt the current current task. How is this even possible? Currently, the CPU is executing the code for this task, not the scheduler code. So how can a planner do something?

I assume that there should be some kind of hardware timer that physically interrupts the CPU after some time and returns control to the scheduler. It's right? Is there any documentation where I can read more about this?

Any answers would be highly appreciated.

+7
source share
2 answers

Your guess is correct. Most operating systems have a timer interrupt that runs privileged code in the kernel at some fixed frequency. This privileged code can decide either (a) to return to the source code that was originally run, or (b) save the context and start the execution of another code.

There are other conditions that can cause a context switch, such as a request to read from I / O when a process has to wait for I / O. The kernel will probably switch to another task while the first one waits.

You may also be interested in reading about the so-called nuclear - free core .

+3
source

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.
+2
source

All Articles