亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 10461 | 回復(fù): 6
打印 上一主題 下一主題

LINUX下編寫(xiě)一個(gè)控制CPU占用率的程序? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2010-05-18 22:05 |只看該作者 |倒序?yàn)g覽
相信看過(guò)編程之美的人都知道,在WINDOWS下編寫(xiě)一個(gè)程序來(lái)控制CPU的占用率,可控制CPU占用率為一條直線,以及正弦曲線等。
那么在LINUX下怎么能實(shí)現(xiàn)呢?
我是按照書(shū)上思想來(lái)編寫(xiě)的,但是實(shí)際中,確不能實(shí)現(xiàn)?
/****************************************************************/
/***     文件名: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;   //忙的時(shí)間
        int idleTime = 0;    //空閑時(shí)間
        long timeuse = 0;    //實(shí)際循環(huán)用的時(shí)間
        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); //得到當(dāng)前的系統(tǒng)時(shí)間
                while (timeuse <= busyTime)
                {
                        gettimeofday(&tpend,NULL);
                        timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);  
                        timeuse /= 1000;               //轉(zhuǎn)換成ms               
                }
                                       
                idleTime = ((100 * busyTime) / cpucoe) - busyTime;
               
                sleep(idleTime / 1000);    //轉(zhuǎn)化成ms
        }
       
        return 0;       
}
/************************End of file********************************/

在這里我們一般輸入忙的時(shí)間為20ms.
在測(cè)試中我,設(shè)置占用率為50。
通過(guò)top命令來(lái)查看。
這個(gè)程序的使用率一直在98%左右,而對(duì)應(yīng)的CPU使用率為43%左右,如果我把其占用率設(shè)置成80%,對(duì)應(yīng)的CPU使用率基本上和前面的一樣,為什么呢?
是不是在LINUX下這種實(shí)現(xiàn)方法不行?
還有沒(méi)有其他的好方法?
希望大家能幫幫忙啊

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2010-05-18 22:31 |只看該作者
沒(méi)人頂我啊,自己先占下沙發(fā)。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2010-05-18 22:41 |只看該作者
用[code][code]來(lái)公開(kāi)嘛,這樣看起來(lái)真別扭

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 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;   //忙的時(shí)間
        int idleTime = 0;    //空閑時(shí)間
        long timeuse = 0;    //實(shí)際循環(huán)用的時(shí)間
        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); //得到當(dāng)前的系統(tǒng)時(shí)間
                while (timeuse <= busyTime)
                {
                        gettimeofday(&tpend,NULL);
                        timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_usec - tpstart.tv_usec);  
                        timeuse /= 1000;               //轉(zhuǎn)換成ms               
                }
                                       
                idleTime = ((100 * busyTime) / cpucoe) - busyTime;
               
                sleep(idleTime / 1000);    //轉(zhuǎn)化成ms
        }
        
        return 0;        
}
/************************End of file********************************/
[code]

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2010-05-18 23:19 |只看該作者
是[code][/code]

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 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;   //忙的時(shí)間
  21.         int idleTime = 0;    //空閑時(shí)間
  22.         long timeuse = 0;    //實(shí)際循環(huán)用的時(shí)間
  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); //得到當(dāng)前的系統(tǒng)時(shí)間
  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;               //轉(zhuǎn)換成ms               
  67.                 }
  68.                                        
  69.                 idleTime = ((100 * busyTime) / cpucoe) - busyTime;
  70.                
  71.                 sleep(idleTime / 1000);    //轉(zhuǎn)化成ms
  72.         }
  73.         
  74.         return 0;        
  75. }
  76. /************************End of file********************************/
復(fù)制代碼

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2010-05-19 09:10 |只看該作者
謝謝上面的提醒
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP