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

Started by
11 comments, last by ajaytemp_55190 12 years, 6 months ago
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. :(
Advertisement
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...?
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.
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.
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.
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 >=]
Facetious answer - easy as pie! Just install Lotus Notes! ;)

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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...
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.

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.

This topic is closed to new replies.

Advertisement