How to write a C or C++ program to act as a memory and CPU cycle filler? How to write a C or C++ program to act as a memory and CPU cycle filler? unix unix

How to write a C or C++ program to act as a memory and CPU cycle filler?


http://weather.ou.edu/~apw/projects/stress/

Stress is a deliberately simple workload generator for POSIX systems. It imposes a configurable amount of CPU, memory, I/O, and disk stress on the system. It is written in C, and is free software licensed under the GPLv2.

The functionality you seek overlaps the feature set of "test tools". So also check out http://ltp.sourceforge.net/tooltable.php.


If you have a single core this is enough to put stress on a CPU:

while ( true ) {   x++;} 

If you have lots of cores then you need a thread per core.

You make it variably hungry by adding a few sleeps.

As for memory, just allocate lots.


There are several problems with such a design:

  1. In a virtual memory system, memory size is effectively unlimited. (Well, it's limited by your hard disk...) In practice, systems usually run out of address space well before they run out of backing store -- and address space is a per-process resource.
  2. Any reasonable (non realtime) operating system is going to limit how much CPU time and memory your process can use relative to other processes.
  3. It's already been done.

More importantly, I don't see why you would ever want to do this.