LPI101-Topic103 for ibm lpi Review: GNU and Unix Commands (6) changing process priorities

Source: Internet
Author: User

Abstract: By setting and changing the priority of a process, the process obtains the required run time segment.

1 Overview

This article will help you lay a solid foundation in the following aspects:

  • (1) Understanding Process Priority
  • (2) set process priority
  • (3) Change Process Priority

This article is the goal of preparing for the LPIC-1 103.6, the weight is 2.

2. Understand your priority

Like most modern operating systems, Linux can run multiple processes. This is done by sharing CPU and other resources between processes. If a process occupies 100% of the CPU, other processes will lose response.

If you run the TOP Command, processes are displayed in descending order of CPU usage by default. In the preceding example, we show a poor man's colck script, which prints the current time every 30 seconds without any action in other time periods. Top may not display this process because it does not use CPU for most of the time.

Typical top output:

Mem:   3924872k total,  3429320k used,   495552k free,   529468k buffersSwap:  7574520k total,        0k used,  7574520k free,  1872160k cached  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 1580 mysql     20   0  870m  73m 6748 S  0.3  1.9 100:09.57 mysqld14163 root      20   0 15024 1384 1008 R  0.3  0.0   0:00.06 top    1 root      20   0 19228 1532 1248 S  0.0  0.0   0:11.62 init    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd    3 root      RT   0     0    0    0 S  0.0  0.0   0:08.19 migration/0    4 root      20   0     0    0    0 S  0.0  0.0   0:10.33 ksoftirqd/0    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0    6 root      RT   0     0    0    0 S  0.0  0.0   0:18.44 watchdog/0    7 root      RT   0     0    0    0 S  0.0  0.0   0:08.87 migration/1    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/1    9 root      20   0     0    0    0 S  0.0  0.0   0:14.39 ksoftirqd/1   10 root      RT   0     0    0    0 S  0.0  0.0   0:18.44 watchdog/1   11 root      RT   0     0    0    0 S  0.0  0.0   0:07.99 migration/2   12 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/2   13 root      20   0     0    0    0 S  0.0  0.0   0:14.89 ksoftirqd/2   14 root      RT   0     0    0    0 S  0.0  0.0   0:17.60 watchdog/2   15 root      RT   0     0    0    0 S  0.0  0.0   0:06.82 migration/3   16 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/3   17 root      20   0     0    0    0 S  0.0  0.0   0:05.61 ksoftirqd/3   18 root      RT   0     0    0    0 S  0.0  0.0   0:17.79 watchdog/3   19 root      20   0     0    0    0 S  0.0  0.0   6:35.02 events/0   20 root      20   0     0    0    0 S  0.0  0.0   6:39.81 events/1   21 root      20   0     0    0    0 S  0.0  0.0  70:32.21 events/2   22 root      20   0     0    0    0 S  0.0  0.0   8:35.72 events/3   23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cgroup   24 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper   25 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns   26 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr   27 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm   28 root      20   0     0    0    0 S  0.0  0.0   0:45.88 sync_supers   29 root      20   0     0    0    0 S  0.0  0.0   0:53.20 bdi-default   30 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0   31 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/1

Some processes in your system may use a lot of CPU resources, such as video editing tools, image format conversion tools, or audio format conversion tools, such as converting mp3 to Ogg.

When your machine has only one or a limited number of CPUs, you need to decide how to share CPU resources among multiple competing processes. This is usually done by selecting a process to run it for a period (time slice), or stopping it until it waits for events such as Io to complete. To ensure that important processes do not lose response due to lack of CPU resources, this option is based on the scheduling priority. The NI column in the Table above represents the scheduling priority of the process, also known as niceness. The priority is usually between-20 and 19, where-20 indicates the highest priority, and 19 indicates the lowest priority.

2.1 view priority using PS

In addition to the top command, you can also use the ps command to display the process priority. When PS-L is used, the output Ni column is the process priority. As follows:

[root@centos192 ~]# ps -lF S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD4 S     0 14148 14146  0  80   0 - 27074 wait   pts/1    00:00:00 bash4 R     0 14548 14148  0  80   0 - 27030 -      pts/1    00:00:00 ps
2.2 default priority

You can guess from the above two examples that at least the priority of processes started by common users is 0. This is correct for the current Linux system. This default value can be obtained by running the nice command without parameters.

ot@centos192 ~]# nice0
3. Set priority

Build a CPU-intensive script before learning how to set and change the priority. It will show that the priority does have an impact.

3.1 a cpu-intensive script

We will create a small script that only uses CPU. This script uses two input parameters: Count value and label. First, the tag and the current system time will be printed, and then enter the cycle. Each cycle will reduce the Count value by one. When the Count value is 0, the cycle will be terminated and the tag and time will be printed. This test script does not carry out error detection and is not very robust, but it can be used to explain the problem.

[root@centos192 ~]# echo 'x="$1"' > count1.sh[root@centos192 ~]# echo 'echo "$2" $(date)' >> count1.sh[root@centos192 ~]# echo 'while [ $x -gt 0 ]; do x=$((x-1)); done' >>count1.sh[root@centos192 ~]# echo 'echo "$2" $(date)' >>count1.sh[root@centos192 ~]# cat count1.shx="$1"echo "$2" $(date)while [ $x -gt 0 ]; do x=$((x-1)); doneecho "$2" $(date)

If you run this script on your computer, the output may be as follows:

[Root @ centos192 ~] #./Count1.sh 10000 AA Friday August 09, 2013 10:30:28 CSTA Friday August 09, 2013 10:30:28 CST [root @ centos192 ~] #./Count1.sh 99000 AA Friday August 09, 2013 10:30:43 CSTA Friday August 09, 2013 10:30:45 CST

The current location is safe. Now we run this script in the background and use the top command to view the CPU usage.

Tasks: 188 total,   2 running, 186 sleeping,   0 stopped,   0 zombieCpu(s): 23.8%us,  1.2%sy,  0.0%ni, 74.8%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%stMem:   3924872k total,  3438424k used,   486448k free,   529588k buffersSwap:  7574520k total,        0k used,  7574520k free,  1873432k cached  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND29561 root      20   0  105m 1096  728 R 99.6  0.0   1:06.54 bash  695 root      20   0     0    0    0 S  0.3  0.0   4:35.89 vmmemctl29562 root      20   0 15024 1388 1008 R  0.3  0.0   0:00.13 top    1 root      20   0 19228 1532 1248 S  0.0  0.0   0:11.67 init    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd

We can see that our script uses 99.6 of the CPU resources. If you want to use other CPUs, run the above command. If the script runs continuously, other work of the system will be seriously affected.

3.2 use nice command to set priority

Since we can keep a CPU busy, let's take a look at how to set the priority of a process. Summarize the knowledge we have learned so far:

  • (1) Linux and UNIX systems have a total of 40 priority levels, from-20 to + 19.
  • (2) The default priority of processes started by normal users is 0.
  • (3) The-L option of the ps command can display the priority of the process.
  • (4) Nice command to display the default priority

In addition to displaying the default priority, nice can also be used to start a process based on the specified priority. You can use the-N (or -- Adjustment) option to add a positive value and a negative number to reduce the priority value. Remember that the higher the priority value, the lower the priority. Generally, only the root user can specify a negative value. That is to say, a common user can only lower the process priority but cannot increase the priority.

To demonstrate how to set the priority using nice, we start two processes using the same script. One process has a priority of 19 and the other has a default priority. After one second, use PS-L to display the Process status, including priority. At last, we added a 30-second sleep to ensure that the command was exited after the sub-SH was executed. As shown in:

[Root @ centos192 ~] # (SH count1.sh 2000000 A &); (Nice-N 19 sh count1.sh 2000000b &); sleep 1; PS-L; sleep 30a August 15, August 09, 2013 Friday 12:29:10 cstb August 15, August 09, 2013 Friday 12:29:10 cstf s uid PID ppid C pri Ni addr sz wchan tty time limit 4 S 0 29538 29536 0 0-27074 wait pts/2 00:00:00 bash0 R 0 30573 1 99 80 0-26514-pts/2 00:00:01 sh0 R 0 30575 1 99 19-26514-pts/2 00:00:00 sh4 R 0 30579 29538 0 80 0-27030-pts/ 2 00:00:00 PSB Friday August 09, 2013 12:29:39 CSTA Friday August 09, 2013 12:29:40 CST

You may be surprised that the two sub-processes have almost finished running at the same time. Does the priority we set have no effect? Remember that our script only uses one CPU. The running machine is equipped with a 4-core CPU. In this way, the two sub-processes run on different CPU cores separately, and there may be no need to prioritize them. To fully occupy four CPU cores, we run six processes at the same time, with priority values of, and 19.

[Root @ centos192 ~] # (SH count1.sh 5000000 A &); (Nice-N 4 sh count1.sh 500366b &); (Nice-N 8 sh count1.sh 5000000 C &); (Nice-N 12 sh count1.sh 500366d &); (Nice-N 16 sh count1.sh 5000000 E &); (Nice-N 19 sh count1.sh 500366f &); sleep 1; PS-L; sleep 30; a. Friday, January 1, 12:25:05 cstb Friday, August 09, 2013 12:25:05 CSTD Friday, August 09, 2013 12:25:05 CSTE Friday August 09, 2013 12:25:05 cstf Friday August 09, 2013 12:25:05 cstf s uid PID ppid C pri Ni addr sz wchan tty time limit 4 S 0 29538 29536 0 80 0-27074 wait pts/2 00:00:00 bash0 R 0 30471 1 50 80 0-26514-pts/2 00:00:01 sh0 R 0 30473 1 50 84 4-26514-pts/ 2 00:00:01 sh0 R 0 30475 1 49 88 8-26514-pts/2 00:00:00 sh0 R 0 30477 1 30 92 12-26514-pts/2 00:00:00 sh0 R 0 30480 1 12 96 16 -26514-pts/2 00:00:00 sh0 R 0 30484 1 6 99 19-26514-pts/2 00:00:00 sh4 R 0 30489 29538 0 80 0-27029-pts/2 00:00:00 PSC August 09, 2013 Friday 12:26:19 CSTA Friday August 09, 2013 12:26:19 cstb Friday August 09, 2013 12:26:20 CSTD Friday August 09, 2013 12:26:49 CSTE Friday August 09, 2013 12:27:15 cstf Friday August 09, 2013 12:27:25 CST

It can be seen that, as a result, multiple processes will compete for the CPU. Basically, the higher the priority, the more opportunities to be executed, the less time it takes to complete the execution.

The nice command cannot use the command list or pipeline as the parameter, just like the nohup command.

4. Change Priority 4.1 renice

If you have started a process and realize that you need to change the priority of the process, you can use the renice command. Renice specifies the absolute priority value rather than the adjustment amount.

I [root @ centos192 ~] # Sh count1.sh 10000000 A & [1] 30799 [root @ centos192 ~] # A. Friday, January 1, August 09, 2013 12:43:19 CST [root @ centos192 ~] # Renice 1 30799; PS-l 3079930799: Old priority 0, new priority 1f s uid PID ppid C pri Ni addr sz wchan tty time limit 0 r 0 30799 29538 97 81 1-26514-pts/21 sh count1.sh0000000 A [root @ centos192 ~] # Renice + 3 30799; PS-l 3079930799: Old Priority 1, new priority 3f s uid PID ppid C pri Ni addr sz wchan tty time limit 0 r 0 30799 29538 98 83 3-26514-pts/0: 2 40 sh count1.sh0000000

Note that only the root user can improve the scheduling priority of the process and make the process not nice. For more information, see the nice and renice man manual pages.

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.