This is the third day of my participation in the August Text Challenge.More challenges in August


Without an operating system, there is usually only one program running, which takes up 100% of the CPU, memory, storage, and everything else. But in modern operation habits, usually need to run multiple programs at the same time. During program execution, the CPU is responsible for executing the instructions written in the program. So running multiple programs at the same time, how does the CPU correctly execute the instructions of each program?

The core problem of CPU virtualization is how to simulate the effect of multiple cpus with a single CPU, that is, CPU reuse. There are two ways to achieve reuse, time division multiplexing, which divides 60 seconds into two parts, 60 seconds before the execution of A program, 60 seconds after the execution of B program, and the next 60 seconds, so as to achieve the reuse effect, and the split time is small to A certain program, the program will feel its own EXCLUSIVE CPU. Another approach is physical splitting, such as running two programs that physically split the CPU in two, one for each program.

Physical isolation is basically not seen, whether it can be achieved is another matter, only to explore TDM virtualization.

TDM scheme design

We’ve agreed on the general idea and it seems pretty simple: process A executes first, process B executes.

Thus, the question arises: how do I interrupt the CPU from executing A to executing B when process A is executing? Interrupt CPU execution and regain CPU execution permission can be realized through the interrupt of hardware, CPU hardware can set interrupt program, when the interrupt can jump to execute interrupt program. By interrupting the program, you can stop the execution of process A. But how does process B start?

Review the process execution process.

  1. Loads a process executable from hard disk into memory. Executable files store code instructions for the process and required data.
  2. Allocate the required stack space for the process
  3. The CPU reads code instructions from memory and repeats the action until the process terminates.

So, if YOU want B to start, you also need to write a piece of code in the interrupt routine to load and run the process. The process from stopping one process to executing another is now implemented. The next question is when process B stops, how do I get process A to resume its original execution? Therefore, when A is interrupted to stop execution, need to save the current A process to execute several instructions, the current stack variables should also be saved. These variables are then restored to resume execution when executed again to process A. This process is called a context switch, and the interrupt is called the operating system.

Where are the context variables stored during context switching? Disks are definitely not practical because disk IO and CPU operations are not at the same level and are usually stored in memory.

User mode vs. kernel mode

You think this is the end of it? This is just the beginning. AB two processes are running. For example, both disk writes and the same file. This obviously produces indescribable data. To do this, you want to have a middleman who can mediate that while one process is writing to the file, no other process is writing to the file. In addition to disk resources, network bandwidth, CPU, memory and other resources also want to be controlled.

To meet the above requirements, some important operations, such as reading and writing disks, network cards, and allocating memory, can only be performed by the operating system. When other processes perform such operations, hardware interrupts will be triggered and the corresponding operating system handlers will be found to execute the operations (there is a mapping table for the handlers allocated in advance, which is called the trap table).

The environment in which a program runs with restrictions is called user mode, while the mode in which it runs without restrictions is called kernel mode. Of course, going from user mode to kernel mode requires two process switches before returning.

Process scheduling

The problem becomes more complicated when there are more processes that need to be executed simultaneously. When there are 10 processes executing at the same time, which of the 10 should the operating system choose to execute after the interrupt returns CPU execution permissions to the operating system? The following describes some scheduling policies.

Policy metrics. The quality of a policy can be determined by the turnaround time (task completion time – task arrival CPU time). The lower the turnaround time, the better the CPU scheduling strategy.

FIFO

First in, first out. It’s kind of simple. But its disadvantage is that in some cases the turnaround time is long. Because if you have three processes a, b, and c, execute 100,10,10 seconds. Execution of a first results in a longer turnaround time for processes B and C.

Minimum task priority

Since the above example is insufficient, design a priority queue where the process with the shortest time executes first and the process with the longest time executes later. To do this, the first thing we need to solve is how to determine the duration of the task. How to evaluate the process time is not clear to me.