Easy C++ Program to Make a CPU go to 100% Useage.

tank171

Member
May 27, 2007
93
0
0
I made this program to try to crash my computer by making it run out of RAM, but something else happened. It made my single core CPU go to 100% according to task manager. I tried this on my C2D computer, and it made the CPU go to 50%. Then I opened 2 at once on my C2D machine and it made the CPU usage go to 100%. So I believe you just have to open one of these per core to get 100% usage. I think that this simple program would be great for a CPU stress test.



#include <iostream>
using namespace std;

int main ()
{
int * p;

while (p == p){
(*p++)+1;
}
}
 

Lord Banshee

Golden Member
Sep 8, 2004
1,495
0
0
Other than that only test integer unit of the CPU.. i believe most of the heat comes from floating point unit.
 

tank171

Member
May 27, 2007
93
0
0
If you want float, I got it.

#include <iostream>
using namespace std;

int main ()
{
float * p;

while (p == p){
(*p++)+1.144564542315345;
}
}
 

tank171

Member
May 27, 2007
93
0
0
Here is a hybrid.


#include <iostream>
using namespace std;

int main ()
{
int * i;
float * p;

while (p == p){
(*p++)+1.144564542315345;
(*i++)+1;
}
}
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
Not the best idea to use a pointer like that. If you ever modifed the values you may crash the os.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
what's with the 'OS crashing' talk? Are you running this on same archaic system w/o protected memory?
 

3NF

Golden Member
Feb 5, 2005
1,345
0
0
It would be nice for a stress test if you could actually stop it gracefully - what are you doing, end task?
 

Snapster

Diamond Member
Oct 14, 2001
3,917
0
0

#include <iostream>
using namespace std;

int main ()
{
while (true){}
}

Will have the same end result, again no graceful way of ending. If you wanted to test memory you need allocate in a loop, ie new instances of an object, add to an array etc.

 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
One of my professors has a little program like that. I don't think I have the source, unfortunately. I think it mallocs by powers of 2, then when malloc fails, it starts incrementing linearly until it reaches the limit and reports the results.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Originally posted by: tank171
Here is a hybrid.


#include <iostream>
using namespace std;

int main ()
{
int * i;
float * p;

while (p == p){
(*p++)+1.144564542315345;
(*i++)+1;
}
}

Does this even run? You are just going through address space and changing values of memory sequentially. Once you hit p or i = 0 or in a protected range you are gonna GPF your program.

Why not try to clean that up, allocate 2 gigs of memory (32 bit os which is all you are allowed to allocate per process) and run your algoritm in valid address space.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Originally posted by: brandonb
Originally posted by: tank171
Here is a hybrid.


#include <iostream>
using namespace std;

int main ()
{
int * i;
float * p;

while (p == p){
(*p++)+1.144564542315345;
(*i++)+1;
}
}

Does this even run? You are just going through address space and changing values of memory sequentially. Once you hit p or i = 0 or in a protected range you are gonna GPF your program.

Why not try to clean that up, allocate 2 gigs of memory (32 bit os which is all you are allowed to allocate per process) and run your algoritm in valid address space.

Haha. Both values are signed and uninitialized, so who the hell knows where it will start writing to memory. I dub this the Machinegun of Doom program.

But no, as someone else mentioned, no O/S crash. Just a 0xc0000005 at some point.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Here's my improved version:

int main()
{
while (true)
{
outportb(rnd(0xffff), rnd(0xff));
puts("oops");
}
}
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
the program runs fine, didn't anyone notice that there are no assignments to either pointer location? He just runs through the virtual address space for both pointers, so yeah ALU is used but only for pointer arithmetic. Aggressively optimizing compiler will assemble this program to just the loop, and will discard the contents.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Originally posted by: HigherGround
the program runs fine, didn't anyone notice that there are no assignments to either pointer location? He just runs through the virtual address space for both pointers, so yeah ALU is used but only for pointer arithmetic. Aggressively optimizing compiler will assemble this program to just the loop, and will discard the contents.

You're right, I didn't catch that. He creates expressions but never assigns the results, so they're both nops.

Still funny though.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
This makes me laugh


(*p++)+1.144564542315345;

What is this, a fraction of a pointer or something?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Originally posted by: xtknight
This makes me laugh


(*p++)+1.144564542315345;

What is this, a fraction of a pointer or something?

Ha. Well, let's see... the expression will increment p by the size of a pointer, dereference it, create a float and copy the value into it, then add 1.14456... bla bla to it. Except that as a previous poster noted there is no assignment, so the compiler won't generate any of this code .

I should note that's true if the postfix increment operator binds at higher precedence than the dereference operator, which I think it does.
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Originally posted by: brandonb
Originally posted by: tank171
Here is a hybrid.


#include <iostream>
using namespace std;

int main ()
{
int * i;
float * p;

while (p == p){
(*p++)+1.144564542315345;
(*i++)+1;
}
}

Does this even run? You are just going through address space and changing values of memory sequentially. Once you hit p or i = 0 or in a protected range you are gonna GPF your program.

Why not try to clean that up, allocate 2 gigs of memory (32 bit os which is all you are allowed to allocate per process) and run your algoritm in valid address space.

I haven't programmed in years, but that's what I thought. but I haven't programmed in years, so I thought I could be wrong as well.

Properly, I think it should be:


int main ()
{
int * i;
float * p;

while (p == p){
*(p++)+1.144564542315345;
*(i++)+1;
}
}

Oh, and FWIW, doesn't modulus have one of the highest cycle costs for FP? I seem to remember that from my ASM days. Exponential and logrithmic arithmetic is a good way to eat up cycles.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Originally posted by: aCynic2
Originally posted by: brandonb
Originally posted by: tank171
Here is a hybrid.


#include <iostream>
using namespace std;

int main ()
{
int * i;
float * p;

while (p == p){
(*p++)+1.144564542315345;
(*i++)+1;
}
}

Does this even run? You are just going through address space and changing values of memory sequentially. Once you hit p or i = 0 or in a protected range you are gonna GPF your program.

Why not try to clean that up, allocate 2 gigs of memory (32 bit os which is all you are allowed to allocate per process) and run your algoritm in valid address space.

I haven't programmed in years, but that's what I thought. but I haven't programmed in years, so I thought I could be wrong as well.

Properly, I think it should be:


int main ()
{
int * i;
float * p;

while (p == p){
*(p++)+1.144564542315345;
*(i++)+1;
}
}

Oh, and FWIW, doesn't modulus have one of the highest cycle costs for FP? I seem to remember that from my ASM days. Exponential and logrithmic arithmetic is a good way to eat up cycles.

I think the postfix increment operator binds before the dereference operator. If so shifting the start of the parens doesn't change the result of this expression. I haven't looked it up in the ARM though, and I don't do much c++ anymore. The OP needed the parens to separate the postfix increment from the addition.
 

exdeath

Lifer
Jan 29, 2004
13,679
10
81
Any endless loop will eat CPU time. Even doing nothing will execute immediate jumps as fast as possible.

Your attempt to eat ram didn't work? How big is your hard drive

If you want to burn memory, call new [] without delete in an endless loop and listen to your hard drive grind to a halt and watch the memory footprint in task manager start to skyrocket until you get a 'windows is low on virtual memory' warning

What is really fun is on old 8088 PCs with DOS, writing something that outputs random words to random IO ports in a endless loop. Did that one time and got all kinds of flashy crap on the screen, buzzing noises, and the floppy drives clicked and killed one of my disks before it finally froze.

Edit: never mind, ++ takes precedence over * but if thats what you were doing it should have crashed nearly immediately as soon as you read from an unallocated page. Since it's not crashing I can only assume that because you don't assign the result to anything, that whole calculation is ignored thus no reads are performed, just the pointer is incremented indefinitely.
 

Red Squirrel

No Lifer
May 24, 2003
67,913
12,379
126
www.anyf.ca
Writing programs like this is fun. In some cases theres no point, but its still fun. I've written many during C++ class when I was bored in college.

Note: Never do this on a citrix server. Those poor IT guys... year 2 we had local compilers as a result of us crashing the citrix server so often.


Something fun to mess with is sorting algorthms, like bubble sorting.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Originally posted by: RedSquirrel
Writing programs like this is fun. In some cases theres no point, but its still fun. I've written many during C++ class when I was bored in college.

Note: Never do this on a citrix server. Those poor IT guys... year 2 we had local compilers as a result of us crashing the citrix server so often.


Something fun to mess with is sorting algorthms, like bubble sorting.

Trees. Gotta love trees.
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Originally posted by: Markbnj
Trees. Gotta love trees.

I didn't make it far enough into my programming degree to see trees.

I pretty much learned how bubble, insertion and quick sorts worked, also searching a list, but not searching a tree, since I didn't make it to trees.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |