Use the taskset command to restrict the CPU of a process
It is often felt that the system resources are insufficient, and no more than three important services can be run on a single machine. However, every day we need to perform backup compression and other operations on the machine, and the network will be transmitted for a long time, this affects the insufficient system resources;
At this time, we can limit some less important tasks, such as copy, backup, and synchronization, to a single cpu or a core of a multi-core cpu, although this is not necessarily the most effective method, it can maximize the use of valid resources to reduce the cpu resources occupied by less important processes;
Taskset can help us complete this task, and the operation is very simple;
The tool is installed by default. The rpm package name is util-linux.
The following example shows how to use a previously written shell script that consumes CPU resources to exhaust the resources of four CPUs on a 16-CPU machine:
Use the top command to see the effect of 4 CPUs running full:
Now you can use the taskset command to adjust the CPU used by these processes:
Taskset-cp 1 25718 taskset-cp 3 25720
Taskset-cp 5 25722
Taskset-cp 7 25724 in top to see the effect:
Haha, the CPU usage has been provisioned. Similarly, we can make a process use only a few CPUs:
1 taskset-cp 25718
For more details, see man taskset.