当前位置:网站首页>CPU process priority

CPU process priority

2022-06-24 13:23:00 Chen Bucheng I

One . brief introduction

The way process priorities work has not changed much since their invention , Whether it's just one cpu Era , Or multicore cpu Time , Are occupied by the control process cpu The length of time . That is, in the same scheduling cycle , Higher priority processes take longer , Low priority processes take up less time .

The most familiar priority setting method on the system is nice and renice command .

Two .NICE value

nice The value should be familiar Linux/UNIX People know the concept of , We all know that it is a reaction process “ priority ” State value , Its value range is 40-20 to 19-0, altogether 40 A level . The smaller the value , The said process ” priority ” The higher the , And the greater the value “ priority ” The lower the .

We can go through nice Command to execute a command to be executed nice Value settings , The method is :

in addition , Use renice Command can control a running process nice Value adjustment , We can also use, for example top、ps Wait for the command to view the process nice value , I won't say more about the specific method , You can refer to the relevant manpage.

What we need to pay attention to is , I'm using it here nice Worth the title , Not priority (priority) The idea that . Of course ,nice and renice Of man In the manual , It also says priority The concept , But I want to stress that , Please don't confuse these two concepts in the system , One is nice value , One is priority value , They are inextricably linked , But for the current Linux system , They are not the same concept .

Have you figured out PR Values and NI The difference in value ? without , Then we can first figure out what is nice value .

nice Although the value is not priority, But it does affect the priority of the process .

In English , If we describe a person nice, That generally means that this person is more popular . What kind of people have good luck ? Often humility 、 A polite person . such as , You and a nice Go to lunch with people , Ordered two identical meals , I served one first and then ,nice The one who usually says :“ You eat first, you eat first !”, This is good popularity , This man nice! But if the other one comes late , So this nice People are going to be hungry . What does this ? The more nice The worse the ability of people to seize resources , And the less nice The stronger the preemptive ability of people . This is it. nice The meaning of value size ,nice The lower the value , It means that the less the process is nice, preemption cpu The stronger the ability to , The higher the priority . In the original use O1 The scheduling of Linux On , We'll also put nice The value is called static priority , This is basically in line with nice Value characteristics , Namely nice When the value is set , Unless we use renice Change it , Otherwise it will remain unchanged . and priority The value of is in the previous kernel O1 The performance on the scheduler will change , So it's also called dynamic priority .

3、 ... and . priority

Simple understanding nice After the concept of value , Let's see what is priority value , Namely ps As seen in the order PRI Value or top As seen in the order PR value .nice Namely top Medium NI value

In kernel , The value range of process priority is 140 Priority ranges , The value range is from 0-139, The smaller the value , The higher the priority .nice It's worth it 40-20 To 19-0, The priority range mapped to the actual is 100-139.

Under normal circumstances , The priority of any process is this value , Even if we pass nice and renice The command adjusts the priority of the process , Its value range will not exceed 100-139 The scope of the , Unless the process is a real-time process , Then its priority value will become 0-99 One of this range . There's a message here , That is, the current Linux Is an operating system that already supports real-time processes .

Simply speaking , The real-time operating system needs to ensure that the relevant real-time processes respond in a short time , There won't be a long delay , It also requires minimum interrupt delay and process switching delay . For such needs , General process scheduling algorithm , Whether it's O1 still CFS Are insatiable , So when the kernel is designed , The real-time process is mapped separately 100 Priority , These priorities are higher than those of normal processes (nice value ), This ensures that the real-time process has the highest distress record , Always ahead of the line to be dealt with .

The scheduling algorithms of real-time processes are also different , They use simpler scheduling algorithms to reduce scheduling overhead . in general ,Linux The processes running in the system can be divided into two categories :

Four . Real time process

Non real time processes The main difference between them is that they are distinguished by priority . All priority values are in 0-99 Within the scope of , It's all real-time processes , and 100-139 In scope are non real time processes . Can be used in the system chrt Order to see 、 Set the real-time priority state of a process . Let's take a look first chrt Use of commands :

Let's first focus on the Policy options part , You will find that the system provides for a variety of processes 5 Two scheduling strategies . But what is not stated here is , These five scheduling strategies are used for two processes respectively , The scheduling strategy that can be used for real-time processes is :SCHED_FIFO、SCHED_RR, For non real-time processes :SCHED_OTHER、SCHED_OTHER、SCHED_IDLE.

The overall priority strategy of the system is : If there are real-time processes to be executed in the system , Priority is given to the execution of real-time processes . Until the real-time process exits or voluntarily gives up CPU when , Will schedule the execution of non real-time processes . The priority range that real-time processes can specify is 1-99, The method of executing a program to be executed in real time is :

Just said ,SCHED_RR and SCHED_FIFO They are all real-time scheduling strategies , Can only be set for real-time processes . For all real-time processes , High priority ( Namely priority Small number ) The process must ensure that it executes before the process with low priority .SCHED_RR and SCHED_FIFO The scheduling strategy will only work when the priorities of two real-time processes are the same , The difference is as the name suggests :

SCHED_FIFO: Scheduling in a first in first out queue , With the same priority , Whoever executes first will be dispatched first , Unless it quits or actively releases CPU.

SCHED_RR: Multiple processes with the same priority are processed in the way of time slice rotation . The length of the time slice is 100ms.

原网站

版权声明
本文为[Chen Bucheng I]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210524135807684B.html