Simulating CPU Saturation in Linux Environments
Verifying Available Compute Resources
Before applying load, confirm the number of logical processing units available on the system. For this demontsration, a machine with 16 logical cores is used.
bash lscpu | grep "^CPU(s):"
Output: CPU(s): 16
Alternatively, parse /proc/cpuinfo directly:
bash grep -c ^processor /proc/cpuinfo
Output: 16
Establishing a Baseline
Use the top command to observe current resource utilization. The id column in the %Cpu(s) line represents the idle percentage. Initially, the system should show a high idle value near 100%.
bash top -bn1 | grep "Cpu(s)"
Output: %Cpu(s): 0.2 us, 0.1 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
Saturating a Single Core
To max out a single logical core, execute a script that runs an infinite loop performing continuous no-op operations in the background.
Create stress_single.sh:
bash #!/bin/bash
consume_one_core() { while true; do : done }
consume_one_core &
Run the script and verify the impact:
bash bash stress_single.sh ps -eo pid,cmd,%cpu | grep stress_single
Monitoring with top will reveal that one specific process is consuming 100% of a processor, and the overall idle percentage drops accordingly.
Saturating All Available Cores
To completely exhaust all processing capacity, spawn multiple concurrent instances of the infinite loop, matching the total number of logical cores.
Create stress_full.sh:
bash #!/bin/bash
burn_cpu() { while true; do : done }
launch_stress() { local target_cores=$1 local current=0 while [ $current -lt $target_cores ]; do burn_cpu & current=$((current + 1)) done }
launch_stress "$1"
Execute the script targeting 16 cores:
bash bash stress_full.sh 16
Observe the system metrics again:
bash top -bn1 | head -n 5
The output will indicate that the idle percentage (id) has dropped to 0.0, and multiple background processes are each consuming 100% of a distinct logical processor, confirming total CPU saturation.