做代码优化,发现代码中获取系统CPU核数是通过system调用命令得到的,想想最近被system支配的恐惧,果断改掉。
linux c中获取CPU核数的函数原语有两个:
#include <sys/sysinfo.h>
get_nprocs_conf();
get_nprocs();
第二个函数是返回当前可用的CPU数量,不可用的意思是CPU HANG住了。如果安装了man page,可以直接在man page中找到用法。
#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;
}
此处评论已关闭