gpt4 book ai didi

使用 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 或 PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY 时的 CreateProcess 问题

转载 作者:可可西里 更新时间:2023-11-01 11:55:18 25 4
gpt4 key购买 nike

当我调用 CreateProcess 并使用 PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY 扩展属性时,我不断收到错误 87,ERROR_INVALID_PARAMETERS。我使用完全相同的代码调用 CreateRemoteThreadEx,效果很好。此外, PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 似乎没有效果。那我做错了什么!?

Microsoft Windows Server 2008 R2 企业版,6.1.7601 SP1 内部版本 7601

我什至安装了这个服务包:A child process cannot be created by calling a CreateProcess function that uses the PROC_THREAD_ATTRIBUTE_PREFERRED_NODE parameter in Windows 7 or in Windows Server 2008 R2

示例代码如下:

#include <windows.h>

typedef unsigned __int64 QWORD;

class CErr {
public:
CErr(LPCSTR szFunc, DWORD nErr) {
char szBuf[0x10000];
DWORD fFlags = FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM;
DWORD fLang = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
if (!nErr)
nErr = GetLastError();
FormatMessage(fFlags, NULL, nErr, fLang, szBuf, sizeof(szBuf) - 1, NULL);
printf("%s: %s", szFunc, szBuf);
}
};

int main(int argc, char* argv[])
{
DWORD nErr;
size_t cb;
char sAttribsBuf[4096];
auto pAttribs = (PPROC_THREAD_ATTRIBUTE_LIST)sAttribsBuf;

if (!InitializeProcThreadAttributeList(NULL, 1, 0, &cb)
&& ((nErr = GetLastError()) != ERROR_INSUFFICIENT_BUFFER))
throw CErr("InitializeProcThreadAttributeList", nErr);
if (!InitializeProcThreadAttributeList(pAttribs, 1, 0, &cb))
throw CErr("InitializeProcThreadAttributeList", 0);

#if 1 // if enabled, CreateProcess succeeds, but doesn't set affinity

WORD iNuma = 1; // WORD is the only size that does not error here
if (!UpdateProcThreadAttribute(pAttribs, 0, PROC_THREAD_ATTRIBUTE_PREFERRED_NODE,
&iNuma, sizeof(iNuma), NULL, NULL))
throw CErr("UpdateProcThreadAttribute", 0);

#else // if enabled, CreateProcess fails with ERROR_INVALID_PARAMETERS(87)

GROUP_AFFINITY GrpAffinity = { 0 };
GrpAffinity.Mask = 1;
if (!UpdateProcThreadAttribute(pAttribs, 0, PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY,
&GrpAffinity, sizeof(GrpAffinity), NULL, NULL))
throw CErr("UpdateProcThreadAttribute", 0);

#endif

auto fCreationFlags = EXTENDED_STARTUPINFO_PRESENT;
PROCESS_INFORMATION pi = { 0 };
STARTUPINFOEX si = { 0 };
si.StartupInfo.cb = sizeof(si);
si.lpAttributeList = pAttribs;
if (!CreateProcess(NULL, "notepad.exe", NULL, NULL, false, fCreationFlags,
NULL, NULL, &si.StartupInfo, &pi))
throw CErr("CreateProcess", 0); // error if ...ATTRIBUTE_GROUP_AFFINITY

// SetProcessAffinityMask(pi.hProcess,1); // if enabled, notepad's affinity is set

WaitForSingleObject(pi.hProcess, INFINITE);

DeleteProcThreadAttributeList(pAttribs);

return 0;
}

最佳答案

从文档中看不清楚,但我想我明白了。 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 只能与 CreateProcess() 一起使用。 PROC_THREAD_ATTRIBUTE_IDEAL_PROCESSOR 和 PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY 只能与 CreateThread() 一起使用。

PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 可能将进程的亲和性设置为与节点在同一组中的所有处理器。我无法验证它,因为我的测试系统在两个 numa 节点上只有 12 个内核。将 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 设置为 0 或 1 会设置对所有核心的关联。我确实验证了 CreateProcess 创建的进程的堆栈位于 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 指示的 numa 节点上。也没有记录,传入节点的大小必须是 2 个字节。

关于使用 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE 或 PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY 时的 CreateProcess 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18647965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com