开发者

How to write a program, which uses 30% of CPU? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help 开发者_JS百科center. Closed 11 years ago.

This is an interview question: write a program, which uses 30% CPU? How would you write such a program?


maybe this is not what you expected, but try:

#include <unistd.h>
#include <stdio.h>
#include <time.h>
#define IMAX 999999
#define SLEEP 9999

int main( int argc, char *argv[] ) {
    long i;
    for(;;) {
        for(i=0; i<IMAX; i++) {}
        usleep(SLEEP);
    }
}

experiment with the SLEEP length. This one gave 25% load on my notebook. IMAX 999 gave 76%. without sleep = 100%.


This works on my laptop

Private Sub Form1_Shown(sender As Object, _
                        e As System.EventArgs) Handles Me.Shown
    Dim numProc As Integer = Environment.ProcessorCount
    For x As Integer = 1 To numProc
        Dim t As New Threading.Thread(AddressOf foo)
        t.IsBackground = True
        t.Start()
    Next
End Sub

Private Sub foo()
    Const usage As Double = 0.35 '35%
    Dim sleep As Integer = CInt((1 - usage) * 100)
    Dim stpw As New Stopwatch
    Do
        stpw.Reset()
        stpw.Start()
        Do

        Loop While stpw.ElapsedMilliseconds < 100 - sleep
        Threading.Thread.Sleep(sleep)
    Loop
End Sub

I do wonder, as others have, about the intent of the question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜