Skip to main content
GameDev.net gamedev.net
🔒 Locked

Programming Assignment: How to make CPU hog 100% and allocate RAM to the max value?

Started by tom_mai78101 Sep 26, 2011 at 2:19 AM 11 replies 4.6k views
Original Post
tom_mai78101
tom_mai78101
I'm asking for a homework tip (not [color="#ff0000"]help, emphasizing the word "help".) on how to complete this assignment.

I would rather be asking my professor why would you do this to a computer, and not take this assignment, but unfortunately he's out of state. Even unfortunately, it's for 5 grade points in his Operating System Concept class added to the entire semester. (That means, if you finish this assignment, you will earn 5 grade points on your total class grade. If you have 55 grade points, add 5 to it, and pass the class. I don't know what people called them, but the principles are the same.)

I was thinking of using a simple while loop, to allocate all of the RAMs given on an average computer and then try multithreading to achieve a busy 100% stable CPU, as according to the assignment tasks. But then, a classmate of mine said multithreading won't work that way, and was considering on idling the CPU to the maximum. But he doesn't know how to do it from there.

Usually, professors and other developers will try to avoid these programming tasks as much as possible. To be honest, I have no clue why he wanted to do this. So, I supposed it wouldn't hurt anybody to ask for help on this problem.

Yes, yes, all of you. I know to consequences of writing such a terrible terrible program, even if the program is the homework of our assignment here. But please do forgive me.
Hodgman
Hodgman
Ask the OS how many hardware threads are available. Create that many threads (N.B. you already have one thread, so that number minus one). Set them to wait until you're done wasting all your RAM.
In a loop:
Allocate a very large block of memory of size X (using the OS's least restrictive allocation API?)
If success, fill the allocation with random bytes (check your OS's textbook for why you should bother writing to the allocated area wink.gif).
If fail, reduce the size of X.
Break from the loop when X gets too small and is still failing.

Now, tell the OS that all of your threads are real-time priority, and make each thread run in a tight loop executing some ALU-heavy instructions. Do not issue any idle/wait/sleep/power-saving commands inside the loop.

Then make a batch file that launches over 9000 concurrent instances of that program.

On Windows XP, I've locked up my quad-core PC by doing this before, and had to use the hard reset button sad.gif

And yes, this is a very strange question to ask! Perhaps he's hoping you'll demonstrate some insight into the workings of OS concepts, like virtual memory, preemptive multi-threading, etc...?
Adam_42
Adam_42
Note that if you want to get at all the RAM on a PC with more than about 2GB installed you'll need to use multiple processes or a 64-bit program.

You might find http://blogs.technet.com/b/markrussinovich/archive/2008/07/21/3092070.aspx useful.
Telastyn
Telastyn
Frankly this isn't that hard of a problem. How much code have you written to try and solve the problem?

You thought about it and then asked a classmate instead of just trying it out? C'mon... most of classwork is learning how to solve problems; how to do research on your own. Hodgman's answer is more than adequate, but as you start tackling harder and harder problems there's going to be fewer and fewer people with the ability to answer your question. Pretty quickly the problems will be specific enough that nobody will be able to answer your question and you're going to have to figure it out for yourself. Best start early with these easy ones.
Telgin
Telgin
The answers so far are pretty relevant, but I'd like to add in that you may have some difficulty getting the RAM maxed out. In particular, don't try to allocate a buffer the size of your entire RAM size, because the OS probably won't give it to you. malloc will fail or operator new will throw an exception, because the OS probably won't be willing or able to allocate a contiguous memory chunk that large. Maybe on an older computer with <1GB of RAM it might, then page most of it out, but I think even on 64-bit computers allocations of several GB may not work.

So instead, in your loop where you're wasting CPU time, just malloc or new a few MB at a time and throw away the pointer. It shouldn't take too long for that to eat up your RAM. And page file.
Success requires no explanation. Failure allows none.
rip-off
rip-off
On Linux, there is a supervisor which will kill processes when available RAM/Swap is exhausted. See here for some interesting information. There may also be ulimit thresholds in place.

If something like that isn't in place, a fork bomb is an easy way to hog all the resources - in a way that is hard to stop >=]
21st Century Moose
21st Century Moose
Facetious answer - easy as pie! Just install Lotus Notes! ;)
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 
Oberon_Command
Oberon_Command
Seconding the forkbomb. I wrote up a very simple forkbomb just now that completely locked up the computer in less than five minutes. Only took 5 lines of code. It didn't suck up all the CPU, but it sure ate up the RAM quick enough...
ryan20fun
ryan20fun
if you want threads...

(using C++ )
i made a DLL's entry point function create a thread and then that thread would create a window.

the catch was the the entry point kept being called, so in the end it would lock up my pc as soon as i hit F5.

will that work for your assign ment ?
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.
taz0010
taz0010
If you have a 64 bit OS then compile x64 so you can access all avaliable RAM. If you're compiling 32 bit, I'm pretty sure the OS will complain if your program tries to allocate more than 2GB total, so you'll need to spawn multiple processes (not threads).

I'd suggest running at least as many *logical* threads as your computer has, or you won't max out machines with hyperthreading. These threads can run infinite loops. If that doesn't keep you at 100%, then try allocating a *lot* of threads and have them all do the same. Context switching will incur considerable overhead.

If you really want to bring the system to it's knees, have these threads acquire critical sections in a chain which results in non-stop blocking and context switches. Your aim is not to cause deadlock, but have each thread being constantly blocked and unblocked.

ryan20fun
ryan20fun
also if you want to chow up cpu cycles, create and then destroy buffers.
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.
tom_mai78101
tom_mai78101
Now that I've finished this week's class lessons, this is what I've learned:

1. The homework the professor tasked us to do last week ended up having all of my classmates being unable to finish it, let alone program it; the task is super difficult.
2. He mentioned that in another class, he gave them the same homework, and gotten to know that there's one student said he was able to complete it. However, the student didn't demoed (or showed) his program, so the professor extended the period of this homework to be dued some time later in order for both classes to "iron this assignment out".

He didn't spoke of the given assignment any hints or anything, and told us to "think" about it. He wanted us to do more than just reading books and passing the class, thus began the reign of his "experimentially education"... Something I didn't get the picture quite well. He said to us that the academy has two different types of educational purposes: "educational" and "research". The former is about how the academy teach the students concepts, theory, algorithms, etc. The latter is about how the academy lets the students "do" those concepts, theory, etc., and letting them do some researching. And all of this have to do with why he would task us to do this assignment.

In short, I now have a exaggeratedly good reason for me to program on a Sunday weekend with a bad mood. Thanks for some tips, and I'll seek to it with my Google skills.
ajaytemp_55190
ajaytemp_55190

Seconding the forkbomb. I wrote up a very simple forkbomb just now that completely locked up the computer in less than five minutes. Only took 5 lines of code. It didn't suck up all the CPU, but it sure ate up the RAM quick enough...



[color=#1C2837][size=2]"and then try multithreading to achieve a busy 100% stable CPU"
[color="#1c2837"][size=2]

[color="#1c2837"][size=2]less threadlike would have more busier CPU.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.