Two CPU cores are controlled at the same time, showing two curves

Source: Internet
Author: User

The http://www.bkjia.com/kf/201110/107761.html specifies the CPU that can be specified when the thread is running, so that when one CPU is idle, another CPU can be computed while displaying two curves in the task manager, A sine curve and a cosine curve.

Code:

 
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_amplece = 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) * amplian ));
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_ampl--(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 );
}
}
}
}
}

I don't need to worry about the departments that have been commented out. I use it to test other parts. When CPU1 is idle, it does not let the thread sleep, but transfers the current process to CPU2, controls the ratio of busy and idle time of CPU2, and displays the cosine curve on CPU2.

This time, because two CPUs are used at the same time, many services and other software provided by the operating system may have an impact on the effect. Therefore, to adjust the time slice, I will adjust it to 100.

Effect

 

 

From: Watkins. Song

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.