当我调用CreateProcess并使用PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY扩展属性时,我一直收到错误87,ERROR_INVALID_PARAMETERS。我使用完全相同的代码来调用CreateRemoteThreadEx,这很好。此外,PROC_THREAD_ATTRIBUTE_PREFERRED_NODE似乎没有任何效果。那么我做错了什么!?
Microsoft Windows Server 2008 R2 Enterprise,6.1.7601 SP1 Build 7601
以下是示例代码:
#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;
}
答案 0 :(得分:2)
从文档中不清楚,但我想我已经明白了。 PROC_THREAD_ATTRIBUTE_PREFERRED_NODE仅应与CreateProcess()一起使用。 PROC_THREAD_ATTRIBUTE_IDEAL_PROCESSOR和PROC_THREAD_ATTRIBUTE_GROUP_AFFINITY只能与CreateThread()一起使用。
PROC_THREAD_ATTRIBUTE_PREFERRED_NODE可能正在将进程的亲缘关系设置为与节点在同一GROUP中的所有处理器。我无法验证它,因为我的测试系统在两个numa节点上只有12个核心。将PROC_THREAD_ATTRIBUTE_PREFERRED_NODE设置为0或1可设置与所有核心的亲缘关系。我确实验证了CreateProcess创建的进程堆栈位于PROC_THREAD_ATTRIBUTE_PREFERRED_NODE指示的numa节点上。也没有记录,传入的节点的大小必须是2个字节。