Number of CPUs used for dynamic Linux switching Abstract: to test some code, the running result will be affected by multi-core parallel execution, so we hope to adjust the number of CPUs used. The preceding method on the network is to add a maxcpus parameter to the kernel startup parameter. However, if this is the case, it is too troublesome to restart each time the kernel is switched. Think... to test some code, the running result will be affected by multi-core parallel processing, so we hope to adjust the number of CPUs used. The preceding method on the network is to add a maxcpus parameter to the kernel startup parameter. However, if this is the case, it is too troublesome to restart each time the kernel is switched. Think about Linux, it should be very powerful, so you can modify the number of CPUs dynamically. Inadvertently saw a file named cpu-hotplug.txt under the Documentation folder of Linux code, so I looked at it, we can see in/sys/devices/system/cpu that the folders of each CPU are named by cpuX, such as cpu0, cpu1, and cpu2. These folders contain an online file. If the value is 0, the CPU is disabled. If the value is 1, the CPU is enabled. Note: The root permission is required. Because I only need to switch between single-core and multi-core, I wrote two scripts in/usr/local/sbin: singlecore #! /Bin/bashcpus_dir = "/sys/devices/system/cpu" for cpu in $ (ls "$ cpus_dir" | grep 'cpu [0-9] \ + ') docpu_online = "$ cpus_dir/$ cpu/online" if [-e "$ cpu_online" & $ (cat $ cpu_online) = 1] thenecho 0> "$ cpu_online" fidonemulticore #! /Bin/bashcpus_dir = "/sys/devices/system/cpu" for cpu in $ (ls "$ cpus_dir" | grep 'cpu [0-9] \ + ') docpu_online = "$ cpus_dir/$ cpu/online" if [-e "$ cpu_online" & $ (cat $ cpu_online) = 0] thenecho 1> "$ cpu_online" after fidone, you only need to run sudo singlecore or sudo multicore ~ By the way, I was wondering what would happen if I disabled all the CPUs? The result shows that cpu0 does not have an online file, that is, at least one CPU is available in Linux.