1、在Linux下获取CPU核数
linux下可以通过linux系统提供的sysconf()来获取当前CPU个数,sysconf在头文件unistd.h中声明。
sysconf函数中输入参数_SC_NPROCESSORS_CONF和_SC_NPROCESSORS_ONLN均可以获取系统CPU个数。
_SC_NPROCESSORS_CONF:返回系统所有的CPU核数,这个值也包括系统中禁止用户使用的CPU个数;
_SC_NPROCESSORS_ONLN:返回系统中可用的CPU核数;
#include "unistd.h"
printf("system cpu num is %d", sysconf( _SC_NPROCESSORS_CONF));
printf("system enable cpu num is %d", sysconf(_SC_NPROCESSORS_ONLN));
2、GNU C Library也提供了一种获取CPU个数的方法,get_nprocs_conf()和get_nprocs()函数可以用来获取系统CPU个数,在头文件”sys/sysinfo.h”中声明。
get_nprocs_conf():与sysconf( _SC_NPROCESSORS_CONF)作用相同,获取当前系统所有的CPU核数;
get_nprocs():与sysconf( _SC_NPROCESSORS_ONLN)作用相同,获取当前系统用户可以使用的CPU核数。
#include "sys/sysinfo.h"
printf("system cpu num is %d", get_nprocs_conf());
printf("system enable num is %d", get_nprocs());
Original: https://www.cnblogs.com/zhjh256/p/16412175.html
Author: zhjh256
Title: c语言获取cpu数量
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535642/
转载文章受原作者版权保护。转载请注明原作者出处!