Introduction

I needed to generate CPU load on a Linux server for testing purposes. Looking for a quick solution, I executed the yes command like this:

yes > /dev/null 

The yes command continuously outputs "y" infinitely. I assumed that "since it's an infinite loop, CPU usage should reach 100%," but when I checked the results, something unexpected happened.

Info: The test environment I used was a Linux server with 2 cores.

Looking at the top Command Output

When I checked with the top command, I noticed something interesting:

%Cpu(s): 29.8 us, 20.2 sy, 0.0 ni, 49.4 id, 0.0 wa, 0.0 hi, 0.7 si, 0.0 st %Cpu0 : 0.0 us, 0.0 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st %Cpu1 : 64.1 us, 35.9 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2095 root 20 0 221388 1836 1712 R 100.0 0.2 3:42.71 yes 

Even though the yes process was using 100%, the overall CPU usage was only around 50%. Looking at the CPU-specific usage, only CPU1 was running at nearly 100%, while CPU0 was mostly idle.

The yes Command Operates as a Single Thread

yes is a single-threaded process. Therefore, it can run on either CPU0 or CPU1, but it cannot run on both CPUs simultaneously.

What Does CPU Usage of 100% Mean?

The %CPU value shown in the process list of top means "using 100% of a single CPU core."

Therefore, when running a single yes process on a 2-core system:

  • CPU0: 0%
  • CPU1: 100%

This results in yes showing 100% in the process list, while overall CPU usage is approximately 50%.

How to Achieve 100% Overall CPU Usage?

To load all CPU cores, you need to run processes equal to the number of CPU cores. So you can execute the following command:

for i in $(seq $(nproc)); do yes > /dev/null & done 

Warning: After testing is complete, don't forget to run pkill yes.

Adding More Cores Doesn't Speed Things Up

Since yes is single-threaded, its processing performance depends on the performance of a single core.

Therefore, even if you increase the CPU to 32 cores, the processing speed of yes itself will not fundamentally improve.

On the other hand, for tasks that can operate with multiple threads such as software builds or data analysis, performance improvement can be expected as the number of CPU cores increases.

It's not the number of cores that matters, but whether the software can effectively utilize those cores.

Summary

Here's what I learned from this investigation:

  • yes operates as a single-threaded process
  • A single-threaded process can only utilize one CPU core at a time
  • Running a single yes command will not result in 100% overall CPU usage
  • To load all CPU cores, you need multiple processes (or threads) proportional to the number of CPU cores

CPU usage is something we often glance at casually, but understanding what's happening behind the scenes requires actual investigation. By testing with simple commands, I was able to gain a deeper understanding of how Linux and operating systems work.