Program running sequence)

Source: Internet
Author: User
AboutProgramRunning sequence

We know that Linux is an environment where many people workTopWe also found that many programs are running at the same time, but most of them are in sleeping state. Think about it. If all programs are awakened at the same time, what should the CPU process first? That is to say, the program is run in a high priority order? This requires consideration of the program's priority and CPU scheduler!

The CPU schedule is different from the routine schedule in the previous chapter. CPU scheduling refers to the calculation code for each program to be run by the CPU, while the routine work scheduling refers to scheduling a program at a certain time and then handing it over to the system for operation. CPU scheduling is highly correlated with the operating system!

 

 

Priority and nice values

 

We know that the CPU can run up to several GB of micro-commands in one second, and each program can be switched to run by the CPU through the core CPU schedule, therefore, each program will be executed by the CPU in more or less one second. If the program is concentrated in a queue waiting for the CPU to run, there is no priority, that is, like we need to queue up to play popular games on the playground, everyone will come in order! If you want to play it again after you finish it, please wait in the queue later. The situation is a bit like below:

 

Suppose pro1 and pro2 are urgent programs, and pro3 and pro4 are general programs. In such an environment, they are not prioritized! Pro1 and pro2 still have to wait without preferential treatment! If pro3 and pro4 work stinks and grows! So urgent pro1 and pro2 will have to wait for half a day to complete! Really troublesome! So we want to prioritize programs! If the priority is high, the running times can be more than once, without the need to compete with slow-priority programs! We can explain the program priority and CPU schedule as follows:

As shown in, pro1 and pro2 with high priority can be used twice, but pro3 and pro4 with less importance have fewer runs. In this way, pro1 and pro2 can be quickly completed! You must note that, yes, the priority is not always twice! To achieve the above functions, Linux gives the program a so-called "priority (PRI)", whichThe lower the PRI value, the higher the priority. However, this pri value is dynamically adjusted by the core, and users cannot directly adjust the PRI value.Where did pri appear?

[Root @ WWW ~] # PS - Lf s uid PID ppid C pri Ni addr sz wchan tty time cmd 4 S 0   18625   18623    2    75     0 - 1514   Wait PTS/ 1      00 : 00 : 00  Bash  4 R 0  18653   18625    0    77     0 - 1102 -Pts/ 1      00 : 00 : 00   PS 

 

Since PRI is the core dynamic adjustment, We users do not have the right to interfere with PRI! If you want to adjust the program's priority, you have to go through the nice value! Nice is the Ni in the above table! In general, the correlation between PRI and Ni is as follows:

 
PRI (new) = PRI (old) +Nice

However, you should pay special attention to the fact that if the original PRI was 50 and we didn't give a nice = 5, it would make pri 55! Because PRI is determined by the system "dynamic", although the nice value can affect PRI, the final PRI is determined only after system analysis. In addition, nice values are positive and negative, and since PRI is run earlier and earlier,When the nice value is negative, the program will reduce the PRI value, that is, it will be processed preferentially.In addition, you must note that:

    • Nice value can be adjusted from-20 ~ 19;
    • Root can adjust the nice value of programs of others at will, and the range is-20 ~ 19;
    • Generally, users can only adjust their own program nice value, and the value range is only 0 ~ 19 (avoid general users from occupying system resources );
    • Generally, you can only increase the nice value. For example, if Nice is 5, you can only adjust it to 5 in the future;

That is to say, to adjust the priority of a program, it is to "adjust the nice value of the program! So how to give a program nice value? There are two methods:

    • A specific Nice value is immediately given when the program starts to run: Use the nice command;
    • Adjust the nice value of an existing PID: Use the renice command.

 

Nice: The New Run Command gives a new nice value.
[Root @ WWW ~] # Nice [-N number] Command Options and parameters: -N: Followed by a value. The value range is- 20 ~ 19  . Example 1: Use root  Nice The value is- 5  , Used to run VI and observe the program! [Root @ WWW ~] # Nice -N- 5 VI & [  1 ] 18676  [Root @ WWW ~] # PS -Lf s uid PID ppid C pri Ni addr sz wchan tty time cmd  4 S 0   18625   18623    0    75     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    77     0 - 1101 -Pts/ 1      00 : 00 : 00   PS  # The original bash PRI is  75 So VI should be 75 . However, due Nice - 5 , # The PRI of VI is reduced! But not  70  Because the core will be dynamically adjusted! [Root @ WWW ~] # Kill - 9 % 1 <= Close VI after testing

As mentioned above, nice is used to adjust the program running priority! This is just a running example! When do I usually increase the nice value? For example, some unimportant programs are involved in the background work of the system, such as backup work! Because the backup process consumes a considerable amount of system resources, you can increase the nice value of the Backup command to make the system resources more equitable!

 

Renice: the nice of the existing program is adjusted.
[Root @ WWW ~] # Renice  [Number] PID options and parameters: PID: the ID of a program! Example 1: Find your own bash PID and Nice Adjusted 10  [Root @ WWW ~] # PS - Lf s uid PID ppid C pri Ni addr sz wchan tty time cmd  4 S 0   18625   18623    0    75     0 - 1514   Wait PTS/ 1     00 : 00 : 00  Bash  4 R 0   18712   18625    0    77     0 - 1102 -Pts/ 1      00 : 00 : 00  PS  [Root @ WWW ~] # Renice   10   18625  18625 : Old priority 0 , New priority 10  [Root @ WWW ~] # PS - Lf s uid PID ppid C pri Ni addr sz wchan tty time cmd  4 S 0   18625  18623    0    85    10 - 1514   Wait PTS/ 1      00 : 00 : 00  Bash  4 R 0   18715   18625    0   87    10 - 1102 -Pts/ 1      00 : 00 : 00   PS 

If you want to adjust an existing program, you have to use renice. The method is simple. renice is followed by a value and a PID. Because the PID is followed, you must find the PID by using the PS or command observed by other programs!

As we can see from the above example, although the bash program is modified, the nice in the ps command triggered by the program will inherit from 10! Understand it! The entire nice value can be passed between the parent program --> subprograms! In addition to renice, the top can also adjust the nice value!

 

From http://vbird.dic.ksu.edu.tw/linux_basic/0440processcontrol_3.php#dmesg

 

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.