Is it possible to get such info by some API or function, rather than parsing the /proc/cpuinfo
?
相关问题
- Multiple sockets for clients to connect to
- slurm: use a control node also for computing
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
You can use this for mostly all kind of linux distro
For C code
(In QNX systems , you can use
num_cpus = sysinfo_numcpu()
)For shell scripting, you can use
cat /proc/cpuinfo
or use
lscpu
ornproc
commands in linuxNo, it is not. Either you must parse cpuinfo file, or some library will do it for you.
Parse the file /proc/cpuinfo. This'll give you lot of details about the CPU. Extract the relevant fields into your C/C++ file.
libcpuid
provides a simple API which will directly return all the CPU features, including number of cores. To get the number of cores at runtime, you could do something like this:Depending on your flavor of Linux you will get different results from /proc/cpuid.
This works for me on CentOS for getting total number of cores.
The same doesn't work in Ubuntu. For Ubuntu you can use the following command.
Have you ever seen the output of this shell command "cat /proc/cpuinfo"? I think there you can get out all the information that you need. To read the information in a C program I would prefer the file manipulation functions like fopen, fgets and so on.