Processes and process-related properties

1. What is a process?

A running program is called a process.

1.1 Process PCB

Process information is placed in a data structure called a process control block, which can be understood as a collection of process attributes.
The process PCB under Linux is a task_struct structure.
task_struct is a data structure of the Linux kernel that is loaded into RAM (memory) and contains process information.

1.2 task_ struct

task_ struct content classification:
identifier: the unique identifier describing this process, used to distinguish other processes.
Status: Task status, exit code, exit signal, etc.
Priority: Priority relative to other processes.
Program Counter: The address of the next instruction in the program to be executed.
Memory pointers: Including pointers to program code and process-related data, as well as pointers to memory blocks shared with other processes.
Context data: The data in the registers of the processor when the process is executed.
I/O status information: including displayed I/O requests, I/O devices assigned to the process and a list of files used by the process.
Billing information: May include sum of processor time, sum of clocks used, time limits, billing account number, etc.
other information.

1.3 View process

Process information can be viewed through the /proc system folder.
For example: To obtain the process information with PID 123, you need to view the /proc/123 folder.
Most process information can also be obtained using user-level tools such as top and ps.

2. Create a process through a system call - fork

insert image description here

3. Process status

static const char * const task_state_array[] = {
    
    
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
"X (dead)", /* 16 */
"Z (zombie)", /* 32 */
};

R running status (running): It does not mean that the process must be running, it indicates that the process is either running or in the run queue.

S sleeping state (sleeping): means that the process is waiting for the event to complete (the sleep here is sometimes called interruptible sleep (interruptible sleep)).

D Disk sleep state (Disk sleep) is sometimes called uninterruptible sleep state (uninterruptible sleep). Processes in this state usually wait for the end of IO.

T stop state (stopped): A process can be stopped (T) by sending a SIGSTOP signal to the process. The suspended process can be resumed by sending the SIGCONT signal.

X dead state (dead): This state is just a return state, you will not see this state in the task list.

3.1 Z(zombie) – zombie process

Zombies are a special state. When the process exits and the parent process does not read the return code of the child process exit, a zombie (corpse) process will be generated. The zombie process will remain in the process table in a terminated state, and will always wait for the parent process to read the exit status code . Therefore, as long as the child process exits, the parent process is still running, but the parent process does not read the state of the child process, and the child process enters the Z state. The parent process is required to use the wait() system call interface to handle resources of the zombie process.
insert image description here
insert image description here

3.2 Harm of Zombie Process

The exit status of the process must be maintained, because he wants to tell the process (parent process) that cares about it, the task you entrusted to me, and how I am doing. But if the parent process never reads, the child process will always be in the Z state? Yes!

Maintaining the exit status itself requires data maintenance, which also belongs to the basic information of the process, so it is stored in task_struct (PCB). In other words, the Z state has not been exited, and the PCB has to be maintained all the time? Yes!

That parent process creates a lot of child processes, but if they don't recycle, will it cause a waste of memory resources? Yes! Because the data structure object itself will occupy memory, think about defining a structure variable (object) in C language, it is necessary to open up space in a certain location of memory!
Memory leak? Yes.

4. Orphan process

If the parent process exits early, then the child process exits later and enters Z, what should I do?
The parent process exits first, and the child process is called an "orphan process". The orphan process is adopted by the No. 1 init process, and of course the init process must be recycled.
insert image description here
insert image description here

5. Process priority

5.1 Basic concepts

The order of cpu resource allocation refers to the priority of the process.
Processes with higher priority have the right to execute first. Configuring process priority is useful for Linux in a multitasking environment and may improve system performance.
You can also run the process on a specified CPU. In this way, assigning unimportant processes to a certain CPU can greatly improve the overall performance of the system.

5.2 PRI and NI

PRI is also relatively easy to understand, that is, the priority of the process, or in layman's terms, the order in which programs are executed by the CPU. The smaller the value, the higher the priority of the process.
What about NI? It is the nice value we are talking about, which represents the modified value of the priority of the process that can be executed.
The smaller the PRI value, the faster it will be executed. After adding the nice value, the PRI will become: PRI(new)=PRI(old)+nice.
In this way, when the nice value is negative, the program will take priority The level value will become smaller, that is, its priority will become higher, and it will be executed sooner. Therefore, to adjust the process priority, under Linux, it is to adjust the nice value of the process. The value range of nice is -20 to 19, a total of 40 levels.

It should be noted that the nice value of the process is not the priority of the process, they are not a concept, but the nice value of the process will affect the priority change of the process. It can be understood that the nice value is the correction data of the process priority.

6. View the command of process priority

Use the top command to change the niceness of an existing process.
top
After entering top, press "r" –> enter the process PID –> enter the nice value.

Seven, some other concepts about the process

Competitiveness: There are a large number of system processes, but only a small amount of CPU resources, or even one, so the processes are competitive. In order to efficiently complete tasks and compete for related resources more reasonably, they have priorities.
Independence: Multi-process operation requires exclusive use of various resources, and does not interfere with each other during multi-process operation.
Parallelism: Multiple processes run simultaneously under multiple CPUs separately, which is called parallelism.
Concurrency: Multiple processes use process switching under one CPU to allow multiple processes to advance within a period of time, which is called concurrency.

Eight, the overall understanding framework of the process

insert image description here
The above is all the content I want to share with you today, have you learned it? If you feel that you have gained something, please light up your heart and pay attention to it by the way. We will continue to update the relevant content of Linux in the future. See you in the next issue! ! ! ! !

Guess you like

Origin blog.csdn.net/weixin_70056514/article/details/131713540