none
对CPU组二设置亲和性 RRS feed

  • 问题

  • 大家好,我的问题是这样的:

             在windows7系统下,当CPU线程数超过64会自动分成两组,如何在C#或者C++程序中对某个进程设置CPU组二的亲和性?

    2018年4月23日 6:31

全部回复

  • 你好,

    C# 可以使用下面的代码来改组二的亲和性,值1 是组一,值3 是全部。

    using System;
    using System.Diagnostics;
    
    
    using System.Text;
    
    
    namespace caAfinity
    
    
    {
    
    
    class Program
    
    
    {
    
    
    static void Main(string[] args)
    
    
    {
    
    
    Console.WriteLine("Current ProcessorAffinity: {0}", Process.GetCurrentProcess().ProcessorAffinity);
    
    
    Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)2;
    
    
    Console.WriteLine("Current ProcessorAffinity: {0}", Process.GetCurrentProcess().ProcessorAffinity);
    
    
    while (true)
    
    
    { }                         // Tight CPU loop
    
    
    }
    
    
    }
    
    
    }


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2018年4月23日 8:53
    版主
  • hello,

          Zhanglong.

          Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)2;

          这样写法应该只是绑定第2个超线程(2的1次方)的方法吧?而不是在第二组CPU上绑定任意一个或多个超线程。

    2018年4月24日 0:27
  • 你好,

    请看下面的链接, 你可以看看Alois Kraus 回答里面的comments.

    No you do not need these appsettings since they are only necessary if you want to let your threads trun on all CPU groups. But you want to run only on group 0 or 1. To do that you need to call SetProcessAffinity(1) or SetProcessAffinity(2) to set bit 0 or bit 1.

    https://stackoverflow.com/questions/44854648/processor-affinity-group-c-sharp

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年4月30日 7:27
    版主