计算cpu空闲时间 [英] calculation of cpu idle time

查看:275
本文介绍了计算cpu空闲时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有计算cpu空闲时间的问题,如果空闲时间是
,那么我必须关闭系统。请有人告诉我这个想法



hi
i have problem to calculate idle time of cpu and if idle time is
more then i have to shut down the system. can anyone tell me the idea
to so that please

推荐答案


ra ****** @ gmail.com 写道:



i计算cpu的空闲时间有问题,如果空闲时间是多少b $ b,那么我必须关闭系统。任何人都可以告诉我这个想法

以便请b
hi
i have problem to calculate idle time of cpu and if idle time is
more then i have to shut down the system. can anyone tell me the idea
to so that please



你的意思是如果CPU似乎处于空闲状态,你会把它关闭吗? br />

关闭它不是C本身可以做的事情,但是如果你的C有'b
'系统'

函数,或者如果你可以把运行这个程序的命令放在一个

批处理文件中,你没问题。


只做一个系统(关闭 ;)或者说关闭在批处理文件中,

无论什么命令关闭你的计算机。


现在要弄清楚它是否空闲,这有点棘手。但是可行,如果

你的C有一个函数可以返回当前时间,最好是

小增量。我们的想法是在时间()返回值之间形成间隔

的直方图,例如:


#include< time.h>


#define MaxMsec 1000

unsigned long int Histo [MaxMsec];

int i,j; time_t现在,Prev,Elap,Base;

for(i = 0; i< MaxHisto; i ++)Histo [i] = 0;


上一页= 0; Base = Prev = time();


for(j = 1; j< 100000; j ++){

Now = time() - Base; Elap = Now - Prev; Prev = Now;

If(Elap< 0)fprintf(stderr,Time to backwardwards !! \ n)

else

If(Elap> = MaxMsec)Elap = MaxMsec;

Histo [Elap] ++;

}


....然后扫描直方图。如果CPU非常空闲,您应该在非常低或零毫秒处看到一个

大峰值。如果有其他重要的任务在运行,你会看到其他峰值,通常在5到50美元/ b $ b毫秒范围内。 (许多OS的调度算法将时间分配为5到

50毫秒)。


可以肯定的是,绕过该代码循环它运行了几秒钟。


是的,不保证可以在任何地方工作,但似乎在

Windows和Linux上工作。

You mean if the CPU seems to be idle, you shut it down?

Shutting it down isnt something C can do by itself, but if your C has
the "system"
function, or if you can put the command that runs this program in a
batch file, you''re okay.

Just do a system( "shutdown") or put "shutdown" in the batch file,
whatever command shuts down your computer.

Now to figure out if it''s idle, that''s a bit tricky. But doable, if
your C has a function that returns you the current time, preferably in
small increments. The idea is to form a histogram of the intervals
between the values time() returns, something like:

#include <time.h>

#define MaxMsec 1000

unsigned long int Histo[ MaxMsec ];
int i, j; time_t Now, Prev, Elap, Base;
for( i = 0; i < MaxHisto; i++ ) Histo[i] = 0;

Prev = 0; Base = Prev = time();

for( j = 1; j < 100000; j++ ) {
Now = time() - Base; Elap = Now - Prev; Prev = Now;
If( Elap < 0 ) fprintf( stderr, "Time going backwards!!\n" )
else
If( Elap >= MaxMsec ) Elap = MaxMsec;
Histo[ Elap ] ++;
}

.... then scan the histogram. If the CPU is very idle, you should see a
big peak at very low or zero msec. If there are significant other
tasks running, you''ll see other peaks, usually in the range of 5 to 50
msec. (Many OS''s scheduling algorithms allocate time in slices of 5 to
50 msec).

To be sure, put a loop around that code so it runs for several seconds.

Yes, not guaranteed to work anywhere, but seems to do the job on
Windows and Linux.





Ancient_Hacker在08/23/06 11:45写道:


Ancient_Hacker wrote On 08/23/06 11:45,:

[...]


现在要弄清楚它是否闲置,这有点棘手。但是可行,如果

你的C有一个函数可以返回当前时间,最好是

小增量。我们的想法是在时间()返回值之间形成间隔

的直方图,例如:

[Millikan油滴实验]
[...]

Now to figure out if it''s idle, that''s a bit tricky. But doable, if
your C has a function that returns you the current time, preferably in
small increments. The idea is to form a histogram of the intervals
between the values time() returns, something like:
[the Millikan oil-drop experiment]



这是一个糟糕的建议,编码严重。


-
Er ********* @sun.com

This is terrible advice, and badly coded.

--
Er*********@sun.com




Eric Sosman写道:

Eric Sosman wrote:

Ancient_Hacker写于08/23/06 11:45,:
Ancient_Hacker wrote On 08/23/06 11:45,:

[...]


现在要弄清楚它是否闲置,这有点棘手。但是可行,如果

你的C有一个函数可以返回当前时间,最好是

小增量。我们的想法是在时间()返回值之间形成间隔

的直方图,例如:

[Millikan油滴实验]
[...]

Now to figure out if it''s idle, that''s a bit tricky. But doable, if
your C has a function that returns you the current time, preferably in
small increments. The idea is to form a histogram of the intervals
between the values time() returns, something like:
[the Millikan oil-drop experiment]



这是一个糟糕的建议,而且编码很糟糕。


-
Er ********* @ sun.com



非常感谢您的意见。


如果符合以下条件,您的意见会更加重要:


(1)您有一个可行的选择。

(2你解释了为什么这是严重的原因。编码。我看到一个蠢 - 我们没有检测到正确倒退的时间。否则有一些方法可以做一个简单的直方图。请随时教育我。

Thank you so much for your opinion.

Your opinion would have more weight if:

(1) You had a viable alternative.
(2) You explained why this is "badly" coded. I see one goof-- we''re
not detecting time going backwards correctly. Otherwise there are darn
few ways to do a simple histogram. Please feel free to educate me.


这篇关于计算cpu空闲时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆