Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

batch file - Change affinity of process with windows script

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...