1

C/C++ プログラムを実行するために、コマンド ライン (つまりhttp://man7.org/linux/man-pages/man7/cpuset.7.html ) からcpuset を使用しています。

C/C++ がその上で実行されている cpuset を取得できるかどうか疑問に思います。

私はhttp://man7.org/linux/man-pages/man3/CPU_SET.3.htmlを読みましたが、私が望むものを達成できるマクロがそこにありません。

プログラム内で cpuset を取得する主な理由は、pthread_attr_setaffinity_np() に渡すことができるように cpu_set_t* を埋めることです。

前もって感謝します。

4

2 に答える 2

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;
}
于 2013-09-20T12:22:43.603 に答える
0

U は次のように設定された 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);
于 2013-09-20T12:18:34.370 に答える