3

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?
1
  • 1
    If you have trouble opening certain processes you should obtain the SeDebugPrivilege (requires that your program runs as admin). Commented Oct 24, 2011 at 20:02

3 Answers 3

5

Here is a power shell script that does it. You can also run it with additional bat file if needed. Then in task manager manualy set affinity of your benchmarks.

run_set_affinity.bat:

powershell -executionpolicy bypass -file set_affinity.ps1 

set_affinity.ps1:

# elevate privileges if we are not running as Administrator, so we can set affinity of Windows owned processes
# source: http://superuser.com/questions/108207/how-to-run-a-powershell-script-as-administrator

param([switch]$Elevated)

function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

if ((Test-Admin) -eq $false)  {
    if ($elevated) {
        'tried to elevate to full privileges, did not work, aborting'
    } else {
        'running my self again with full privileges'
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-executionpolicy bypass -noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }
    exit
}
'running with full privileges'





# set affinity of all processes to CPU 3 and CPU 4
# it prints processes that it was unable to set affinity of    
# source: https://digitaljive.wordpress.com/2011/11/18/set-processor-affinity-with-powershell/

# 1 (CPU 1) 
# 2 (CPU 2) 
# 4 (CPU 3) 
# 8 (CPU 4) 
# 16 (CPU 5) 
# 32 (CPU 6) 
# 64 (CPU 7) 
# 128 (CPU 8)

$affinity = 4 + 8
'setting all processes to affinity: '+$affinity
'processes unable to set affinity of: '

$allProcesses = Get-Process * 
foreach ($process in $allProcesses) { 
    try {
        $process.ProcessorAffinity = $affinity
    }
    catch {
        $process
    }
}
0
3

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.

2
  • 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
    Commented Oct 23, 2011 at 15:39
  • 1
    I think you should open a separate question on that rather than trying to turn a question into a conversation. Commented Oct 23, 2011 at 20:17
1

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

start /affinity 1 java.exe

Tested successfully on Windows 7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.