Order of operation of the program [go]

Source: Internet
Author: User

about the order in which programs are run

We know that Linux is a multi-worker environment, from the top of the output we also found that the system at the same time there are a lot of programs in operation, but most of the programs are in hibernation (sleeping) state only. Think, if all the programs are woken at the same time, then the CPU should be the first to process the program? In other words, is the program being run in a higher order of precedence? This has to take into account the priority of the program and the CPU platoon Chengro!

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

Priority and nice values

We know that the CPU can run as many as a few G of micro-commands a second, through the core CPU scheduling can be the CPU switching operation, so each program in a second more or less will be the CPU run part of the command code. If the program is focused on waiting for the CPU to run in one queue, and not in the order of precedence, that is, like we go to the playground to play popular games need to queue, everyone is in order! You want to play again after you play (not finished), please go to the back line to wait. The situation is a bit like this:


In the assumption that Pro1, Pro2 is an urgent procedure, Pro3, Pro4 is the general procedure, in such an environment, because does not have the priority order, alas Ah! Pro1, Pro2 still have to continue 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 time to finish! How troublesome! So Luo, we want to prioritize the program! If the priority order is higher then the number of runs can be more than a few times, but not with the slower priority of the program to grab location! We can explain the priority of the program to the CPU schedule as follows:

As shown, with high priority Pro1, Pro2 can be taken two times, while less important Pro3, Pro4 run fewer times. As a result, Pro2 can be completed faster Pro1. Note that only, not the priority will be run two times! In order to achieve the above function, we Linux give the program a so-called "precedence" (priority, PRI), the lower the PRI value represents the higher the priority meaning. However, this PRI value is dynamically adjusted by the core, and the user cannot directly adjust the PRI value. Let's see where the PRI has appeared.

[[email protected] ~]# ps-lf s   UID   PID  PPID  C PRI  NI ADDR SZ wchan  TTY time          CMD4 s     0 1862 5 18623  2   0-  1514 wait   pts/1    00:00:00 bash4 R     0 18653 18625  0  77   0-  1102-      pts/1    00:00:00 PS

Because the PRI is the core dynamic adjustment, our users also have no right to interfere with the PRI! If you want to adjust the priority of the program, you have to pass the nice value! Nice value is the NI on the table! In general, the relevance of PRI to NI is as follows:

PRI (new) = PRI (old) + Nice

However, you should be particularly aware that if the original PRI is 50, it is not that we give a nice = 5, it will make the PRI become 55 Oh! Because the PRI is the system "dynamic" decision, although the Nice value can affect the PRI, however, the final PRI is still to be determined after a systematic analysis. In addition, the nice value is positive or negative, and since the smaller the PRI is, the lower the value is, the less the PRI value is, and the higher the precedence is processed. In addition, you have to be aware that:

    • The nice value can be adjusted to a range of-20 ~ 19;
    • Root is free to adjust the nice value of his or her program, with a range of-20 ~ 19;
    • General users can only adjust their own program's nice value, and the range is only 0 ~ 19 (to avoid the general user preemption system resources);
    • The average user can only adjust the nice value higher, for example, Nice is 5, the future can only be adjusted to greater than 5;

That is to say, to adjust the precedence of a program is to "adjust the program's nice value"! So how do you give a program nice value? There are two ways, namely:

    • First run the program to give a specific nice value immediately: with the nice command;
    • Adjust the nice value of an existing PID: Use the Renice command.

Nice : The newly Run command gives the new nice value
[[email protected] ~]# Nice [-n number] command options and Parameters:  -N: followed by a value, the range of values-20 ~ 19. Example one: Use root to give a nice value of-5, used to run VI, and observe the program! [[email protected] ~]# Nice-n-5 VI &[1] 18676[[email protected] ~]# ps-lf S   UID   PID  PPID  C PRI  NI ADDR SZ Wchan  TTY time          CMD4 S     0 18625 18623  0   0-  1514 wait   pts/1    00:00:00 bash4 T     0 18676 18625  0  -5-  1242 finish pts/1    00:00:00 vi4 R     0 18678 1862 5  0   0-  1101-      pts/1    00:00:00 ps# The original bash PRI is a  , so the VI default should be 75. But given  Nice is-5, # So the PRI of VI is lowered! But not down to 70, because the core will also be dynamically adjusted! [[email protected] ~]# kill-9%1 <== test finished closing VI

As stated earlier, Nice is used to adjust the order of the program's operation priority! This is just a running example! When do you usually make the nice value bigger? For example, in the context of the system, some of the less important programs are performed: For example, backup work! Because the backup work is quite a drain on the system resources, this time can be the backup command of the nice value of a bit larger, you can make the system more equitable resource allocation!

Renice: Nice re-adjustment of existing programs
[[email protected] ~]# renice [number] PID options and Parameters: PID: The ID of a program!  Example one: Find your own bash PID and adjust that PID nice to 10[[email protected] ~]# ps-lf S   UID   PID  PPID  C PRI  NI ADDR SZ Wchan  TTY time          CMD4 S     0 18625 18623  0   0-  1514 wait   pts/1    00:00:00 bash4 R     0 18712 18625  0   0-  1102-      pts/1    00:00:00 ps[[email protected] ~]# Renice 10 1862518625:old priority 0, new priority 10[[email protected] ~]# ps-lf S   UID   PID  PPID  C PRI  NI ADDR SZ Wchan  TTY time          CMD4 S     0 18625 18623  0  -  1514 wait   pts/1    00:00:00 bash4 R     0 18715 18625  0  -  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 the value and PID can be followed. Because the next is the PID, so you have to use PS or other programs to observe the command to find the PID!

From the above example we also see that, although the program is modified bash, but the PS command triggered by the program will also inherit 10 Oh! Know it! The entire nice value can be passed between the parent program and the subroutine! In addition, in addition to Renice, in fact, the top of the same can be adjusted nice value!

Transfer from HTTP://VBIRD.DIC.KSU.EDU.TW/LINUX_BASIC/0440PROCESSCONTROL_3.PHP#DMESG

Order of operation of the program [go]

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.