Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set CPU affinity of a program?

I have a program written in C#, I am using VSTS 2008 + .Net 3.5 + Windows Vista Enterprise x86 to develop a Windows Forms application.

My current computer is dual-core CPU, I want to set CPU affinity of my program to run on a specific CPU and free another CPU to do some other job. Any ideas how to do this? Either through coding or configuration is ok.

A little more background is, my program is CPU intensive, so I do not want to let it occupy all two CPU resources on my computer and I want to free one CPU so that I can browse network at the same time quickly. :-)

thanks in advance, George

like image 257
George2 Avatar asked Jun 27 '09 07:06

George2


2 Answers

  1. Go to Task Manager -> Processes tab.
  2. Look for your program. Right click on it.
  3. Select Set Affinity and uncheck one of the checkboxes.

This should free up one processor for you.

For doing it from the code you can add this statement:

System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr) 1;

Cheers!

like image 73
Gaurav Avatar answered Sep 28 '22 18:09

Gaurav


The Windows API functions to do this are SetProcessAffinityMask() and SetThreadAffinityMask(). I don't know .NET so I can't say whether there are wrappers around those functions, but this seems to suggest otherwise.

BTW: I agree that these are necessary only in very specific circumstances, it's normally best to let the OS scheduler deal with it. It's one of those questions where you probably shouldn't do it if you have to ask how.

like image 30
mghie Avatar answered Sep 28 '22 18:09

mghie