Basic knowledge of the process The whole family has pictures and stories

Note: This article is reproduced with the author Kobayashi's coding
story:
We write a line of code, in order to make it work, we have to send it to the city (in process), then since we are in the city, we must not do anything wrong.

People in the city follow the rules of the people in the city. There is a city management (operating system) that specifically governs you. People let you rest and then you work. After all, there is only one booth (CPU), and everyone has to occupy this. The stalls came to work, and there were too many people in the city to work.

Therefore, for the sake of fairness, urban management uses a strategy (scheduling) method to give everyone a fixed working time (time slice). When the time is up, it will notify you to take a rest and change another person to work.

In addition, you must not be lazy during the break. Remember where your work is. Otherwise, when you work next time, you forget where your work is. How can you continue?

Some people may also work in the county (thread), which is relatively easy here. When resting, there are relatively few things to remember, and they can also share the resources in the city.
The topic:
Insert picture description here
Insert picture description here
The code we write is just a static file stored on the hard disk. After compilation, a binary executable file will be generated. When we run this executable file, it will be loaded into the memory, and then the CPU will execute the program. Each instruction, then this running program, is called a "process" .
Now we consider that a program that reads file data from the hard disk is executed. Then when the instruction to read the file is executed, the data will be read from the hard disk. However, the read and write speed of the hard disk is very slow. At this time, if the CPU foolishly waits for the hard disk to return data, the CPU utilization is very low.

To make an analogy, when you go to boil water, do you foolishly wait for the kettle to boil? Obviously, children will not wait stupidly. We can do other things before the kettle boils. When the kettle is boiling, we will naturally hear the sound of "dididi", so pour the boiling water into the water cup.

Therefore, when the process wants to read data from the hard disk, the CPU does not need to block waiting for the return of the data, but to execute another process. When the hard disk data returns, the CPU will receive an interrupt, so the CPU will continue to run the process.
Insert picture description here
This idea of ​​multiple programs and alternate execution has the initial idea of ​​CPU managing multiple processes.

For a system that supports multiple processes, the CPU will quickly switch from one process to another, during which each process runs for tens or hundreds of milliseconds.

Although a single-core CPU can only run one process at a certain moment. But during 1 second, it may run multiple processes, which creates the illusion of parallelism. In fact, this is concurrently sent Insert picture description here
to dinner time. A couple of young couples are groaning in their stomachs, so the boys act on their own and want to give The girl was cooking dinner, so he looked for the recipe of spicy chicken on the Internet, and then bought some chicken, chili, spices and other ingredients, and then made this dish while watching and learning. Insert picture description here
Suddenly, the girl said that she wanted to drink Coke, so the boy had to pause the cooking, and marked which step to do in the recipe of the mobile phone, and recorded the status information.

Then the boy followed the girl's instructions, ran downstairs to buy a bottle of ice-cola, and then went back to the kitchen to continue cooking.

This reflects that the CPU can switch from one process (cooking) to another process (buying Coke). Before switching, it is necessary to record the status information of the current process, so that the execution can be resumed when the next switch comes back.

Therefore, it can be found that the process has an activity law of "run-pause-run".
The state of the process
In the above, we know that the process has a "run-pause-run" law of activity. Generally speaking, a process does not run continuously from beginning to end, and it restricts the execution of other processes in concurrent execution.

Sometimes it is in the running state, and sometimes it is in a waiting state when it is suspended for some reason. When the reason for the suspension disappears, it enters the ready-to-run state. Insert picture description here
Let me explain in detail the state transition of the process:

NULL -> Creation state: the first state when a new process is created;

Creation state -> Ready state: After the process is created and initialized, when everything is ready to run, it becomes ready state. This process is very fast;

Ready state -> Running state: After the process in the ready state is selected by the process scheduler of the operating system, it is assigned to the CPU to formally run the process;

Running status -> End status: When the process has finished running or an error occurs, it will be processed by the operating system as the end status;

Running state -> Ready state: During the running process of a process in the running state, because the running time slice allocated to it is used up, the operating system will change the process to the ready state, and then select another process from the ready state to run;

Running state -> blocking state: when the process requests an event and must wait, such as requesting an I/O event;

Blocking state -> Ready state: When the event that the process is waiting for is completed, it changes from the blocked state to the ready state;

In addition, there is another state called the suspended state, which means that the process does not occupy physical memory space. This is different from the blocking state, which is waiting for the return of an event.

Due to virtual memory management, the space used by the process may not be mapped to physical memory, but on the hard disk. At this time, the process will appear in a suspended state, and the call to sleep will also be suspended.
The suspended state can be divided into two types:

Blocking suspended state: the process is in external storage (hard disk) and waiting for an event to appear;

Ready suspended state: the process is in external memory (hard disk), but as long as it enters the memory, it runs immediately;

These two suspended states plus the previous five states become seven state transitions (the color left to me is not much), as shown in the figure below: Insert picture description here
The control structure of the process is controlled
by the process in the operating system Block (process control block, PCB) data structure to describe the process.

What is PCB? Open Zhihu and search and you will find that this thing is not that simple. Insert picture description here
PCB is the only identification of the existence of a process, which means that a PCB must exist for the existence of a process. If the process disappears, then the PCB will also disappear.
What information does the PCB contain?
Process description information:

Process identifier: identify each process, each process has a unique identifier;

User identifier: the user to which the process belongs. The user identifier is mainly for sharing and protection services;

Process control and management information:

The current state of the process, such as new, ready, running, waiting or blocked, etc.;

Process priority: the priority when the process preempts the CPU;

Resource allocation list:

Information about the memory address space or virtual address space, the list of opened files, and the I/O device information used.

CPU related information:

The value of each register in the CPU, when the process is switched, the status information of the CPU will be saved in the corresponding PCB, so that when the process is re-executed, the execution can continue from the breakpoint.

It can be seen that PCB contains a lot of information.

  • Process control After
    we are familiar with the state transition of the process and the data structure PCB of the process, let's take a look at the process of process creation, termination, blocking, and wake-up. These processes are also process control.

01 Create process

The operating system allows one process to create another process, and allows the child process to inherit the resources owned by the parent process. When the child process is terminated, the resources inherited from the parent process should be returned to the parent process. At the same time, terminating the parent process will also terminate all of its child processes.

The process of creating a process is as follows:

Assign a unique process identification number to the new process and apply for a blank PCB. The PCB is limited. If the application fails, the creation will fail;

Allocate resources for the process. If the resources are insufficient here, the process will enter a waiting state to wait for resources;

Initialize the PCB;

If the scheduling queue of the process can accommodate the new process, insert the process into the ready queue and wait for it to be scheduled to run;

02 Terminate the process

The process can be terminated in three ways: normal termination, abnormal termination, and external intervention (signal kill off).

The process of terminating the process is as follows:

Find the PCB of the process that needs to be terminated;

If it is in the execution state, immediately terminate the execution of the process, and then allocate CPU resources to other processes;

If it has child processes, all of its child processes should be terminated;

Return all resources owned by the process to the parent process or operating system;

Delete it from the queue where the PCB is located;

03 Blocking process

When a process needs to wait for an event to complete, it can call a blocking statement to block itself and wait. Once blocked waiting, it can only be awakened by another process.

The process of blocking the process is as follows:

Find the PCB corresponding to the identification number of the process to be blocked;

If the process is in the running state, protect its site, turn its state into a blocking state, and stop running;

Insert the PCB into the blocking queue;

04 Wake up the process

The process changes from "running" to "blocking" state because the process must wait for the completion of a certain event, so it is absolutely impossible for a process in a blocked state to wake itself up.

If a process is waiting for an I/O event, and another process needs to send a message to it, only when the event expected by the process occurs, the discoverer process wakes it up with a wake-up statement.

The process of waking up the process is as follows:

Find the PCB of the corresponding process in the blocking queue of the event;

Remove it from the blocking queue and set its state to the ready state;

Insert the PCB into the ready queue and wait for the scheduler to schedule;

Process blocking and wake-up are a pair of statements with opposite functions. If a process calls a blocking statement, there must be a corresponding wake-up statement.

  • Process context switching
    . CPU resources are shared between processes. At different times, processes need to be switched so that different processes can be executed on the CPU. Then this process switches to another process to run, which is called process context switching.

Guess you like

Origin blog.csdn.net/jiahuiandxuehui/article/details/114572436