dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Interview Question, Please Help

  1. #1
    Join Date
    Jan 2007
    Posts
    145

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

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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.
    Last edited by Danny; 10-31-2007 at 10:39 AM.
    Danny Kalev

  3. #3
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    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 ?
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by Amahdy
    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.

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    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!

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    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?
    Last edited by Danny; 10-31-2007 at 02:01 PM.
    Danny Kalev

  8. #8
    Join Date
    May 2007
    Posts
    843
    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.

  9. #9
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by Peter_APIIT
    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.

  10. #10
    Join Date
    Nov 2003
    Posts
    4,118
    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 Kalev

  11. #11
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Ah...I see. Well, I certainly agree that C++ is right where it belongs.

  12. #12
    Join Date
    Jan 2007
    Posts
    10
    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.

  13. #13
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  14. #14
    Join Date
    Nov 2003
    Posts
    4,118
    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 Kalev

  15. #15
    Join Date
    Nov 2007
    Posts
    1
    Why did you ban it career ? What wrong did he do by asking this question ?

Similar Threads

  1. Replies: 0
    Last Post: 12-16-2006, 03:04 AM
  2. Java - J2ee Interview Question and Answers
    By pravin007 in forum Java
    Replies: 2
    Last Post: 07-26-2006, 10:07 PM
  3. tricky interview question
    By drkybelk in forum C++
    Replies: 9
    Last Post: 06-28-2006, 12:36 PM
  4. interview question
    By rainerblue in forum Java
    Replies: 3
    Last Post: 03-25-2006, 06:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center