程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 同時控制CPU的兩個內核,顯示兩個曲線

同時控制CPU的兩個內核,顯示兩個曲線

編輯:C#入門知識

http://www.BkJia.com/kf/201110/107761.html 中指出可以指定線程的運行時指定的CPU,這樣,在一個CPU空閒的時候,可以讓另外一個CPU進行運算, 同時在任務管理器中顯示兩個曲線,一個正弦曲線,一個余弦曲線。

代碼:

 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using System.Threading; 
 
namespace cpu_4_3 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            const int SAMPLING_COUNT = 200; 
            const double PI = 3.14159; 
            const int TOTAL_AMPLITUDE = 100; // the length of each time piece 
 
            const float CPU2_BUSY_TIME_LOW = 0.3F; 
            const float CPU2_BUSY_TIME_HIGH = 0.7F; 
            //const int system_busy = 10; // take the system cpu consume into consideration 
 
            Process p = Process.GetCurrentProcess(); 
            p.ProcessorAffinity = (IntPtr)0x0001; 
 
            double[] busySpan = new double[SAMPLING_COUNT]; 
            int amplitude = (TOTAL_AMPLITUDE) / 2; 
            double radian = 0.0; 
            double radianIncreament = 2.0 / (double)SAMPLING_COUNT; 
 
            for (int i = 0; i < SAMPLING_COUNT; i++) 
            { 
                busySpan[i] = ((double)(amplitude + Math.Sin(PI * radian) * amplitude)); 
                radian += radianIncreament; 
            } 
 
            int startTick = Environment.TickCount; 
            for (int j = 0; ; j = (j + 1) % SAMPLING_COUNT) 
            { 
                p.ProcessorAffinity = (IntPtr)0x0001; 
                startTick = Environment.TickCount; 
                while ((Environment.TickCount - startTick) < busySpan[j]) 
                { 
                    // 
                } 
                p.ProcessorAffinity=(IntPtr)0x0002; // during the process 1 idle time, change to processor 2. 
                startTick = Environment.TickCount; 
                // cycle in the processor 2 time. 
                while ((Environment.TickCount - startTick) < ((TOTAL_AMPLITUDE-(int)busySpan[j]))) 
                { 
 
                    //int cpu2_sleep_time; 
                    //// judge from j, verify the period of the process 2. 
                    //if (j <= 125) 
                    //{ 
                    //    // in this part, the cpu rate is controlled to 30% 
                    //    int cpu2_time = 250 - (int)busySpan[j]; 
                    //    int cpu2_time_busy_low = (int)(cpu2_time * CPU2_BUSY_TIME_LOW); 
                    //    cpu2_sleep_time = cpu2_time - cpu2_time_busy_low; 
                    //    while ((Environment.TickCount - startTick) < cpu2_time_busy_low) 
                    //    { } 
                    //} 
                    //else 
                    //{ 
                    //    // in this part, the cpu rate is controlled to 70% 
                    //    int cpu2_time = 250 - (int)busySpan[j]; 
                    //    int cpu2_time_busy_high = (int)(cpu2_time * CPU2_BUSY_TIME_HIGH); 
                    //    cpu2_sleep_time = cpu2_time - cpu2_time_busy_high; 
                    //    while ((Environment.TickCount - startTick) < cpu2_time_busy_high) 
                    //    { } 
                    //} 
                    //System.Threading.Thread.Sleep(cpu2_sleep_time); 
                } 
            } 
        } 
    } 

注釋掉的部門不用管,我用來測試其他的部分的。在CPU1空閒的時候,不讓線程sleep,而是將當前進程轉移到CPU2上,控制CPU2的繁忙和空閒的時間比例,同時也在CPU2上顯示余弦曲線。

這次因為是同時使用兩個CPU,可能操作系統自帶的很多服務和其他軟件會對效果產生影響,所以要調整時間片,我調到100。

效果如圖:

 

 \

摘自:Watkins.Song

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved