taskset是linux自带的一个命令,可用来将进程绑定到指定CPU
相关的函数有: sched_setaffinity, CPU_CLR, CPU_ISSET, CPU_SET, CPU_ZERO


// cpufreq库可在/usr/lib目录下找到
// 编译: g++ -g -o x x.cpp -lcpufreq
// 需要以root用户执行以下代码
//#include <cpufreq.h>
#include <stdio.h>
#include <sys/sysinfo.h> // get_nprocs

// 如果不存在/usr/include/cpufreq.h
#ifndef _CPUFREQ_H
    extern "C" int cpufreq_cpu_exists(unsigned int cpu);
    extern "C" unsigned long cpufreq_get_freq_kernel(unsigned int cpu);
    extern "C" unsigned long cpufreq_get_freq_hardware(unsigned int cpu);
    extern "C" int cpufreq_get_hardware_limits(unsigned int cpu, unsigned long *min, unsigned long *max);
#endif

int main()
{
    // 取得cpu core的个数,proc是processor的意思
    int nprocs = get_nprocs();
    for (int i=0; i<nprocs; ++i)
    {
        if (0 == cpufreq_cpu_exists(i))
        {   
            unsigned long min_freq = 0;
            unsigned long max_freq = 0;
            cpufreq_get_hardware_limits(i, &min_freq, &max_freq);

            printf("cpu[%d]:\n", i); 
            printf("min_freq: %lu, max_freq: %lu\n", min_freq, max_freq);
            printf("kernel freq: %lu, hardware freq: %lu\n", cpufreq_get_freq_kernel(i), cpufreq_get_freq_hardware(i));
            printf("\n");
        }   
    }   

    return 0;
}


相关文章:

  • 2021-04-25
  • 2021-11-17
  • 2021-07-30
  • 2021-07-17
  • 2021-12-10
  • 2021-06-05
  • 2022-12-23
猜你喜欢
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
相关资源
相似解决方案