我从命令行(即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()。
提前谢谢。
发布于 2013-09-20 20:22:44
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;
}发布于 2013-09-20 20:18:34
可以在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);https://stackoverflow.com/questions/18916254
复制相似问题