how to set CPU affinity of a program?
Asked Answered
Y

3

10

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

Yarber answered 27/6, 2009 at 7:9 Comment(2)
I've seen a utility to do this for interbase server (ibaffinity I think it was called). I'd be interested in seeing how to do this for any of my own programs too - presumably you can't do it at run time in your executable, you'd have to do it via external configuration or during your installation process (I'd guess).Gregoriogregorius
@robsoft: This was only necessary for old Interbase / Firebird versions. Recent versions tie the server processes to one processor core each. It's open source, so one can always check out how it's done there.Renata
M
13
  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!

Malanie answered 27/6, 2009 at 7:25 Comment(5)
Any programming or configuration ways? I do not want end user to do this each time. :-)Yarber
Try this: System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = 1;Malanie
BTW: To use second set it to 2 and to use both cores you can set the value to 3 :)Malanie
System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)1; forgot the typecasting laste time :PMalanie
@Gaurav, why use IntPrt, why not use int?Yarber
R
7

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.

Renata answered 27/6, 2009 at 7:30 Comment(3)
@mghie, in my problem, 1. I think I should call SetProcessAffinityMask other than SetThreadAffinityMask, correct? 2. Could I call SetProcessAffinityMask from the current process to set the affinity of the current process? Or I have to set it up from external tool?Yarber
You can do it from the process itself. It's probably best to start with this MSDN article: msdn.microsoft.com/en-us/magazine/cc300701.aspx#S11, it should provide some code as well as helpful background information.Renata
I like your reply, about using System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity property, why should we use IntPrt, why not use int?Yarber
W
3

Actually, your application will not use more than one CPU unless you specifically do something to utilize more CPUs. If you use the thread pool and/or start additional threads you may use additional available cores, but otherwise your application will just have one thread per default and thus only use one CPU.

Wilhelmina answered 27/6, 2009 at 7:14 Comment(5)
My application will use more than one CPU because it recording through Windows Media Encoder and do real time encoding to wmv format -- I have tested it and seen all CPU resources are occupied from Windows task manager. Any ideas how to set CPU affinity?Yarber
@Brian: It may still make sense to force a thread to remain on one core, for those systems where the cache is per-core.Renata
@mghie, I agree with you. Any ideas for my problem?Yarber
@George2: Well, see my answer, read the MSDN documentation how to use the functions, and call them using platform invoke.Renata
Cool, @mghie, please see my reply, I like your answer.Yarber

© 2022 - 2024 — McMap. All rights reserved.