免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10412 | 回复: 6
打印 上一主题 下一主题

LINUX下编写一个控制CPU占用率的程序? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-18 22:05 |只看该作者 |倒序浏览
相信看过编程之美的人都知道,在WINDOWS下编写一个程序来控制CPU的占用率,可控制CPU占用率为一条直线,以及正弦曲线等。
那么在LINUX下怎么能实现呢?
我是按照书上思想来编写的,但是实际中,确不能实现?
/****************************************************************/
/***     文件名:Tank_manager.CPP                              ***/
/***     功能: 控制CPU的占用率                               ***/
/***            输入你想要CPU占用率                            ***/
/***     日期: 2010/05/18                                     ***/
/****************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>

//long GetTickCount()
//{   
//        tms tm;   
//        return times(&tm);
//}


int main()
{
        bool temp = true;
        int busyTime = 0;   //忙的时间
        int idleTime = 0;    //空闲时间
        long timeuse = 0;    //实际循环用的时间
        int cpucoe = 0;      //CPU占用率
        struct timeval tpstart,tpend;
       
        printf( "lease input CPU busyTime:";
        scanf("%d",&busyTime);
        while(temp)
        {       
                if ((busyTime <= 0))
                {
                        printf("Input Error!Please input CPU busyTime:";
                        scanf("%d",&busyTime);
                }
                else
                {
                        temp = false;
                }
        }
       
        temp = true;
        printf( "lease input CPU coefficient(0 to 100):";
        scanf("%d",&cpucoe);
       
        while(temp)
        {       
                if ((cpucoe > 100) || (cpucoe <= 0))
                {
                        printf("Input Error!Please input CPU coefficient(0 to 100):";
                        scanf("%d",&cpucoe);
                }
                else
                {
                        temp = false;
                }
        }
       
        printf("If you want to interrputctrl + c)\n";       
        while (1)
        {
                gettimeofday(&tpstart,NULL); //得到当前的系统时间
                while (timeuse <= busyTime)
                {
                        gettimeofday(&tpend,NULL);
                        timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);  
                        timeuse /= 1000;               //转换成ms               
                }
                                       
                idleTime = ((100 * busyTime) / cpucoe) - busyTime;
               
                sleep(idleTime / 1000);    //转化成ms
        }
       
        return 0;       
}
/************************End of file********************************/

在这里我们一般输入忙的时间为20ms.
在测试中我,设置占用率为50。
通过top命令来查看。
这个程序的使用率一直在98%左右,而对应的CPU使用率为43%左右,如果我把其占用率设置成80%,对应的CPU使用率基本上和前面的一样,为什么呢?
是不是在LINUX下这种实现方法不行?
还有没有其他的好方法?
希望大家能帮帮忙啊

论坛徽章:
0
2 [报告]
发表于 2010-05-18 22:31 |只看该作者
没人顶我啊,自己先占下沙发。

论坛徽章:
0
3 [报告]
发表于 2010-05-18 22:41 |只看该作者
用[code][code]来公开嘛,这样看起来真别扭

论坛徽章:
0
4 [报告]
发表于 2010-05-18 23:16 |只看该作者
[code]
/****************************************************************/
/***     文件名:Tank_manager.CPP                              ***/
/***     功能: 控制CPU的占用率                               ***/
/***            输入你想要CPU占用率                            ***/
/***     日期: 2010/05/18                                     ***/
/****************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>

//long GetTickCount()
//{   
//        tms tm;   
//        return times(&tm);
//}


int main()
{
        bool temp = true;
        int busyTime = 0;   //忙的时间
        int idleTime = 0;    //空闲时间
        long timeuse = 0;    //实际循环用的时间
        int cpucoe = 0;      //CPU占用率
        struct timeval tpstart,tpend;
        
        printf( "lease input CPU busyTime:";
        scanf("%d",&busyTime);
        while(temp)
        {        
                if ((busyTime <= 0))
                {
                        printf("Input Error!Please input CPU busyTime:";
                        scanf("%d",&busyTime);
                }
                else
                {
                        temp = false;
                }
        }
        
        temp = true;
        printf( "lease input CPU coefficient(0 to 100):";
        scanf("%d",&cpucoe);
        
        while(temp)
        {        
                if ((cpucoe > 100) || (cpucoe <= 0))
                {
                        printf("Input Error!Please input CPU coefficient(0 to 100):";
                        scanf("%d",&cpucoe);
                }
                else
                {
                        temp = false;
                }
        }
        
        printf("If you want to interrputctrl + c)\n";        
        while (1)
        {
                gettimeofday(&tpstart,NULL); //得到当前的系统时间
                while (timeuse <= busyTime)
                {
                        gettimeofday(&tpend,NULL);
                        timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);  
                        timeuse /= 1000;               //转换成ms               
                }
                                       
                idleTime = ((100 * busyTime) / cpucoe) - busyTime;
               
                sleep(idleTime / 1000);    //转化成ms
        }
        
        return 0;        
}
/************************End of file********************************/
[code]

论坛徽章:
0
5 [报告]
发表于 2010-05-18 23:19 |只看该作者
是[code][/code]

论坛徽章:
0
6 [报告]
发表于 2010-05-19 09:10 |只看该作者
--------------------------------------------------------------------------------

  1. /****************************************************************/
  2. /***     文件名:Tank_manager.CPP                              ***/
  3. /***     功能: 控制CPU的占用率                               ***/
  4. /***            输入你想要CPU占用率                            ***/
  5. /***     日期: 2010/05/18                                     ***/
  6. /****************************************************************/

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11. #include <time.h>

  12. //long GetTickCount()
  13. //{   
  14. //        tms tm;   
  15. //        return times(&tm);
  16. //}


  17. int main()
  18. {
  19.         bool temp = true;
  20.         int busyTime = 0;   //忙的时间
  21.         int idleTime = 0;    //空闲时间
  22.         long timeuse = 0;    //实际循环用的时间
  23.         int cpucoe = 0;      //CPU占用率
  24.         struct timeval tpstart,tpend;
  25.         
  26.         printf( "lease input CPU busyTime:";
  27.         scanf("%d",&busyTime);
  28.         while(temp)
  29.         {        
  30.                 if ((busyTime <= 0))
  31.                 {
  32.                         printf("Input Error!Please input CPU busyTime:";
  33.                         scanf("%d",&busyTime);
  34.                 }
  35.                 else
  36.                 {
  37.                         temp = false;
  38.                 }
  39.         }
  40.         
  41.         temp = true;
  42.         printf( "lease input CPU coefficient(0 to 100):";
  43.         scanf("%d",&cpucoe);
  44.         
  45.         while(temp)
  46.         {        
  47.                 if ((cpucoe > 100) || (cpucoe <= 0))
  48.                 {
  49.                         printf("Input Error!Please input CPU coefficient(0 to 100):";
  50.                         scanf("%d",&cpucoe);
  51.                 }
  52.                 else
  53.                 {
  54.                         temp = false;
  55.                 }
  56.         }
  57.         
  58.         printf("If you want to interrputctrl + c)\n";        
  59.         while (1)
  60.         {
  61.                 gettimeofday(&tpstart,NULL); //得到当前的系统时间
  62.                 while (timeuse <= busyTime)
  63.                 {
  64.                         gettimeofday(&tpend,NULL);
  65.                         timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);  
  66.                         timeuse /= 1000;               //转换成ms               
  67.                 }
  68.                                        
  69.                 idleTime = ((100 * busyTime) / cpucoe) - busyTime;
  70.                
  71.                 sleep(idleTime / 1000);    //转化成ms
  72.         }
  73.         
  74.         return 0;        
  75. }
  76. /************************End of file********************************/
复制代码

论坛徽章:
0
7 [报告]
发表于 2010-05-19 09:10 |只看该作者
谢谢上面的提醒
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP