multi-process image

1. Intuitive idea of ​​CPU management

1. The core of the operating system
The core of the operating system is to manage the computer hardware. Obviously, the CPU is the core hardware in a computer system. When the operating system manages the CPU, it leads to a multi-process image. The CPU is managed through the multi-process image operating system, and thus other hardware is also managed.
2. How to manage the CPU?
To manage the CPU, you must first use the CPU.
1). So how does the CPU work?
When an initial PC address is given, the CPU automatically fetches instructions from the memory for execution.
2). The most intuitive way to manage the CPU is to set the initial value of the PC. But is there any problem with this?
Example 1:
As the following code, when the CPU executes this code, the PC pointer will first point to the address of int main(), and then execute the code segments sequentially.

int main(int argc,char *argv[]){
    
    
    int i,to,*fp,sum=0;
    to=atoi(argv[1]);
    for(i=1;i<=to;i++){
    
    
       sum=sum+i;
       fprintf(fp,"%d",sum);
    }
}

We can replace fprintf() with another calculation statement. What is the execution speed of these two programs at this time?
insert image description here
The running speed of the first program: 0.015/10^7 (seconds/each time)
The running speed of the second program: 0.859/10^3 (seconds/each time)
0.015/107 : 0.859/103is equal to5.7x10 5 : 1which is equivalent to106 : 1
Then it means that performing 10 6 calculations is equivalent to performing one IO operation. IO operations need to go through a series of processing such as disks, so its execution process is slow.
Example 2:
There is a code segment as shown below.
According to the idea that the CPU automatically fetches and executes instructions from the memory, it will be realized in this way:
insert image description here
in the process of IO operation, the CPU does not need to do it. According to calculation speed: IO operation speed = 10 6 : 1, if the entire code segment takes 4 seconds in total, the CPU will only run for 2 seconds. CPU utilization is 50%. However, when actually writing the code, one IO operation may be required after 20 calculations. At this time, the utilization rate of the CPU is only
(20 x 1/10 6 )/ (20 x 1/10 6 +10 6 ). All in all it is very low.
Example 3:
What do we usually do when we boil hot water?
First pour cold water into the kettle, and then put the kettle on the stove. At this time, we can do other things. When the kettle beeps, it is equivalent to reminding us that the water is boiling, and then we deal with it and use the hot water to do other things.
We are equivalent to CPU, and the stove is equivalent to other equipment. When the CPU wants to complete a job, it does not use its hands, but uses other tools to realize the work. During this entire period of time, the CPU can do other work, just wait for the results of the completion of the work to process the completion of the work.
3. Multiple programs are executed alternately
as shown below:
insert image description here
Then we can calculate the utilization rate of each hardware:

single program multiprogramming
CPU utilization 40/80=50% 40/45=89%
DEV1 utilization 15/80=18.75% 15/45=33%
DEV2 utilization 25/80=31.25% 25/45=56%

4. Alternately execute multiple programs on one CPU: Concurrency. How is concurrency achieved?
First of all, one thing that needs to be clarified is: when going from program 1 to program 2, the OS modifies the PC pointer so that it points to the first address of program 2. But is it enough to just modify the register PC?
insert image description here
If you just switch PCs, the information of program 1 is like this when the PC is switched out: when the PC is
insert image description here
switched back, the information of program 1 becomes like this:
insert image description here
Therefore, the program needs to be saved when the PC is switched out to program 2 1 The current information, including data, etc. Every program has a structure for storing information: PCB. So that the information presents this effect:
insert image description here
To sum up: the running program is different from the static program. The biggest difference is that the running program needs to record data and other information during the running process, while the static program does not. All the differences are stored in the PCB. To describe these differences, we need to use a concept to distinguish running programs from static programs. Then the running program is called a process, and a running program is called a process.
The running program and the static program can also be understood through a small story:
just like we read a book, it is like a static program before reading this book, and we do not need to record anything. And when we read a book and read dozens of pages, we suddenly have other things to do. Before doing other things, we must first record how many pages we have read now, so that we can start from here after doing other things. Start reading on one page. This becomes the running program.

2. Multi-process image

In order for the CPU to execute more efficiently, it is not enough to execute one process after another. It is necessary to run multiple processes so that they can be executed alternately.

1. What is a multi-process image?
In the eyes of upper-level users,
multi-process means that the user starts multiple processes, and they start or stop according to their own wishes.
In the eyes of the operating system,
multi-process is the OS needs to manage, record the status of each process, and promote multiple processes according to the PCB.
Summary: The operating system needs to record the information of multiple processes and advance them in a reasonable order (that is, allocate resources, perform scheduling, etc.), which is a multi-process image.

2. The existence time of the multi-process image is from the start of the computer to the end of the shutdown. How are so many processes executed in the computer?

  • (1) fork() in main creates the first processif(!fork()) {init();}
  • (2) init executes the shell
  • (3) The shell starts other processes again.
    A piece of code in the shell is as follows:
    it means that entering a command starts a process. Return to the shell and start other processes.
    int main(int argc,char *argv[]){
          
          
        while(1){
          
            
            scanf("%s",cmd);
            if(!fork()){
          
            
                 exec(cmd);
            }
            wait();
        }
    }
    

3. How to view multiple processes in the computer?
Open the Windows task manager to see the CPU, memory, disk, network, etc. occupied by each process.
We can find that the operating system manages the use of the computer by the user through the management process. When a computer performs a task, it actually starts a process, and the end of a process is the completion of a task. And the computer starts to work by starting a process. So it further shows that the multi-process image is the core image of the operating system.

4. How to organize multi-process?
(1) Steps:

  • There is a process executing
  • There are some processes waiting in the ready queue to execute
  • Some processes are waiting for an event, such as some processes are waiting in the disk waiting queue to read data from the disk.

(2) Process state diagram
Multi-process organization: PCB (Process Control Block: data structure used to record process information) + state + queue
insert image description here
This diagram is called a process state diagram.
It can give a clear description of the process lifetime.
It is a window into the process management of the operating system.
5. How do multiple processes alternate?
(1) Steps:

  • When starting a disk read and write process,
  • pCur.state = ‘W’;
  • Put pCur in the blocking queue;
  • Execute schedule(); (toggle)

A piece of code in the schedule is as follows:
The meaning is that pNew gets the PCB of the next process in the ready queue. Then switch pCur to pNew, and execute the process displayed by pNew.
schedule()
{
pNew = getNext(ReadyQueue); (scheduling)
switch_to(pCur,pNew);
}

(2) Alternate three parts: Queue operation + scheduling + switching A.
The introduction of getNext()
is also the scheduling of the process.
The following are two simple schedules:
1.FIFO

  • FIFO is clearly a fair strategy
  • FIFO obviously does not take into account the difference in the tasks performed by the process

2.priority

  • How should the priority be set? It may starve some processes

B. How is switch_to() executed?
Pseudocode:
switch_to(pCur,pNew) {
pCur.ax = CPU.ax;
pCur.bx = CPU.bx;
...
pCur.cs = CPU.cs;
pCur.retpc = CPU.pc;
CPU.ax = pNew.ax;
CPU.bx = pNew.bx;
...
CPU.cs = pNew.cs;
CPU.retpc = pNew.pc;
}
But if you want to accurately describe the information, you must use assembly code.
6. How does it affect the process?
Assuming that multiple processes exist in memory at the same time, the following problems may occur:
insert image description here

Solution: Separation of address spaces for multiple processes.
The address space separation of multiple processes is the main content of memory management.
As shown below:
insert image description here
In this way:
the mapping table of process 1 restricts access to the scope of process 1,
process 1 cannot access the contents of other processes at
all, and each process can coexist in memory. for better management.

7. How do multiple processes cooperate?
(1) Think about the printing process. This is multi-process cooperation:

  • The application submits the print job
  • The print job is put into the print queue
  • The print process takes jobs from the queue
  • Print process control printer print

(2) An example:
Take the printing process as an example. The producer process is the task of the application, and the consumer process is the printing process.
insert image description here
Then it is also possible to proceed like this: the consumer process starts to execute after the producer process executes half, and then switches to the producer process after half of the execution. Then the two cooperating processes must modify the counter. As follows:
P.register = counter;
P.register = P.register + 1;
C.register = counter;
C.register = C.register - 1;
counter = P.register;
counter = C.register;
Finally counter=4.
However, according to common sense, it should be executed after the producer process is completed, and the consumer process is executed again, and the counter is finally equal to 5

How to solve this contradiction?
We need to understand that during the execution of the producer process, another process cannot be switched at will, and another process will be executed only after its execution is completed. The core lies in process synchronization (that is, a reasonable advancement sequence).
The method is: block other processes from accessing the counter when writing the counter. Specific steps are as follows:
insert image description here

Guess you like

Origin blog.csdn.net/qq_44875145/article/details/104849004