3

I am working with a CPU-intensive real time application, and therefore I am trying to reserve a whole core for it.

To accomplish this in Windows, I am trying to set the CPU affinity of all running processes to the other cores, and then set the affinity of my real time application to the "free" core. Additionally, I am setting the priority to high.

Unfortunately, the following code (129 for testing as it means first and last core on my system) is not changing the affinity of all running processes:

while (Process32Next(hSnapShot, processInfo)!=FALSE)
{
    hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, processInfo->th32ProcessID);
    SetProcessAffinityMask(hProcess, 129);
}

Some system processes, like svchost.exe or csrss.exe, have the affinity 0xCCCCCCCC (looks like it is not initialized and is not used at all). And, of course, they are keeping it after a failed SetProcessAffinityMask().

Also, using Task Manager is not possible, as it denies access when trying to change affinity of those system processes.

Is it possible to change affinity for those processes as well?

Additional Information:

  • Windows 7 64bit
  • Real-time app has only one thread, therefore one core is "enough".
  • The below images show the difference:

Not working:

Not working

Working:

Working

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Pogoattack
  • 31
  • 2
  • 1
    This isn't possible for fairly obvious reasons. Imagine if multiple processes did this? Windows isn't a real time OS so you've got the wrong tool for the job. If you would check for errors in your code you'd learn why it was failing. Why aren't you checking for errors? – David Heffernan Nov 06 '16 at 19:53
  • I know that windows is not the right solution but I am trying to make the best out of the situation as I have given apps/code compiled for windows and there is nothing I can do about it. Just because it is not supposed to change doesn't automatically mean it is not possible. But it is in the users responsibility what kind of software he is using. Don't get me wrong I am not saying this is a good idea for everyone. Setprocessoraffinity returns a boolean to determine weather it was successful or not. This value is zero for all "3435973836" values. GetLastError() returns 0 for all processes. – Pogoattack Nov 06 '16 at 21:13
  • You don't check return values and you don't call GetLastError. – David Heffernan Nov 06 '16 at 21:21
  • And the code above is not the same producing the screenshots. The code above is shortend to point out the problem. But I added those informations in the last comment: not error, return is nonzero. – Pogoattack Nov 06 '16 at 22:12
  • Asking about code, but showing different code isn't ideal. Anyway, you won't be able to reserve a processor for one process only. You need a real time OS. – David Heffernan Nov 06 '16 at 22:13
  • I know and this is a fairly desperate try to make the best out of the given situation as I don't have the possibility to run the given program on anything but windows. – Pogoattack Nov 06 '16 at 22:14
  • Just don't run anything else on the machine – David Heffernan Nov 06 '16 at 22:16
  • I *think* that the best you can do is to set the priority of your own process to as high as possible (having first set the affinity of course). There's nothing you can do about hardware interrupts or the preemption of your thread to deal with them, or about those system processes that run with higher priority still. But it's as good as you can get. – Harry Johnston Nov 08 '16 at 02:25
  • I was already afraid that this might be the only solution. Still better than nothing. Thank you for your advise. – Pogoattack Nov 08 '16 at 06:30
  • Does all of the above still apply for Windows 10? From purely technical POV (ignoring whether it's "right" or not, or whether it's a wrong type of OS for the use case), is it even *possible* to change the affinity for system processes (or the system affinity masks)? Even if the application was running with Administrator privileges, would Windows just prohibit it anyway? – huoneusto Jan 04 '21 at 13:13

0 Answers0