Intuitive idea of the operating system managing the CPU

How the CPU Works

To manage the CPU, you must first learn how to use the CPU. Let's start with the execution of a program to see how the CPU works.

void main(){
    int i , sum;
    for(i = 0; i < 100000; i++){
        sum = sum + i;
    }
    printf("%d",sum);
}

If we want to execute the above program, first we need to put the program in the memory, and then set the PC register to the first memory address of the program, for example, set the PC to 50, and the CPU finds the address as 50 through the address bus memory, fetch the first instruction and execute it, add 1 to the PC to point to the next instruction, and the CPU fetches the address and executes it. This process is automatically executed by the CPU .

As can be seen from the above program, if you want the CPU to work, you only need to set an initial value for the PC register, and the CPU can work automatically.

what is concurrency

Let's run the following two programs separately

//程序1
void main(){
    int i , sum;
    for(i = 0; i < 100000; i++){
        sum = sum + i;
    }
    sum = sum + 1 ;

}
// The running time is: 0.324s
//程序2
void main(){
    int i , sum;
    for(i = 0; i < 100000; i++){
        sum = sum + i;
    }
    //sum = sum + 1;
    printf("%d",sum);
}
// The running time is: 0.413s

In fact, the second program replaces an operation instruction with an IO operation instruction. The above results show that the ratio of the time to execute an operation instruction to the time to execute an IO instruction is 1:10 5 , and the IO operation is much slower than the calculation operation, generally several orders of magnitude. The CPU does not work during the IO operation. If one program is executed and then another program is processed, the utilization rate of the CPU will inevitably become lower. Therefore, for the utilization of the CPU, it is very important to switch back and forth between the multi-programs to use the CPU to effectively improve the utilization of the CPU.

We call a CPU executing multiple programs alternately: concurrency .

Introducing the concept of process

We know that to improve CPU utilization, we can allow the CPU to execute multiple programs concurrently, so how to achieve concurrency?

It is necessary to switch the address of program execution to the first address of another program, and then the CPU automatically takes the address and executes it. The address switching is performed through the PC register (indicating the storage location of the instruction in the memory, that is, the address information, in the assembly, the register IP is used to represent the PC) . In fact, it is not enough to just switch the PC. It is also necessary to restore the information that was executed before the program, so each program has a structure for storing information: PCB. The process control block (PCB) is equivalent to a structure, including process state program counter, CPU registers, CPU scheduling information, memory management information, counting information, IO status and other information.

To distinguish it from a static program, we call a running program a process . Compared with static programs, the process saves all the information used to control and manage the process, so that when the program is executed again, it can restore the state of the last execution for execution.

multi-process view

We know that the core function of the operating system is to manage computer hardware resources, and how to make better use of CPU resources leads to the management of the CPU (switching, scheduling and control of the CPU by the operating system). The multi-process view (the appearance of multiple processes executing alternately on the CPU) is derived from the management of the CPU for better utilization of CPU resources.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324955067&siteId=291194637