SetProcessAffinityMask - 选择多个处理器?
如何使用 SetProcessAffinityMask 选择多个逻辑处理器?
在 Windows 任务管理器中,您可以执行此操作作为示例:
我更新了我的 CreateProcess 过程来执行此操作:
type
TProcessPriority = (ptLow = $00000040,
ptBelowNormal = $00004000,
ptNormal = $00000020,
ptAboveNormal = $00008000,
ptHigh = $00000080,
ptRealtime = $00000100);
procedure RunProcess(FileName: string; Priority: TProcessPriority);
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CmdLine: string;
Done: Boolean;
begin
FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);
CmdLine := FileName;
UniqueString(CmdLine);
try
Done := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
CREATE_NEW_PROCESS_GROUP + Integer(Priority),
nil, nil, StartInfo, ProcInfo);
if Done then
begin
// Todo: Get actual cpu core count before attempting to set affinity!
// 0 = <All Processors>
// 1 = CPU 0
// 2 = CPU 1
// 3 = CPU 2
// 4 = CPU 3
// 5 = CPU 5
// 6 = CPU 6
// 7 = CPU 6
// 8 = CPU 7
// this sets to CPU 0 - but how to allow multiple parameters to
// set more than one logical processor?
SetProcessAffinityMask(ProcInfo.hProcess, 1);
end else
MessageDlg('Could not run ' + FileName, mtError, [mbOk], 0)
finally
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end;
请注意我在那里添加的注释。最好更新我的过程以包含一个新的 Affinity 参数,我可以将其传递给 SetProcessAffinityMask。
出于显而易见的原因,调用其中任何一个都不会选择相应的处理器,它们给出了我想要执行的操作的想法:
SetProcessAffinityMask(ProcInfo.hProcess, 1 + 2);
SetProcessAffinityMask(ProcInfo.hProcess, 1 and 2);
例如,为进程选择任何 CPU,如任务管理器中所示。
我应该如何使用数组、集合或其他东西来做到这一点?我无法让它与多个值一起工作。
谢谢。
How can I use SetProcessAffinityMask to select more than one logical processor?
In Windows Task Manager you can do this as an example:
I updated my CreateProcess procedure to do this:
type
TProcessPriority = (ptLow = $00000040,
ptBelowNormal = $00004000,
ptNormal = $00000020,
ptAboveNormal = $00008000,
ptHigh = $00000080,
ptRealtime = $00000100);
procedure RunProcess(FileName: string; Priority: TProcessPriority);
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CmdLine: string;
Done: Boolean;
begin
FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb := SizeOf(TStartupInfo);
CmdLine := FileName;
UniqueString(CmdLine);
try
Done := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
CREATE_NEW_PROCESS_GROUP + Integer(Priority),
nil, nil, StartInfo, ProcInfo);
if Done then
begin
// Todo: Get actual cpu core count before attempting to set affinity!
// 0 = <All Processors>
// 1 = CPU 0
// 2 = CPU 1
// 3 = CPU 2
// 4 = CPU 3
// 5 = CPU 5
// 6 = CPU 6
// 7 = CPU 6
// 8 = CPU 7
// this sets to CPU 0 - but how to allow multiple parameters to
// set more than one logical processor?
SetProcessAffinityMask(ProcInfo.hProcess, 1);
end else
MessageDlg('Could not run ' + FileName, mtError, [mbOk], 0)
finally
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end;
Note the comments I put in there. It would be good to update my procedure to include a new Affinity parameter which I can pass to SetProcessAffinityMask.
Calling any of these won't select the corresponding processors for obvious reasons, they give the idea of what I am wanting to do:
SetProcessAffinityMask(ProcInfo.hProcess, 1 + 2);
SetProcessAffinityMask(ProcInfo.hProcess, 1 and 2);
eg, select any of the CPU's for a process, as shown in Task Manager.
How should I do this, using an Array, Set or something else? I cannot get it to work with multiple values.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是一个位掩码,如文档中所述。
等等。您可以使用逻辑
或
来组合它们。因此处理器 0 和 1 将是$01
或$02
,等于$03
。我将使用移位运算符
shl
为特定处理器创建值。像这样:您可以轻松地扩展它,以在循环中使用逻辑
or
生成处理器列表的掩码。您可以测试处理器是否处于位掩码中,如下所示:
注意:我使用的是
DWORD_PTR
,因为对于 64 位目标,位掩码是 64 位宽。这种细微差别对于 XE 上的您来说并不重要,但值得正确对待它以使将来的代码移植更容易。It is a bitmask as described in the documentation.
And so on. You can use logical
or
to combine them. So processors 0 and 1 would be$01
or$02
which equals$03
.I would use the shift operator
shl
to create values for specific processors. Like this:You can readily extend this to generate masks for lists of processors using logical
or
in a loop.You can test for a processor being in a bit mask like this:
Note: I'm using
DWORD_PTR
because for 64 bit targets the bitmask is 64 bits wide. That nuance doesn't matter for you on XE but it's worth getting it right to make any future code porting easier.它在我的 XE 上是 32 位位掩码(但在 64 位 XE2 上可能是 64 位!)
只需定义一组 [0..31],其中 0=cpu 1 等。
然后将结果键入掩码为双字。
所以
It's a 32-bit bitmask on my XE (but it could be 64-bit on 64-bit XE2!)
Just define a set of [0..31] where 0=cpu 1 etc.
Then typemask the result to a dword.
So