I. Basic concepts
Hello, everyone. In this article we would like to introduce is about Linux [process priority], the first thing we need to understand is its basic concepts
- existLinux Basics – Permissions In the article, we have talked about what permissions are, and what is the difference between having permissions and not having permissions in Linux. Now we’re going to compare the concepts of privilege and priority.
- scope of one’s jurisdiction:say whether or not you can do something
- prioritization:Indicates your access to the resource, who accesses it first and who accesses it later
- So as far as prioritization is concerned, we are already empowered to do something, we just need to think about an order of precedence
II. Why there are process priorities
Next, let’s talk about why there is such a thing as a process priority.
The first question I want to ask is: do readers sometimes feel that their computers are more laggy?
So the operating system must ensure healthy competition and confirm priorities before it can organize all the processes to run one after another
Not only that then, but it’s also possible to run the process on a specified CPU so that the
Scheduling unimportant processes to a particular CPU can greatly improve overall system performance
So, the process with high priority has the right to be executed first, and with the priority; it can make some important processes to be executed first.
III. How to view and modify the process priority
Knowing the basic concept of process priority, let’s take a look at what it really is in Linux.
- We can check the priority of a running process with the following command
ps -al
- Here we are focusing on the following important information:
- UID :: Representing the identity of the implementer
- PID : represents the code name of the process
- PPID : represents the process from which this process is derived, i.e., the code name of the parent process.
- PRI : represents the priority at which the process can be executed, the lower the value the earlier it will be executed
- NI : represents the nice value of the process
1、PRI and NI
So let’s focus on these two information values
- with regards to
PRI
It’s still relatively well understood, i.e.Priority of processesThe smaller the value, the higher the priority of the process.
- the
NI
And? This is what we call the nice value, which is a modified value that indicates the priority at which a process can be executed.
Then some readers are wondering about this
revised value The concept of
- Press next.
R/r
After that, the following sentence pops up, where therenice
This means that we need to modify the process again, and the shell is waiting for us to enter the PID of the process.Enter the above635
PID to renice [default pid = 1]
- Then, we see that the shell is again waiting for us to enter the changes we need to make to the
nice
value
Renice PID 635 to value
- Then we can try to change this
nice
value, which is initially seen as80
Now we subtract 30 from it, but at the end of the execution we find that aPermission denied
If you are familiar with Linux permissions, you will know that this is a [privilege denial】
- Above it is because the permission was rejected, we are to the identity of the ordinary user to perform, then now it we switch to the [root] after you can be a hundred no holds barred ,           ã
So what’s next? Let’s see.root How powerful is it?
- Here is Linux’s formula for [PRI].
PRI(new) = PRI(old) + nice
- What can be observed here is this
NI
has really changed, but then we can see that although the modified value of the-30
But when I finally went to check it out, it was-20
So.PRI
The value of[60]
![[Linux] Process Prioritization [Linux] Process Prioritization](https://imgs.developpile.com/imgs/93de4e23528043e6b9bbceb808d08fab.gif)
Then some students will be very confused about the above, why is that?
Here words to add a point, in fact, for ordinary users can also go to set the process priority, let’s take a look at the
- As you can see here I’m going as a normal user, just now we typed in
-30
meet withPermission denied
But what about now, but when we type in 100 and then go out and look at the process at this point, thePRI
value but saw that it had indeed changed as well
![[Linux] Process Prioritization [Linux] Process Prioritization](https://imgs.developpile.com/imgs/fec2bd3bc67b4e27993f3fa609d0dfd0.gif)
The words here are popularized for the readers
The above is how to view and modify the current process priority, but the priority of a process we had better not easily modify, otherwise it will bring some unnecessary trouble!
2. Basic concepts of bitmap
I’ll talk more about this concept in my C++ column, so here’s what I’m talking about.
The first question I would like to ask is: how does the operating system carry out the scheduling, based on priority?
- The first thing we need to know about bitmaps is that they are actually a whole string of
01
sequence, like the following
0000000000000000001111111111111111111
- Let’s assume that the structure of the bitmap is the following structure, so now if we want to find the Nth bit in the bitmap, we need to do a bit operation
struct bitmap
{
char bits[100];
}
- on account of
bits
There are 100 elements in the array, achar
type has 8 bits, then we can calculate the current subscript and the value of pos, and then perform a bitwise operation to find out exactly where the N is located.
IV. Understanding process priorities through scheduling algorithms
We are explainingBasic concepts of processes When we talk about each CPU maintaining a runtime queue i.e. therun_queue
- We see one here.
run_queue
There are two pointer arrays that hold pointers to thetask_struct
The pointer to the structure of the This pointer array of[0, 99]
The number subscripts are generally not used, and are given to other processes, so we use the[100, 139]
this interval (math.)
- How much data is in this range? That’s right, it’s 40! If you’ve read this article carefully, you’ll be very sensitive to this number, even though we’ve talked about process priorities earlier in this article.
NI
The range at that point was[60, 99]
It also contains 40 numbers.
Then the reader should be able to feel that the processes in this operating system are constantly needing to be executed, so now I would like to ask: If the CPU is now scheduling this run queue, but at this time there is a new process over, at this time how to deal with this process?
- We see that in the
run_quque
There are actually two secondary pointers to [run] and [wait], which in turn point to therunning
andwaiting
At this time, see the right side of this screen, they are actually two mirror queues, the top queue is being run by the CPU scheduling, while the following queue is a waiting queue, if there is a new process needs to be scheduled to execute the first link to this waiting queue!
![[Linux] Process Prioritization [Linux] Process Prioritization](https://imgs.developpile.com/imgs/b96f4a45a501492492ae5415fd25df45.jpeg)
Then a student asks: so one queue just keeps running and the other one keeps waiting?
- Of course not, if the processes in the upper run queue have finished executing, then the following code will be executed, and the processes in the two queues will be swapped.
swap(&run, &wait);
So how do we know if this current run queue is empty?
- Here we need to use the [bitmap] we talked about above, we in this
run_queue
A new member is added to the structure calledis_empty
of the bitmap structure variables.
- So let’s now define a
char
The size of the array is 5, so when we traverse the process queue through this array, we assume that the first process priority is60 is empty, and the second bit is whether the process has a priority of61 of whether this group is empty. We use0
stands for null, with1
on behalf of non-empty, then if we want to go to determine whether the process queue is empty, then we only need to look at the 01 sequence is 0 can be, if you want to find which process priority in the queue is not empty, then you only need to go to find the sequence for the [1] can be
V. Summarizing and refining
Finally, to summarize what we have learned in this article
- First of all, we learned the basic concept of process priority, which is different from the [permissions] is that the priority we are for the access to resources.Who visits first, who visits later
- The reason for the existence of process priority is that in a computer, resources are limited and processes are multiple, so there must be competition. Then if we have [process priority] then we canScheduling unimportant processes to a particular CPU can greatly improve overall system performance
- If a process has a priority, we can try to view it in Linux and modify it, mainly by using the
NI
This value to carry out a modification, but we have to pay attention to the modification of the time, Linux in order to prevent the user to do too much modification of thisNI
The value of the[60, 99]
If we make too many upward or downward changes, we won’t exceed the limit.
- Finally, we will use the underlying operating system [scheduling algorithm] to further understand the process priority, and then step by step to improve the
run_queue
This running queue at the same time, we clearly know the operating system in the face of so many processes, is how to do a good job of rational planning it is through the maintenance of twomirror waiting queueThe processes inside are executed alternately.