3

Windows 7 で実行中のすべてのプロセスにアフィニティを設定する方法はありますか?

いくつかのハイパースレッディング ベンチマークを実行したいのですが、それらが特定のコアで単独で実行されていることを確認したいと考えています。私はラッパーでそれらを実行していますが、次のようなことをしたいです(疑似コードで):

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 

これで、現在のプロセスとその子プロセスのアフィニティを設定する方法はわかったと思いますが、どうすれば次のことができますか:

  1. すべてのプロセスを反復しますか?
  2. 他のプロセスのアフィニティを設定しますか?
4

3 に答える 3

4

これは、それを行うパワーシェルスクリプトです。必要に応じて、追加のバット ファイルを使用して実行することもできます。次に、タスク マネージャーで手動でベンチマークのアフィニティを設定します。

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
    }
}
于 2016-02-03T17:41:46.150 に答える
3

CodeProject のこのプロジェクトは、すべてのプロセスを列挙し、それらの優先度を変更する方法を示しています。1 行の変更で調整され、すべてのプロセスが列挙され、それらのアフィニティが変更されます。に変更SetProcessPriorityするだけSetProcessAffinityMaskです。

于 2011-10-23T15:21:08.250 に答える
0

次の構文を使用して、起動時に実行するようにスケジュールされたタスクトリガーをセットアップします。

start /affinity 1 java.exe

Windows 7 で正常にテストされました。

于 2012-08-02T17:39:27.990 に答える