[Linux] Process Prioritization

Time:2023-10-1

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 jurisdictionsay whether or not you can do something
    • prioritizationIndicates 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?
  • This is because in a computer, resources are limited and processes are multiple. So it’s doomed to be doomed–The processes are in competition with each other!
So the operating system must ensure healthy competition and confirm priorities before it can organize all the processes to run one after another
  • However, if the process does not get CPU resources for a long time, the code of the process will not be able to advance for a long time, which involves a [starvation problem] in the subject of “Operating Systems”.
Not only that then, but it’s also possible to run the process on a specified CPU so that theScheduling 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
[Linux] Process Prioritization

1、PRI and NI

So let’s focus on these two information values
  • with regards toPRIIt’s still relatively well understood, i.e.Priority of processesThe smaller the value, the higher the priority of the process.
  • theNIAnd? 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 thisrevised value The concept of
  • To put it simply, the nice value of a process is not the priority of the process, they are not the same concept.But the process nice value affects the process priority change.
  • We can try to change thisnice value. First, we execute the[top]command, to enter the Linux similar to the [Task Manager] interface
[Linux] Process Prioritization
  • Press next.R/rAfter that, the following sentence pops up, where thereniceThis 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]
[Linux] Process Prioritization
  • Then, we see that the shell is again waiting for us to enter the changes we need to make to thenicevalue
Renice PID 635 to value
[Linux] Process Prioritization
  • Then we can try to change thisnice value, which is initially seen as80Now we subtract 30 from it, but at the end of the execution we find that aPermission deniedIf you are familiar with Linux permissions, you will know that this is a [privilege denial
[Linux] Process Prioritization
  • 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 ,           ã
[Linux] Process Prioritization
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 thisNIhas really changed, but then we can see that although the modified value of the-30But when I finally went to check it out, it was-20So.PRIThe value of[60]
[Linux] Process Prioritization Then some students will be very confused about the above, why is that?
  • As we said above, although there is this [process priority], but then we canby modifyingNIvalue to adjust the process priorityBut this adjustment isn’t for you to mess around with.Linux doesn’t want to involve users too much in prioritizationThe reason why there exists such a range of values is that-20 ~ 19There are 40 levels in total. Therefore, if we adjust the value of [NI] beyond this range, it will be limited to a certain extent.So this.PRIThe value of the60 ~ 99is a closed interval between
  • We can go back and try entering a larger value, and it will be constrained to the99This place, and will not go up again. The reader can also try one other value, and it’s the same, it will only be limited to inside this interval
[Linux] Process Prioritization
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-30meet withPermission deniedBut what about now, but when we type in 100 and then go out and look at the process at this point, thePRIvalue but saw that it had indeed changed as well
[Linux] Process Prioritization The words here are popularized for the readers
  • On Linux systems its allowed toroot The user sets a negative priority, as well as decreasing the size of the priority value for existing processes; however, for theregular user For example, although we can not set a negative priority, but it is possible to set a [positive priority], you can go to the existing process to increase the size of priority values
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 of01sequence, 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 ofbitsThere are 100 elements in the array, achartype 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.
[Linux] Process Prioritization

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_queueThere are two pointer arrays that hold pointers to thetask_structThe 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.NIThe range at that point was[60, 99]It also contains 40 numbers.
[Linux] Process Prioritization
  • Moving on to see what this array of pointers points to, each pointer points to atask_struct, i.e. a process PCB, thereafter if a process of the same priority comes, then it is placed behind like a queue, if there is a process of this priority is not the same, then it is chained behind the pointer to the other structure
  • For the following structure, we call it a hash table, which will be introduced in the C++ series of articles
[Linux] Process Prioritization
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 therun_ququeThere are actually two secondary pointers to [run] and [wait], which in turn point to therunningandwaitingAt 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 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 thisrun_queueA new member is added to the structure calledis_emptyof the bitmap structure variables.
  • So let’s now define acharThe 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 use0stands for null, with1on 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
[Linux] Process Prioritization

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 theNIThis 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 thisNIThe 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 therun_queueThis 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.

Recommended Today

uniapp and applet set tabBar and show and hide tabBar

(1) Set the tabBar: uni.setTabberItem({}); wx.setTabberItem({}); indexnumberisWhich item of the tabBar, counting from the left, is indexed from 0.textstringnoButton text on tabiconPathstringnoImage PathselectedIconPathstringnoImage path when selectedpagePathstringnoPage absolute pathvisiblebooleannotab Whether to display uni.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) wx.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) […]