To adjust the use of a CPU for a program:
First, use Taskset to make full use of multi-core CPU, so that the utilization of the CPU is balanced to each CPU
#taskset
-P, set an existing PID instead of reopening a new task
-C, specify a processing, you can specify multiple, comma-delimited, or specify a range, such as: 2,4,5,6-8.
1, switch a process to the specified CPU
TASKSET-CP 3 13290
2, let a program run on the specified CPU
Taskset-c 1,2,4-7 tar jcf test.tar.gz test
It should be noted that TASKSET-CP 3 13290 does not inherit the parent process when setting an existing PID.
So a command like tar zcf xxx.tar.gz xxx is best to specify the CPU at startup, and if it is already started, you need to specify the gzip process for the tar call.
Second, use nice and renice to set the priority of program execution
Format: Nice [-n value] command
The Nice directive can change the priority level of program execution. Directive allows the user to specify a priority level, called a nice value, when executing the program.
This number is from the highest priority of 20 to the lowest priority of 19. The negative value is only root for power.
General users, you can also use the nice command? Walks 鲋 spring onion harmonic sash flies Tao angry?? The ice value is more and more high.
There are two ways to set a nice value for a program:
1, start executing the program given a nice value, with the nice command
2, adjust the PID of a running program nice value, with renice command
It is usually backed up by a nice value, in order not to occupy a very high amount of system resources.
Cases:
Nice-n tar zcf test.tar.gz test
A program initiated by NICE whose child processes inherit the nice value of the parent process.
View nice values
# nice-n-6 vim Test.txt &
# ps-l
F S UID PID PPID C PRI NI ADDR SZ Wchan TTY time CMD
4 S 0 19427 2637 0 75 0–16551 wait PTS/6 00:00:00 bash
4 T 0 21654 19427 0 71-6–23464 finish PTS/6 00:00:00 vim
Renice Adjust the nice value of a running program
Format: Renice [nice value] PID
Third, use Ulimit to limit CPU occupancy time
Note that the Ulimit limit is the current shell process and its derived child processes. Therefore, you can call Ulimit in your script to limit CPU usage time.
For example, limit the amount of CPU time in tar, in seconds.
# Cat Limit_cpu.sh
Ulimit-sht 100
Tar test.tar.gz test
If Tar takes longer than 100 seconds, tar will exit, which may result in incomplete packaging, so it is not recommended to use Ulimit to limit CPU time.
In addition, by modifying the system's/etc/security/limits configuration file, you can restrict the user.
Four, the use of the program with the use of CPU to adjust the function
Some programs have the ability to adjust CPU use, such as the Nginx server, through its configuration file, you can specify the CPU for the worker process, as follows:
Worker_processes 3;
Worker_cpu_affinity 0001 0010 0100 1000;
Here 0001 0010 0100 1000 are masks, representing the 1th, 2, 3, 4 CPU cores respectively, which makes the CPU use more evenly to each core.
Find a CPU running on a process:
The output format of the PS command can be customized with the-o parameter, and you can use the following command to display the process's corresponding execution CPU:
# Ps-eo PID,ARGS,PSR
The meaning of the parameter:
pid– Process ID
args– the command line arguments that are passed in when the process executes
psr– the CPU assigned to the process
For additional information about the PS command, please refer to the PS manual:
# mans PS
Example:
[Root@www ~]# Ps-eo PID,ARGS,PSR
.............
20965/usr/local/php-fcgi/bin/php 1
21683/bin/sh/usr/local/ddos/ddo 1
21684 Sleep 600 3
21746/sbin/udevd-d 2
21832/bin/sh/usr/local/ddos/ddo 1
21833 Sleep 600 0
21865 sshd:root@pts/0 3
21873-bash 1
21927 Ps-eo PID,ARGS,PSR 1
23520 [Pdflush] 3
23744 Nginx:master PROCESS/USR/1
23745 Nginx:worker Process 3
23749 Nginx:worker Process 3
23753 Nginx:worker Process 3
23755 Nginx:worker Process 3