I have server with two physical processors. I want to set a current process to a specific group as if I do it manually from task manager. Note that it is not affinity but processor group (each group has 16 logical processors). I could not find any way to do that neither in C# nor in C++. Alternatively I tried to create a process with preset attribute:
LPPROC_THREAD_ATTRIBUTE_LIST pAttribs = NULL;
WORD iNuma = 0;
STARTUPINFOEX sInfoEx;
sInfoEx.StartupInfo.cb = sizeof(sInfoEx);
DWORD size;
int success = InitializeProcThreadAttributeList(0, 1, 0, &size);
pAttribs = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(HeapAlloc(GetProcessHeap(), 0, size));
success = InitializeProcThreadAttributeList(pAttribs, 1, 0, &size);
success = UpdateProcThreadAttribute(pAttribs, 0, PROC_THREAD_ATTRIBUTE_PREFERRED_NODE, &iNuma, sizeof(iNuma), NULL, NULL);
long err = GetLastError();
auto fCreationFlags = EXTENDED_STARTUPINFO_PRESENT;
PROCESS_INFORMATION pi = { 0 };
STARTUPINFOEX si = { 0 };
si.StartupInfo.cb = sizeof(si);
si.lpAttributeList = pAttribs;
int p = CreateProcess(NULL, L"notepad.exe", NULL, NULL, false, fCreationFlags, NULL, NULL, &si.StartupInfo, &pi);
call to CreateProcess crashes the application with access violation.
This code does not work either. It just chagnes affinity of one thread but does not look it affects group of a process: How Can I Set Processor Affinity in .NET?
According to the documentation , the Unicode version of CreateProcess
can modify the command line (second) parameter. Passing in a constant string (which you are doing) can result in an access violation.
You'll need pass in a string
or array that is modifiable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.