【问题标题】:Get the Current CPUSet获取当前 CPUSet
【发布时间】:2013-09-20 11:59:32
【问题描述】:

我正在使用命令行中的 cpuset(即http://man7.org/linux/man-pages/man7/cpuset.7.html)来运行 C/C++ 程序。

我想知道 C/C++ 是否能够检索它在其上运行的 cpuset。

我阅读了http://man7.org/linux/man-pages/man3/CPU_SET.3.html,但我没有看到任何可以实现我想要的宏。

我想在程序中检索 cpuset 的主要原因是填充 cpu_set_t* 以便我可以将它传递给 pthread_attr_setaffinity_np()。

提前致谢。

【问题讨论】:

    标签: c linux scheduler glibc


    【解决方案1】:
    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    if (0 == sched_getaffinity(getpid(), sizeof(cpu_set_t), &cpuset)) {
        const long nCores = sysconf( _SC_NPROCESSORS_ONLN );
        for (long i = 0; i < nCores; i++) {
            if (CPU_ISSET(i, &cpuset)) {
                std::cout << "core # " << i << " is in cpuset" << std::endl;
            }
        }
    }
    else {
        std::cerr << "sched_getaffinity() failed: " << strerror(errno) << std::endl;
    }
    

    【讨论】:

      【解决方案2】:

      你可以在设置为的CPU上工作

          #define _GNU_SOURCE
          #include <sched.h>
      
      
          cpu_set_t my_set;
          CPU_ZERO(&my_set);
          CPU_SET(1, &my_set); // here 1 is the cpu 1 similarly u can set more
          CPU_SET(2, &my_set); // here 2 is the cpu 2 similarly u can set more
          pthread_setaffinity_np(pthread_self(), sizeof (cpu_set_t), &my_set);
      

      【讨论】:

      • 感谢 akp。我只是想自己:(您建议的方式不是检索现有的cpuset,而是创建一个cpuset ...
      猜你喜欢
      • 2012-05-29
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 2013-07-07
      • 2013-02-06
      • 2014-01-03
      相关资源
      最近更新 更多