📜 ⬆️ ⬇️

How multitasking works

In response to What do you think, how does multitasking on 80386 work? . In my opinion, none of the suggested answers is correct, but true:

The processor does not know anything about threads or processes. It simply executes instructions sequentially until an interrupt occurs. Interrupt processing is reduced to a procedure call that is not specified in the executable code. This procedure (which is part of the operating system), if necessary, switches processes and threads. Most often, the interrupt routine simply makes the exit and the interrupted thread continues its work without noticing anything.

The steps of this process are elegant and instructive.

Each thread has its own stack - a memory area for storing the state of currently executed procedures. When a procedure is called, the state of the processor is written to the stack, and when it exits, it is read from there back to the processor registers. The most important element of the state is the address of the command being executed. An even more important element of the state is the stack pointer — the address at which state data is written or read. The stack pointer itself is not written to the stack.
')
Several wires lead to the processor from external devices (including the timer). A signal on any of these wires causes the processor to perform an interrupt procedure, the starting address of which is specified in the interrupt table at an index equal to the wire number.

Switching stacks is done like this: an operating system procedure that chooses to switch, saves the contents of the register pointer to the stack in the current stack header, then from the header of another stack, extracts its pointer value and writes to the register, then the usual exit command is executed. That is, a stack change is reduced to replacing the contents of the register - the stack pointer.

When switching processes, the virtual memory registers are additionally overloaded.

Many details in this description are omitted (for example, the priority of interrupts), they can be found in the relevant literature.

Soviet terminology was different from American. The flow was called a process, and the process was called a task, in accordance with the work of Dijkstra. The interaction of sequential processes .

Source: https://habr.com/ru/post/124794/


All Articles