Search code examples
windows-7affinity

How can I programatically set the affinity of all running processes in WIndows 7?


Is there any way to set the affinity for all running processes in Windows 7?

I want to run some hyperthreading benchmarks, and I want to ensure that they're running alone on a certain core. I'm running them with a wrapper, and I want to do something like this (in pseudocode):

foreach process in <list of all processes>
    set affinity to all cores but core x

set affinity of the current process to core x

run benchmark 0 on core x thread 0
run benchmark 1 on core x thread 1 

Now, I think that I know how to set the affinity of the current process and its children, but how can I:

  1. iterate over all processes?
  2. set the affinity of other processes?

Solution

  • This project on CodeProject shows how to enumerate all processes and change their priority. A one-line change will adjust it so it enumerates all processes and changes their affinity. Just change SetProcessPriority to SetProcessAffinityMask.