获得使用C Linux的CPU数量 [英] Get number of CPUs in Linux using C
本文介绍了获得使用C Linux的CPU数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个API来获取CPU的Linux中提供的号码吗?
我的意思是,不使用/ proc内/ cpuinfo或任何其他SYS节点文件...
我发现使用sched.h中此实现:
INT GetCPUCount()
{
cpu_set_t CS;
CPU_ZERO(安培; CS);
sched_getaffinity(0,sizeof的(CS),及放大器; CS); 诠释计数= 0;
的for(int i = 0; I< 8;我++)
{
如果(CPU_ISSET(I,功放; CS))
算上++;
}
返回计数;
}
不过,是不是有使用公共库东西更多更高级别的?
解决方案
的#include<&unistd.h中GT;
的sysconf(_SC_NPROCESSORS_ONLN);
Is there an API to get the number of CPUs available in Linux? I mean, without using /proc/cpuinfo or any other sys-node file...
I've found this implementation using sched.h:
int GetCPUCount()
{
cpu_set_t cs;
CPU_ZERO(&cs);
sched_getaffinity(0, sizeof(cs), &cs);
int count = 0;
for (int i = 0; i < 8; i++)
{
if (CPU_ISSET(i, &cs))
count++;
}
return count;
}
But, isn't there anything more higher level using common libraries?
解决方案
#include <unistd.h>
sysconf(_SC_NPROCESSORS_ONLN);
这篇关于获得使用C Linux的CPU数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文