The order in which processes are run

Source: Internet
Author: User
Tags numeric value
about the order in which programs are run

We know that Linux is a multiplayer environment, the output from top we also found that the system at the same time there are a lot of programs in operation, but most of the program is in hibernation (sleeping) state. Think about it, if all the programs are awakened at the same time, then the CPU should have to deal with that program first. That is, the program is running in a higher order of precedence. This has to take into account the program's priority operating sequence (Priority) and CPU row Chengro.

CPU scheduling is not the same as the routine work schedule in the previous chapter. CPU scheduling refers to the calculation code that each program is run by the CPU, while the routine scheduling is to schedule a program to run at a certain time. CPU scheduling is more relevant than the operating system.

Priority and Nice values

We know that the CPU can run as many as a few G of the number of micro-command times, through the core of the CPU scheduling can be the CPU switch to run the program, so each program in a second or more or less will be CPU run part of the command code. If the program is concentrated in a queue waiting for the CPU to run without prioritization, which is just like when we go to the playground to play hot games we need to queue up, everyone is in order. After you play it again and want to play again (not finished running), please continue to wait in line back. It's kind of like the bottom:




The above diagram assumes that Pro1, Pro2 is an emergency procedure, PRO3, Pro4 is a general procedure in such an environment, due to not having precedence, alas. Pro1, Pro2 still have to wait without preferential treatment. If Pro3, Pro4 's work is smelly and long. So urgent Pro1, Pro2 will have to wait for a long while to be able to complete. That's a problem. So, we want to prioritize the program. If the precedence sequence is higher then the number of runs can be more than a few times, without needing to grab the location with the slower priority program. We can explain the priority sequence of the program with the CPU scheduling in the following illustration:



As shown in the above picture, the Pro1 with high priority, PRO2 can be taken two times, but less important PRO3, Pro4 run fewer times. In this way Pro1, Pro2 can be completed more quickly. Note that the above diagram is just a schematic, not a priority will be run two times. To achieve this, Linux gives the program a so-called "priority run order (priority, PRI)", and the lower PRI value means the higher the priority. However, this PRI value is dynamically adjusted by the core, and the user cannot adjust the PRI value directly. Let's see where the PRI appeared.

[Root@www ~]# Ps-l

F S UID PID PPID CPRI NI ADDR SZ Wchan TTY time CMD

4 S 0 18625 18623 2 0-1514 wait pts/1 00:00:00 bash

4 R 0 18653 18625 0 0-1102-PTS/1 00:00:00 PS

Since the PRI is a core dynamic adjustment, our users have no right to interfere with the PRI. If you want to adjust the priority sequence of the program, you have to pass the nice value. The nice value is NI on the table. In general, the correlation between PRI and NI is as follows:

PRI (new) = PRI (old) + Nice

But you should pay special attention to that if the original PRI is 50, not that we give a nice = 5, it will turn the PRI into 55 oh. Since the PRI is a "dynamic" decision of the system, while the nice value can affect the PRI, the final PRI is still subject to systematic analysis before deciding. In addition, the nice value is positive or negative, and since the PRI is smaller the sooner it is run, so when the nice value is negative, then the program lowers the PRI value, which means it will be processed more preferentially. In addition, you must pay attention to:

L The adjustable range for nice value is-20 ~ 19;

L root can adjust the nice value of oneself or others ' program at will, and the range is 20 ~ 19;

L General users can only adjust their own program's nice value, and the range is only 0 ~ 19 (avoid the general user preemption system resources);

L A general user can only tune the Nice value higher, for example, if Nice is 5, the future can only be adjusted to greater than 5;

That is to say, to adjust the priority run of a program is to "tweak the Nice value of the program". So how to give a program nice value. There are two ways, respectively:

L Give a special nice value immediately when I start to run the program: with nice command;

L Adjust the nice value of an already existing PID: Use the Renice command.

Nice: The new nice value is given to the newly run command

[Root@www ~]# Nice [-n number] command

Options and Parameters:

-N: After a numeric value, the range of the value-20 ~ 19.

Example one: Use root to give a nice value of-5, run VI, and observe the program.

[Root@www ~]# Nice-n-5 VI &

[1] 18676

[Root@www ~]# Ps-l

F S UID PID PPID CPRI NI ADDR SZ Wchan TTY time CMD

4 S 0 18625 18623 0 0-1514 wait pts/1 00:00:00 bash

4 T 0 18676 18625 0 72-5-1242 finish pts/1 00:00:00 VI

4 R 0 18678 18625 0 0-1101-PTS/1 00:00:00 PS

# The original bash PRI is 75, so the VI default should be 75. But given that Nice is-5,

# so the PRI of VI has been lowered. But not down to 70, because the core will also be dynamically adjusted.

[Root@www ~]# kill-9%1 <== test completed to close VI

As mentioned earlier, Nice is used to adjust the program's running precedence. This is just an example of a run. When do you usually want to make the nice value larger? For example, in the background work of a system, some of the less important programs are carried out: for example, backup work. Due to the considerable cost of backup work, this time the nice value of the backup commands can be increased to make the system more equitable in allocating resources.

Renice: Nice realignment of existing programs

[Root@www ~]# renice [number] PID

Options and Parameters:

PID: The ID of a program.

Example one: Find your own bash PID and adjust the PID's nice to 10

[Root@www ~]# Ps-l

F S UID PID PPID CPRI NI ADDR SZ Wchan TTY time CMD

4 S 0 18625 18623 0 0-1514 wait pts/1 00:00:00 bash

4 R 0 18712 18625 0 0-1102-PTS/1 00:00:00 PS

[Root@www ~]# Renice 10 18625

18625:old priority 0, new priority 10

[Root@www ~]# Ps-l

F S UID PID PPID CPRI NI ADDR SZ Wchan TTY time CMD

4 S 0 18625 18623 0 10-1514 wait pts/1 00:00:00 bash

4 R 0 18715 18625 0 10-1102-PTS/1 00:00:00 PS

If you want to adjust a program that already exists, then you have to use Renice. The method used is very simple, renice after the numerical and PID can be. Because the following is the PID, so you must be the PS or other procedures to observe the command to find the PID is OK.

As we can see from the example above, although the program that is being modified is bash, the nice in the PS command triggered by the program will inherit to 10 oh. Get it. The entire nice value can be passed between the parent program's--> subroutine. In addition, in addition to Renice, in fact, the top is also able to adjust the nice value.


Article source

Http://www.cnblogs.com/ggjucheng/archive/2012/10/21/2733147.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.