首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取当前CPUSet

获取当前CPUSet
EN

Stack Overflow用户
提问于 2013-09-20 19:59:32
回答 2查看 1.8K关注 0票数 1

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

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

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

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

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2013-09-20 20:22:44

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2013-09-20 20:18:34

可以在CPU上工作,设置为

代码语言:javascript
复制
    #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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18916254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档