How to get number of cores on linux using c programming? -
i know how number of logical cores in c.
sysconf(_sc_nprocessors_conf);
this return 4 on i3 processor. there 2 cores in i3.
how can physical core count?
this c solution using libcpuid.
cores.c:
#include <stdio.h> #include <libcpuid.h> int main(void) { struct cpu_raw_data_t raw; struct cpu_id_t data; cpuid_get_raw_data(&raw); cpu_identify(&raw, &data); printf("no. of physical core(s) : %d\n", data.num_cores); return 0; }
this c++ solution using boost.
cores.cpp:
// use boost number of cores on processor // compile : g++ -o cores cores.cpp -lboost_system -lboost_thread #include <iostream> #include <boost/thread.hpp> int main () { std::cout << "no. of physical core(s) : " << boost::thread::physical_concurrency() << std::endl; std::cout << "no. of logical core(s) : " << boost::thread::hardware_concurrency() << std::endl; return 0; }
on desktop (i5 2310) returns:
no. of physical core(s) : 4 no. of logical core(s) : 4
while on laptop (i5 480m):
no. of physical core(s) : 2 no. of logical core(s) : 4
meaning laptop processor have hyper-threading tecnology
Comments
Post a Comment