Write Program Control cpu usage-4 drawing the sine curve of CPU usage 2 using C # specifying the CPU running by the thread

Source: Internet
Author: User

Http://www.bkjia.com/kf/201110/107760.html.

 

You can use C # To specify the CPU on which the current thread runs.

 

Pass

 
System. Diagnostics. Process p = Process. GetCurrentProcess ();
P. ProcessorAffinity = (IntPtr) 0x0001;

 

Process. ProcessorAffinity: sets the shielding word for the current CPU. 0x0001 indicates that CPU 1 is selected, and 0x0002 indicates that CPU 2 is used.

 

The Code is as follows:

 
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Diagnostics;
 
Namespace cpu_4_2_csharp
{
Class Program
{
Static void Main (string [] args)
{
System. Diagnostics. Process p = Process. GetCurrentProcess ();
P. ProcessorAffinity = (IntPtr) 0x0001;
 
Const int SAMPLING_COUNT = 200;
Const double PI = 3.14159;
Const int total_amplece = 250; // the length of each time piece
// Const int system_busy = 10; // take the system cpu consume into consideration
 
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)
{
StartTick = Environment. TickCount;
While (Environment. TickCount-startTick) <busySpan [j])
{
//
}
System. Threading. Thread. Sleep (total_ampl--(int) busySpan [j]);
}
}
}
}

Program running result

 

 

 

You can adjust the parameters to make the curve more round, but after debugging for a long time, I got the above. However, the curve graph is much better than the curve graph in http://www.bkjia.com/kf/201110/107760.html.

 

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.