Processes in Linux have priorities. Linux is a multi-user, multi-tasking operating system. There are usually many processes running in the system, but the CPU can only operate one instruction in one clock cycle. Which process should operate first and who should operate last? This needs to be determined by the priority of the process. In the Linux system, there are two parameters that indicate the priority of the process: Pri and Nice; the process priority is the PRI value, not the Nice value, but the Nice value will affect the priority.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Linux-Process Priority
Linux is a multi-user, multi-task operating system. There are usually many processes running in the system. process. However, the CPU can only operate one instruction in one clock cycle (the current CPU uses multi-threading and multi-core technology, so it can operate multiple instructions in one clock cycle. However, the number of instructions that can be operated at the same time is far smaller than that in the system. The total number of processes), then the question comes: Who should calculate first and who should calculate last? This needs to be determined by the priority of the process.
The order in which the CPU allocates resources is the priority
The process with the highest priority has the right to execute first,
You can let the process run on the specified CPU to improve the overall performance of the system
In addition, when the CPU is computing data, it does not complete an integrated calculation and then proceed to the next step. The operation of a process first operates on process 1, then on process 2, then on process 3, and then on process 1 until the process task ends. Not only that, due to the existence of process priority, processes are not calculated sequentially, but which process has a higher priority will be calculated more times in one calculation cycle.
It’s hard to understand this way, let’s put it another way. Suppose I now have 4 children (processes) that need to be fed (computed). I prefer child 1 (process 1 has a higher priority), and child 2, child 3, and child 4 are treated equally (process 2, process 3, and process 4 have priority). Now I start feeding. I can't feed child 1 first, and then feed the other children. Instead, I need to feed the food in a loop (all processes loop during CPU operation). Then, when I am feeding (calculation), I will feed the child a mouthful of rice first, and then feed the other children. And in a cycle, feed child 1 two mouthfuls of food first, because I prefer child 1 (high priority), and feed the other children one mouthful of food. This way, child 1 will be fed first (process 1 computes faster) because I like child 1 more.
View process priority
1. Use ps-al to see the priority of the process
2.PRI and NI
In the Linux system, there are two parameters indicating the priority of the process: Priority and Nice.
[root@localhost ~]# ps -le F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 1 0 0 80 0 - 718 - ? 00:00:01 init 1 S 0 2 0 0 80 0 - 0 - ? 00:00:00 kthreadd ...省略部分输出...
Among them, PRI stands for Priority and NI stands for Nice. Both values indicate priority. The smaller the value, the higher the priority the process will receive from the CPU. However, the PRI value is dynamically adjusted by the kernel and cannot be modified directly by the user. So we can only affect the PRI value by modifying the NI value and indirectly adjust the process priority.
The relationship between PRI and NI is as follows:
PRI (final value) = PRI (original value) NI
In fact, everyone only needs to remember, We can change the priority of the process by modifying the value of NI. The smaller the NI value, the PRI value of the process will decrease, and the process will be processed by the CPU first; conversely, the larger the NI value, the PRI value of the process will increase, and the process will be processed later by the CPU.
There are several things to note when modifying the NI value:
The NI range is -20~19.
Ordinary users can adjust the NI value in the range of 0~19, and they can only adjust their own processes.
Ordinary users can only increase the NI value, but not lower it. If the original NI value is 0, it can only be adjusted to be greater than 0.
Only the root user can set the process NI value to a negative value, and can adjust any user's process.
View the command to view and modify the nice value
1. Use the top command to view and modify the nice value
Enter top-press r, enter the process pid, and enter the nice value. (If it shows that elevation is refused, use sudo to elevate the permissions of the top command).
2. Other concepts
Competitiveness: There are many systems, but only a small number of CPUs, or even only one, so there is competition between processes. In order to complete tasks efficiently and compete for related resources more reasonably, priority is given.
Independence: When multiple processes run, they need to enjoy various resources exclusively, so they do not interfere with each other during the multi-process process.
Parallel: Multiple processes running on multiple CPUs at the same time are called parallel.
Concurrency: Multiple processes use process switching on a CPU to advance multiple processes within a period of time, which is called concurrency.
The process priority is the PRI value, not the Nice value, but the Nice value will affect the priority
PRI is still It is easier 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 this value, the higher the priority of the process. What about NI? This is the nice value we are talking about, which represents the modified value of the priority at which the process can be executed. As mentioned before, 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.
So far, what needs to be emphasized is that the nice value of a 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.
Change the process priority by adjusting the nice value. The nice adjustment range is -20~19.
Influencing factors and principles of process priority and change
Scheduling strategy: RR scheduling and FIFO scheduling processes are real-time processes. Processes scheduled with time-sharing (OTHER) are non-real-time processes.
FIFO (first in, first out) and RR (time slice rotation) are used for real-time processes, and OTHER (time-sharing scheduling) is used for non-real-time processes; real-time processes will preempt ordinary processes; FIFO will cause simultaneous Real-time processes with priority always occupy the CPU, and RR will ensure that real-time processes with the same priority are executed in turn according to time slices.
The priority of real-time processes is higher than that of ordinary processes. Real-time processes use static priority scheduling, and non-real-time processes use dynamic priority scheduling. The dynamic priority of non-real-time processes is adjusted by nice and may be affected by bonus.
Linux processes have two priorities: normal process priority (using SCHED_NORMAL scheduling policy), and real-time process priority (using SCHED_FIFO or SCHED_RR scheduling policy)
Different scheduling strategies Real-time processes are comparable only when they have the same priority. At any time, the priority of a real-time process is higher than that of an ordinary process.
Linux uses static priority scheduling for real-time processes, and for ordinary processes (non-real-time Process), scheduled according to dynamic priority.
The real-time process only has a static priority (between 0~MAX_RT_PRIO-1, the default MAX_RT_PRIO is 100), and the kernel will no longer adjust its static priority based on factors such as sleep;
Each priority level of the real-time process No. 0-99 corresponds to a priority queue (linked list). The corresponding linked list with a higher value is executed first (number 0 is the lowest). The priority can be modified by the chrt and corresponding functions described later;
The static priority of non-real-time processes can be adjusted through the nice value (-20~19): static_prio=MAX_RT_PRIO nice 20.
nice only affects non-real-time processes (static priority is between 100 and 139). The larger the nice value, the larger the static priority value and the lower the priority.
Non-real-time process dynamic priority is calculated based on static priority and bonus: dynamic_prio = max (100, min (static_prio - bonus 5, 139))
bonus reflects the average sleep time of the process (range 0~10), the more sleep time, the more likely it is an interactive process, and the more likely it will not use up the time slice before releasing the CPU every time it is its turn to run.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of Do processes have priorities in linux?. For more information, please follow other related articles on the PHP Chinese website!

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
