質問

In Windows, with

 START /node 1 /affinity ff cmd /C "app.exe"

I can set the affinity of app.exe (number of cores used by app.exe).

With a windows script, How I can change the affinity of a running process ?

役に立ちましたか?

解決

PowerShell can do this task for you

Get Affinity:

PowerShell "Get-Process app | Select-Object ProcessorAffinity"

Set Affinity:

PowerShell "$Process = Get-Process app; $Process.ProcessorAffinity=255"

Example: (8 Core Processor)

  • Core # = Value = BitMask
  • Core 1 = 1 = 00000001
  • Core 2 = 2 = 00000010
  • Core 3 = 4 = 00000100
  • Core 4 = 8 = 00001000
  • Core 5 = 16 = 00010000
  • Core 6 = 32 = 00100000
  • Core 7 = 64 = 01000000
  • Core 8 = 128 = 10000000

Just add the decimal values together for which core you want to use. 255 = All 8 cores.

  • All Cores = 255 = 11111111

Example Output:

C:\>PowerShell "Get-Process notepad++ | Select-Object ProcessorAffinity"

                                                              ProcessorAffinity
                                                              -----------------
                                                                            255



C:\>PowerShell "$Process = Get-Process notepad++; $Process.ProcessorAffinity=13"

C:\>PowerShell "Get-Process notepad++ | Select-Object ProcessorAffinity"

                                                              ProcessorAffinity
                                                              -----------------
                                                                             13



C:\>PowerShell "$Process = Get-Process notepad++; $Process.ProcessorAffinity=255"

C:\>

Source:

Here is a nicely detailed post on how to change a process's affinity: http://www.energizedtech.com/2010/07/powershell-setting-processor-a.html

他のヒント

The accepted answer works, but only for the first process in the list. The solution to that in the comments does not work for me.

To change affinity of all processes with the same name use this:

Powershell "ForEach($PROCESS in GET-PROCESS processname) { $PROCESS.ProcessorAffinity=255}"

Where 255 is the mask as given in the accepted answer.

For anyone else looking for answers to this and not finding any, the solution I found was to use an app called WinAFC (or AffinityChanger). This is a partial GUI, partial command line app that allows you to specify profiles for certain executables, and will poll the process list for them. If it finds matching processes, it will change the affinity of those processes according to the settings in the loaded profile.

There is some documentation here: http://affinitychanger.sourceforge.net/

For my purposes, I created a profile that looked like this:

TestMode = 0
TimeInterval = 1
*\convert.exe := PAIR0+PAIR1

This profile sets any convert.exe process to use the first two CPU core pairs (CPU0, CPU1, CPU2, and CPU3), polling every second. TestMode is a toggle that allows you to see if your profile is working without actually setting affinities.

Hope someone finds this useful!

If you really like enums, you can do it this way. ProcessorAffinity is an IntPtr, so it takes a little extra type casting.

[flags()] Enum Cores {
  Core1 = 0x0001
  Core2 = 0x0002
  Core3 = 0x0004
  Core4 = 0x0008
  Core5 = 0x0010
  Core6 = 0x0020
  Core7 = 0x0040
  Core8 = 0x0080
}

$a = get-process notepad

[cores][int]$a.Processoraffinity
Core1, Core2, Core3, Core4

$a.ProcessorAffinity = [int][cores]'core1,core2,core3,core4'
wmic process where name="some.exe" call setpriority ProcessIDLevel

I think these are the priority levels .You can also use PID instead of process name.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top