This is the second in a series of operating systems.
If you want to know what the operating system does every day, open your resource monitor:
Screenshot of Resource Monitor, Windows 10
Through this chart alone, we can summarize several important functions of the operating system:
- Process management
- Thread management
- Memory management
- I / O management (including disk scheduling)
- File management , this feature is not shown in the picture, but I believe that everyone who uses the computer knows it.
Why should I start with the process?
The reason is simple. We use computers every day, including mobile phones and computers, essentially using applications that run on their operating systems. For us, the most intuitive function of the operating system is process management, so let's start with process management and dig deeper into the essence of the operating system step by step.
What is the process?
I briefly mentioned the concept of process in the first article, and I will go into details here to deepen my understanding.
The operating system is basically designed to meet the needs of users. For individual users, the requirement is to run multiple applications on one computer to meet the needs of life and work. But with so many applications, it is impossible for each program to occupy a CPU core, because CPU cores are limited and human needs are unlimited.
So the operating system needs to allocate unlimited (exaggerated) applications to the limited CPU.
When we open a bunch of programs, these programs will be loaded into memory. In order to distinguish these running programs from those that are not open, we coined the term Process. Therefore, a process is an abstraction of a running program and is dynamic. Process management is actually a way for the operating system to manage the programs we have opened.
Note: To simplify the following discussion, we assume that the computer in question is single-core.
What are the states of a process?
When it comes to processes, we must understand the state of the process. To understand the state of the process, we must look at the process from the perspective of the process and see what happens in the lifetime of the process.
First, when a user opens an application, the program is in a new state (New). At this time, the operating system is not ready for the program. The process itself has not yet entered the memory, and may still remain in the disk.
When this process is loaded into memory, it means that it is ready to run, but because CPU resources are being occupied by other processes, it can only wait for the operating system to allocate CPU for it. This state is called Ready .
After a period of time in the ready state, CPU resources are always allocated, and once the process starts executing, it enters the running state (Running).
Some processes may perform certain blocking operations. Take the I / O operation as an example. After performing the operation, the process needs to wait for the I / O operation to complete. The first article said that the process is unavailable during this time. CPU, if you let it continue to occupy the CPU, it will cause a waste of resources. So the operating system will deprive it of its CPU usage and put it in a blocking state . Wait until the I / O operation is complete before putting it into the ready state.
There is one last state, the exit state . As the name implies, after the process terminates, it will enter the exit state. This process may not be cleared from memory. When the process completely exits memory, the process's life is completely over.
Based on the above discussion, we get five states of the process:
Attentive readers may find that the arrows between the running state and the blocking state, and the blocking state to the ready state are one-way. why?
Let's take a look at the running state and the blocking state first. Recall, what does a process in a blocking state represent? (Hope you see this does stop and think)
A process is in a blocking state, which means that the process has performed a blocking operation and is waiting for the result of the operation. In other words, the process in the blocking state does not have the ability to use the CPU, so even if it is given the CPU, it cannot run and naturally cannot enter the execution state. Therefore, it is impossible to jump directly from the blocking state to the running state.
Let's talk about the ready state and the blocking state. If a process is in the ready state, it means that it is not using the CPU, so it is more unlikely to perform blocking operations. Therefore, you cannot directly jump from the ready state to the blocking state.
Combined with the explanation just now, let's take a look at a simple process queuing model:
ABCDEF in the figure represents the process
It should be noted that the ready state, the running state, and the blocking state provide a system method to describe the behavior of the process and guide the implementation of the operating system. Many actual operating systems are specifically constructed according to these three states. However, this does not mean that there is no place for other states. In some implementations (actually, it is the mainstream implementation, but because the concept of virtual memory is involved, I will leave it to explain later), and the suspended state. But in either state, they are abstracted for the operating system to maximize the use of computer resources.
Leave a few small questions to think about:
- Why can I only switch from the running state to the exit state?
- Can you understand the queuing model above?
What is a PCB?
When the operating system manages and controls the process, it must first know the location of the process (that is, which piece of memory the process has been loaded into), and secondly, it needs to know the attributes of the process, such as the process ID and process status, so we have to There is a structure to hold this information.
The Process Control Block is one such structure. Process control blocks are created when the program starts.
The main content and memory image of the process control block
We can see that the PCB stores important information such as process ID, register status, and stack pointer. This information seems very strange now, but as you deepen your understanding of the operating system, you will understand the meaning of this information and effect.
There is also information in the picture that the PCB is stored in the kernel space -indicating that only the operating system has the right to change the contents of the PCB. Because the PCB is too important, if its internal information is maliciously modified, it will cause the process to terminate unexpectedly and may even cause the operating system to crash.
to sum up
Let's take a look at today's content:
If a program starts running, the operating system creates a process control block for it and loads it into memory. The " process status " information in the process control block changes to the ready state and puts the process in the ready queue. Waiting for CPU allocation. Once the CPU is allocated, the process enters the running state . According to the actual situation, it may also enter the blocking state due to the execution of the blocking operation. When the program finishes running, the process is cleared out of memory by the operating system and then deletes its process control block.
If after reading the previous paragraph, the concept of bold type is still very vague, then I suggest that you read the article slowly again, and you must bring your own thinking, the efficiency of reading without thinking is very low.
I hope you have learned something after reading the article. Thank you for reading, we will have a period later!
Disclaimer: Original article, unauthorized reproduction is prohibited