views:

60

answers:

2

The start Windows shell command (in cmd.exe) appears to be able to start a process with a chosen processor affinity, rather than starting the process then setting the chosen processor affinity some (small) period of time after it has started.

start /affinity 2 something.exe

Is there a way to do the same from within a .NET application either with or without using P-Invoke? Calling out to cmd.exe /C start ... doesn't count.

The Process class has the ProcessorAffinity property, but it throws InvalidOperationException if you try to set it before having called the Start method.

+4  A: 

I suspect what the start command does in this case is start the process suspended (i.e., passes CREATE_SUSPENDED when calling CreateProcess), then sets the processor affinity, then resumes the thread.

This way, while the process exists without the process affinity having been set, by the time it starts executing the processor affinity has been set, so it only ever runs on the designated processor.

Jerry Coffin
+1  A: 

I do not think that you can set processor affinity before starting the process because the first parameter of SetProcessAffinityMask Function is handle to the process.

On the other hand, there is a program called Imagecfg which allows to permanently set processor affinity for an executable but I am not sure how it works.

Giorgi