Number of CPU Cores
- Method 1:
Use the sysconf() function with the _SC_NPROCESSORS_CONF and _SC_NPROCESSORS_ONLN parameters specified:
_SC_NPROCESSORS_CONF: returns the number of CPU cores in the system. All CPU cores are counted even if some cannot be used by users.
_SC_NPROCESSORS_ONLN: returns the number of available CPU cores in the system.
Example:
#include "unistd.h" printf("system cpu num is %d\n", sysconf( _SC_NPROCESSORS_CONF)); printf("system enable cpu num is %d\n", sysconf(_SC_NPROCESSORS_ONLN)); - Method 2:
Use the get_nprocs_conf() and get_nprocs() functions to obtain the number of CPUs.
- get_nprocs_conf(): returns the number of all CPU cores in the current system.
- get_nprocs(): returns the number of CPU cores that can be used by the current user.
#include "sys/sysinfo.h" printf("system cpu num is %d\n", get_nprocs_conf()); printf("system enable num is %d\n", get_nprocs());
Parent topic: C/C++