I am aware that sysconf(_SC_NPROCESSORS_ONLN)
will give me the number of logical processors. But how can I get the number of physical processors?

- 25,185
- 9
- 78
- 101
4 Answers
@Havoc P: offline cpus aren't shown in /proc/cpuinfo .
CPU topology is described by /sys/devices/system/cpu/cpu*/topology/*

- 2,594
- 16
- 8
You could parse /proc/cpuinfo and count the number of distinct "physical id:" lines. Sort of annoying, but I don't know if there's a better option. If you're using GLib or another library with regex support it'd be easier. Or you could popen() a command line to do it if you're feeling really hacky. example command line at: http://www.brandonhutchinson.com/Understanding_proc_cpuinfo.html

- 8,365
- 1
- 31
- 46
-
POSIX regex support (BRE and ERE) is part of the standard library on any POSIX system, including Linux. No need for glib. – R.. GitHub STOP HELPING ICE Feb 25 '11 at 19:44
I think the best way to get this information is to use hwloc: http://www.open-mpi.org/projects/hwloc/.
They provide a bunch of user tools that let you get at the cpu topology of a system, but they also provide a library that you can use from C: http://www.open-mpi.org/projects/hwloc/doc/v1.1.1/#interface

- 1,978
- 14
- 10
Perhaps this answer to a similar question does help. There's a comment about the code not being correct, but it could be a good starting point.