Change the affinity of the process with windows script

On Windows with

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

I can establish the proximity of app.exe (the number of kernels used by app.exe).

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

+9
windows batch-file affinity
source share
7 answers

PowerShell can complete this task for you.

Get affinity:

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

Set proximity:

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

Example: (8-processor 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 = 10,000,000

Just add decimal values โ€‹โ€‹together for which kernel you want to use. 255 = All 8 cores.

  • All Cores = 255 = 11111111

Result:

 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:\> 

A source:

Here is a detailed report on how to change the affinity for a process: http://www.energizedtech.com/2010/07/powershell-setting-processor-a.html

+13
source share

The accepted answer works, but only for the first process in the list. Solving this in the comments does not work for me.

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

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

Where 255 is the mask indicated in the accepted answer.

+3
source share

For those who are looking for answers to this and will not find, the solution I found is to use the WinAFC application (or AffinityChanger). This is a partial graphical interface, a partial command line application that allows you to specify profiles for specific executable files and will view the list processes for them. If he finds the corresponding processes, he will change the affinity of these processes according to the settings of the loaded profile.

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

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

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

This profile installs any process convert.exe for using the first two pairs of core CPUs (CPU0, CPU1, CPU2 and CPU3), polling every second. TestMode is a switch that lets you see if your profile works without actually setting affinity.

Hope someone finds this helpful!

+2
source share
 wmic process where name="some.exe" call setpriority ProcessIDLevel 

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

+1
source share

If you really like enums, you can do it as follows. ProcessorAffinity is IntPtr, so it requires a bit of extra 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' 
0
source share

Jake's answer above about the WinAFC program is very good. I tried this and solved my problem. I needed to control the process that was initiated from another exe file, so that traditional methods could work only with the parent process (exe), and not with the internally invoked one, which was so greedy for the processor that it literally blocked my machine. Therefore, I needed a kind of watchdog timer that intercepts these so-called processes and on the fly checks their compliance, and WinAFC is ideal for this. Thanks Jake!

0
source share

Can someone tell me what I should use with my i5 2500k? :)

-one
source share

All Articles