0

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?

Community
  • 1
  • 1
sergman
  • 171
  • 1
  • 2
  • 12

1 Answers1

0

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.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • Great! This solved the problem with process creation. I wish there would be solution to change process group of the existing process. – sergman Nov 15 '16 at 03:05