Adjust the usage of a program corresponding to the CPU:
I. Use taskset to make full use of multi-core cpu to balance cpu usage to each cpu
# Taskset
-P: Set an existing pid instead of restarting a new task.
-C: Specifies a processing item. Multiple processing items can be specified and separated by commas. You can also specify a range, for example, 2, 4, 5, 6-8.
1. Switch a process to the specified cpu
Taskset-cp 3 13290
2. Run a program on the specified cpu.
Taskset-c 1, 2, 4-7 tar jcf test.tar.gz test
Note that when taskset-cp 3 13290 sets an existing pid, the child process does not inherit the parent process,
Therefore, for commands like tar zcf xxx.tar.gz xxx, it is best to specify the cpu at startup. If the cpu is already started, specify the gzip process called by tar.
2. Use nice and renice to set the priority of Program Execution
Format: nice [-n value] command
Nice commands can change the priority level of program execution. The command allows the user to specify a priority level when executing the program, which is called the nice value.
This value ranges from-20 of the highest priority to 19 of the lowest priority. Only root has the right to use negative values.
Generally, you can use nice commands to manage the priority of execution programs, but you can only increase the nice value.
You can set the nice value for a program in two ways:
1. A nice value is given when you start executing the program. Use the nice command.
2. Adjust the nice value of the PID of a running program and run the renice command.
Generally, you can increase the nice value to back up the data so that it does not occupy a large amount of system resources.
Example:
Nice-n 10 tar zcf test.tar.gz test
The child process of a program started by nice inherits 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 the running program
Format: renice [nice value] PID
3. Use ulimit to limit the cpu usage time
Note that ulimit limits the Current shell Process and its derived child processes. Therefore, you can call ulimit in the script to limit the cpu usage time.
For example, the cpu usage time of tar is limited, in seconds.
# Cat limit_cpu.sh
Ulimit-SHt 100
Tar test.tar.gz test
If tar takes more than 100 seconds, tar will exit, which may lead to incomplete packaging. Therefore, we do not recommend that you use ulimit to limit the cpu usage.
In addition, you can modify the/etc/security/limits configuration file of the system to restrict user access.
4. Use the cpu usage adjustment function provided by the program
Some programs provide the cpu usage adjustment function, such as the nginx server. Through its configuration file, you can specify the cpu for the worker process, as shown below:
Worker_processes 3;
Worker_cpu_affinity 0001 0010 0100 1000;
Here 0001 0010 0100 1000 is a mask, which represents 1st, 2, 3, and 4 cpu cores respectively. This makes the cpu usage evenly to each core.
Find a CPU running in a process:
The output format of the ps command can be customized using the-o parameter. You can use the following command to display the execution CPU of the process:
# Ps-eo pid, args, AND SrS
Parameter description:
Pid-process ID
Args-command line parameters passed in when the process is executed
HDR-CPU allocated to the Process
For more information about ps commands, see ps manual:
# Man ps
Example:
[Root @ www ~] # Ps-eo pid, args, AND SrS
.............
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