Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Interview Question, Please Help

Hi Experts,
Can you please tell me how can I write some code which when run occupies around 50% of the CPU, that is when the program is run the CPU usage of the machine should hover around 50%.
[197 byte] By [it career] at [2007-11-11 11:56:37]
# 1 Re: Interview Question, Please Help
It's a very difficult issue. Controlling the exact amount of CPU usage is nearly impossible with a non real-time OS because you can never predict how much CPU time is allocated at any given moment to the OS tasks and services which are completely independent of your process. You can only approximate it and run the process in a controlled environment (all other non-essential tasks are disabled, no I/O, no GUI etc.). Start with a tight loop and then try to slow it down by adding instructions that put the process to sleep for short intervals.
You may also use a wiseguy solution: create a process that constantly polls the CPU usage and launches tasks if the rate is too slow, or puts tasks to sleep if the rate becomes too high.
Danny at 2007-11-11 20:55:37 >
# 2 Re: Interview Question, Please Help
an infinite loop under my dual core processor make the CPU usage equals exactly 50% , and dual core is finaly one only processor ... one only "PU" .

I'm just asking why we may need to do that ?
Amahdy at 2007-11-11 20:56:37 >
# 3 Re: Interview Question, Please Help
I'm just asking why we may need to do that ?Well, the thread title is "Interview Question", so I'm guessing that is why.
Hack at 2007-11-11 20:57:42 >
# 4 Re: Interview Question, Please Help
Of course on a dual core you get 50% with a 100% usage app ... that is how the dual core is represented on the default task manager, so really you are using 100% of one processor and the scale on the display is 100% = 2 processors, so 50% is one full processor.

To answer this, its not trivial. You can first see if you have a multi core processor and if you do, the task becomes easier (fortunately, cores come in even numbers so you can thread out to use only 1/2 the number of processors and you are good to go!). The hard task is doing this on a single core processor -- for that you will want to potentially monitor the cpu usage and how much of it is your process. If your process is using more than half, use the sleep command to trim it down until you are back in the target zone.
jonnin at 2007-11-11 20:58:41 >
# 5 Re: Interview Question, Please Help
Oh, and if you process needs less cpu, you are also good to go, so one option is to trim the fat from the process (if possible) to use less cpu!
jonnin at 2007-11-11 20:59:47 >
# 6 Re: Interview Question, Please Help
The dual core approach could work, but I suspect that it's not going to be that easy. If your process executes a tight loop that engages one of the two processors 100% of the time, it will be using 50% of the CPU, the math is very simple here. Alas, what about the background services and OS tasks that execute at the same time? They will probably be directed to the second processor which means that you could have significant fluctuations -- suffice it to hover a mouse, or open a document with another process, or have a silly JVM GC cleaning up unreferenced memory...
I'm also joining Amahdy's question: what's the point here? I know it's an interview question, and these questions could really be silly sometimes, but what's the big picture here? Real time programming?
Danny at 2007-11-11 21:00:46 >
# 7 Re: Interview Question, Please Help
You can open a new thread that Real Time programming or something related rather than discuss it here because others may find for help in similar issue.

I hope this help.
Peter_APIIT at 2007-11-11 21:01:50 >
# 8 Re: Interview Question, Please Help
You can open a new thread that Real Time programming or something related rather than discuss it here because others may find for help in similar issue.

I hope this help.:confused: I'm not sure what you mean by this.
Hack at 2007-11-11 21:02:43 >
# 9 Re: Interview Question, Please Help
He probably meant that this discussion should be moved to a different RT thread, forum. However, I don't see any reason why this should be done. It's a valid and important issue, and it's certainly pertinent to C++.
Danny at 2007-11-11 21:03:51 >
# 10 Re: Interview Question, Please Help
Ah...I see. Well, I certainly agree that C++ is right where it belongs.
Hack at 2007-11-11 21:04:46 >
# 11 Re: Interview Question, Please Help
how can people ask such question?? This is stupid. What company is that?

if you know all the hardware details that you are going to use and assume that no other processes are running. then you can create a program like that. Or, if that is linux, and you can change the kernel code, then I think you can make sure from the kernal level, you can always make sure at most 50% of CPU resource is using. Remember, CPU is always running when the computer is on. 0% CPU usage only mean that we keep putting no_op instruction into the CPU, so 50% CPU usage mean that we spend 50% of time putting no_op and 50% of time putting other instructions, which I think is very hard for this to happen.
hc167 at 2007-11-11 21:05:55 >
# 12 Re: Interview Question, Please Help
Ah but thats the point of the question, I think. The question is designed to see if the interviewee knows if the task is even possible, and if so, how one might go about it on a couple of different platforms in a general sense, not at the fully implemented level. Or if they know nothing at all about real time programming, this would really put them on the spot.
jonnin at 2007-11-11 21:06:55 >
# 13 Re: Interview Question, Please Help
I think he's on to something: use a tight loop that engages the CPU 100% of the time, and then splice nop instructions into the loop so that every real instruction is followed by nop. That still leaves us with the issue of background services and hardware interrupts, but perhaps the solution isn't meant to be applied to real OSs but to a virtual machine of some sort.
Danny at 2007-11-11 21:07:55 >
# 14 Re: Interview Question, Please Help
Why did you ban it career ? What wrong did he do by asking this question ?
homework help at 2007-11-11 21:08:51 >
# 15 Re: Interview Question, Please Help
Why did you ban it career ? What wrong did he do by asking this question ?
I don't think it would be appropriate to discuss it on the forum. However, please rest assured that he wasn't banned because of asking this question (which is excellent btw) but for completely other reasons. The decision to do that was accepted by all dev-archive moderators.
Danny at 2007-11-11 21:09:54 >