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;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有