0

I am trying to change affinity of a program to use Core 1,2,3 and 4 of a CPU. And not the rest of them. I have searched a bit around.. I found this one: How Can I Set Processor Affinity in .NET?

But it didn't help me out..

I have a way to get the numbers of cores the CPU have. So it can adjust how many cores it will change it to. So it won't try to change it to more cores than the CPU got and so on..

Is there any easy way to do this?

Naits
  • 13
  • 1
  • 3
    Can you tell us what you tried from the linked question/answer, what you observed and what you expected? – Lasse V. Karlsen Dec 28 '19 at 20:14
  • 2
    Your question isn't clear. Do you want to change the affinity of the running thread? (ie from within the thread itself) Or do you want this application you're writing to be able to change the affinity of another separate process on the system (ie like Task Manager does) – Rowan Smith Dec 28 '19 at 20:18
  • 1
    @RowanSmith Yes that is right. I am currently able to change the priority. But I can't get affinity to work... – Naits Dec 28 '19 at 20:27
  • Yes, doesn't help us know which of the two options you are trying to resolve. Please choose either (1) set affinity of my ApplicationA from inside my ApplicationA; or (2) set the affinity of ApplicationA from ApplicationB – Rowan Smith Dec 28 '19 at 20:57
  • @RowanSmith Woops.. Well I am trying to change affinity of a program from within my program... – Naits Dec 28 '19 at 21:10

2 Answers2

1

I have successfully used the following to put my process on the the first CPU

Console.WriteLine("Press Enter to put the process onto Core 1");
Console.ReadLine();

Process Proc = Process.GetCurrentProcess();
long AffinityMask = (long)Proc.ProcessorAffinity;
AffinityMask &= 0x0001; // Put my process on the First Core
Proc.ProcessorAffinity = (IntPtr)AffinityMask;

Console.WriteLine("Process is now on Core 1");
Console.WriteLine("Press Enter to exit");
Console.ReadLine();

You can check in Task Manager the before and after affinity.

Update:

ProcessorAffinity represents each processor as a bit. Bit 0 represents processor one, bit 1 represents processor two, and so on.

The following table shows a subset of the possible ProcessorAffinity for a four-processor system. Property value (in hexadecimal) Valid processors

0x0001  1
0x0002  2
0x0003  1 or 2
0x0004  3
0x0005  1 or 3
0x0007  1, 2, or 3
0x000F  1, 2, 3, or 4
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Rowan Smith
  • 1,815
  • 15
  • 29
0

Just as an extension to answer by @Rowan Smith, there is an additional way of writing binary numbers in C# 7.0 and higher - binary literals.

To specify hex literal one writes 0x at the beginning of number. For binary literals one should write 0b like this:

0b0000_0000_0000_0001 -> 1
0b0000_0000_0000_0010 -> 2
0b0000_0000_0000_1111 -> 1 & 2 & 3 4

You write them like:

int value = 0b0000_0000_0000_0001; 

Some people might find it easier to write it like this instead of recalculating number between hex and binary representation, although the number itself is longer.

GrayCat
  • 1,749
  • 3
  • 19
  • 30
  • Personally I like to use `1 << 0`, `1 << 1`, `1 << 2`, `1 << 3`, etc. to show setting a single bit, it makes it easier to figure out if you are setting `1 << 3` the you are setting the bit for index 3 (wrt a 0 based index) – Scott Chamberlain Dec 29 '19 at 00:07
  • @ScottChamberlain yes indeed, I prefer it too for cases when I have to set only one bit. It starts being a little harder to grasp when you have to set multiple bits. Using binary literals imposes less mental overhead of calculating it in mind for me. – GrayCat Dec 29 '19 at 00:15