To set the affinity of CPUs using C#

前端 未结 4 1489
甜味超标
甜味超标 2021-02-09 23:26

I have created a window application in C#.Now I want to set the CPU affinity for this application.I may have 2 processors,4 processors,8 processors or may be more than 8 process

4条回答
  •  执念已碎
    2021-02-09 23:47

    The provided sample program by Alex Filipovivi seems incorrect, in that it ORs processor numbers into newAffinity without first converting them into a set bit. So if you input 3,4 to this program, you get an affinity mask of 7, which is cores 1, 2, and 3! The mask should be set to 12 (hex 0xC, binary 1100, which has bits 2 and 3 set, if bit 0 is the least significant bit).

    Replacing

    newAffinity = NewAffinity | int.Parse(item);
    

    with

    newAffinity = newAffinity | (1 << int.Parse(item)-1);
    

    Is one reasonable way to do that.

提交回复
热议问题