如何使用C获取Linux中的CPU数量?

是否有API可以获取Linux中可用的CPU数量?我的意思是,不使用/ proc / cpuinfo或任何其他sys-node文件…

我发现使用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;

}

但是,使用通用库是否还没有更高的层次?

回答:

#include <stdio.h>

#include <sys/sysinfo.h>

int main(int argc, char *argv[])

{

printf("This system has %d processors configured and "

"%d processors available.\n",

get_nprocs_conf(), get_nprocs());

return 0;

}

https://linux.die.net/man/3/get_nprocs

以上是 如何使用C获取Linux中的CPU数量? 的全部内容, 来源链接: utcz.com/qa/413029.html

回到顶部