zoukankan      html  css  js  c++  java
  • [趣味编程]CPU占用率曲线听我指挥

    写一个程序,让用户来决定Windows任务管理器(Task Manager)的CPU占用率。程序越精简越好,计算机语言不限。例如,可以实现下面三种情况:

    1.    CPU的占用率固定在50%,为一条直线;

    2.    CPU的占用率为一条直线,但是具体占用率由命令行参数决定(参数范围1~ 100);

    3.    CPU的占用率状态是一个正弦曲线。
    Google上搜索,可以找到第一题和第3题的C++描述。本人写了个C#版本的,主要用性能计数器
    第一题代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Threading;

    namespace cpuControl
    {
        
    public partial class Form1 : Form
        
    {
            
    public Form1()
            
    {
                InitializeComponent();
            }


            
    private void btnOk_Click(object sender, EventArgs e)
            
    {

                
    if (float.TryParse(tbNum.Text, out num))
                
    {
                    Thread thread 
    = new Thread(new ThreadStart(Start));
                    thread.Start();
                }

                
    else
                
    {
                    MessageBox.Show(
    "Please enter legal num");
                }
     
                
            }

            
    private void Start()
            
    {
                 
    try
                    
    {
                        stop 
    = false;
                        PerformanceCounter cpuCounter 
    = new PerformanceCounter("Processor""% Processor Time""_Total");
                                            
    while (!stop)
                        
    {
                            
    if (cpuCounter.NextValue() > num)
                            
    {
                              
    //15为时间片轮转时间   
                             Thread.Sleep(15);
                            }

                        }

                    }

                    
    catch (Exception ex)
                    
    {
                        MessageBox.Show(ex.ToString());
                    }

            }

            
    float num = 0.0f;
            
    bool stop;

            
    private void btnStop_Click(object sender, EventArgs e)
            
    {
                stop 
    = true;
            }

        }

    }


    关于第2题,本人只实现了把CPU曲线控制在11%,13%,15%,18%,21%,25%,32%,47%,50%,100%这些直线上。用的还是性能计数器。不同点在于Start函数里把sleep的时间设置为变量(15的倍数)
     private void Start()
            
    {
                 
    try
                    
    {
                        stop 
    = false;
                        PerformanceCounter cpuCounter 
    = new PerformanceCounter("Processor""% Processor Time""_Total");
                        
    int temp = (int)num / 15;
                        temp 
    = temp * 15;
                        
    while (!stop)
                        
    {
                            
    if (cpuCounter.NextValue() > 5)
                            
    {
                                Thread.Sleep(temp);
                            }

                        }

                    }

                    
    catch (Exception ex)
                    
    {
                        MessageBox.Show(ex.ToString());
                    }

            }

    具体的控制在任何百分率直线还不能实现,希望哪为朋友有兴趣可以指点下本人!
    第3题还是把人家的C++代码写出来好了。有兴趣的朋友可以转成C#版本的。
    #include <math.h>
    #include 
    <stdlib.h>
    #include 
    <windows.h>
    const double SPLIT=0.01;//windows调度的时间片大概是这个时间 
    const int COUNT=200;
    const double PI=3.14159265;
    const int INTERVAL =300;//控制图像的跨度 
    int main(int argc, char *argv[])
    {
      DWORD busySpan[COUNT];
      DWORD idleSpan[COUNT];
      
    int half =INTERVAL/2;
      
    double radian =0.0;
      
    int i;
      
    for(i=0;i<COUNT;i++)
      
    {
           busySpan[i]
    =(DWORD)(half+(sin(PI*radian)*half));//函数图像上升的部分的跨度 
           idleSpan[i]=INTERVAL-busySpan[i];//下降部分的 
           radian+=SPLIT;//弧度增加 
           
      }

      DWORD startTime
    =0;
      
    int j=0;
      
    //这段原理和上一个程序一样的 
      while(1)
      
    {
         j
    =j%COUNT;
         startTime 
    = GetTickCount();
         
    while((GetTickCount()-startTime)<=busySpan[j])
            ;
         Sleep(idleSpan[j]);
         j
    ++;
                
      }

      

      
    return 0;
    }

    SIGNATRUE-----------------------------------
    龟看上去很慢很慢,而且还有些憨,虽然没有兔子跑的快,但是只要有坚持不懈的毅力,就一定会到达成功的比彼岸.如果自己是龟,就不要试图把自己变成兔子,我就是那只憨龟。
  • 相关阅读:
    Linux下C语言执行shell命令
    netstat实现原理
    安装docker
    springboot启动提示连接mysql报错:java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required
    linux删除用户报错:userdel: user prize is currently used by process 28021
    centos的6.9版本安装openjdk1.8
    centos的6.9版本安装mysql
    安装mysql报错:Can't find messagefile '/usr/share/mysql/english/errmsg.sys'和/usr/bin/mysqladmin: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or
    安装docker报错:https://download.docker.com/linux/centos/7/i386/stable/repodata/repomd.xml: [Errno 14] PYCURL ERROR 22
    linux配置docker报错:ImportError: No module named yum
  • 原文地址:https://www.cnblogs.com/seek/p/1207145.html
Copyright © 2011-2022 走看看