让CPU占用率曲线听你指挥

时间:2023-03-08 23:19:47
让CPU占用率曲线听你指挥

使用GetTickCount()和Sleep():

让CPU占用率曲线听你指挥让CPU占用率曲线听你指挥Code#include <stdio.h>
#include <unistd.h>
#include <time.h> unsigned long GetTickCount()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC,&ts); //LOCK_REALTIME, CLOCK_MONOTONIC
return (ts.tv_sec*1000 + ts.tv_nsec/(1000*1000));
} int main( int argc,char* argv[])
{
//50%
int busyTime=10;
int idleTime=10;
unsigned long startTime = 0; while(true)
{
startTime=GetTickCount();
while((GetTickCount()-startTime)<=busyTime)
{
;
}
usleep(idleTime * 1000);
}
return 0;
}

参考链接:http://blog.csdn.net/wesweeky/article/details/6402564