Take the 2-minute tour ×

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?
share|improve this question
If you have trouble opening certain processes you should obtain the SeDebugPrivilege (requires that your program runs as admin). –  CodesInChaos Oct 24 '11 at 20:02

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
I suspect that he will have to run as a service to be able to set the affinity mask for system processes and services. And even then he will almost certainly not be able to set the affinity of kernel threads. –  Gabe Oct 23 '11 at 15:39
One impossible thing at a time... –  Raymond Chen Oct 23 '11 at 16:01
Where can I learn about running something as a service? –  Nathan Fellman Oct 23 '11 at 20:13
I think you should open a separate question on that rather than trying to turn a question into a conversation. –  Raymond Chen Oct 23 '11 at 20:17

I setup a Scheduled Task Trigger to run at startup with the following syntax:

start /affinity 1 java.exe

Tested successfully on Windows 7.

share|improve this answer

Your Answer


