使用 Windows 脚本更改进程的亲和力
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19187241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change affinity of process with windows script
提问by JuanPablo
In Windows, with
在 Windows 中,使用
START /node 1 /affinity ff cmd /C "app.exe"
I can set the affinity of app.exe (number of cores used by app.exe).
我可以设置 app.exe 的亲和力(app.exe 使用的内核数)。
With a windows script, How I can change the affinity of a running process ?
使用 Windows 脚本,如何更改正在运行的进程的关联?
回答by David Ruhmann
PowerShell can do this task for you
PowerShell 可以为您完成此任务
Get Affinity:
获得亲和力:
PowerShell "Get-Process app | Select-Object ProcessorAffinity"
Set Affinity:
设置亲和力:
PowerShell "$Process = Get-Process app; $Process.ProcessorAffinity=255"
Example: (8 Core Processor)
示例:(8 核处理器)
- 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
- 核心 #=值= 位掩码
- 核心 1 = 1 = 00000001
- 核心 2 = 2 = 00000010
- 核心 3 = 4 = 00000100
- 核心 4 = 8 = 00001000
- 核心 5 = 16 = 00010000
- 核心 6 = 32 = 00100000
- 核心 7 = 64 = 01000000
- 核心 8 = 128 = 10000000
Just add the decimal values together for which core you want to use. 255 = All 8 cores.
只需将要使用的核心的十进制值相加即可。255 = 全部 8 个内核。
- All Cores = 255 = 11111111
- 所有内核 = 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
这是关于如何更改进程的亲和力的非常详细的帖子:http: //www.energizedtech.com/2010/07/powershell-setting-processor-a.html
回答by skjerns
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.
255
接受的答案中给出的掩码在哪里。
回答by Jake
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.
对于其他人正在寻找答案但没有找到答案,我找到的解决方案是使用名为WinAFC(或 AffinityChanger)的应用程序。这是一个部分 GUI、部分命令行应用程序,它允许您为某些可执行文件指定配置文件,并将轮询它们的进程列表。如果找到匹配的进程,它将根据加载的配置文件中的设置更改这些进程的关联。
There is some documentation here: http://affinitychanger.sourceforge.net/
这里有一些文档: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.
此配置文件将任何 convert.exe 进程设置为使用前两个 CPU 核心对(CPU0、CPU1、CPU2 和 CPU3),每秒轮询一次。TestMode
是一个切换开关,可让您查看您的个人资料是否正常工作,而无需实际设置关联。
Hope someone finds this useful!
希望有人觉得这很有用!
回答by js2010
If you really like enums, you can do it this way. ProcessorAffinity is an IntPtr, so it takes a little extra type casting.
如果你真的喜欢枚举,你可以这样做。ProcessorAffinity 是一个 IntPtr,所以它需要一些额外的类型转换。
[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'
回答by npocmaka
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.
我认为这些是优先级。您也可以使用 PID 代替进程名称。