How to get the number of CPUs in Linux using C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On Linux, the standard practical way to get the number of CPUs available to your program is sysconf(_SC_NPROCESSORS_ONLN). That returns the number of processors currently online. If you want something more Linux-specific, glibc also provides get_nprocs(). The right choice depends on whether you want broader POSIX-style portability or a Linux-focused helper.
The Most Common Solution: sysconf
sysconf is the usual answer because it is familiar, widely supported on Unix-like systems, and easy to call.
_SC_NPROCESSORS_ONLN means the number of processors currently online and usable by the system.
Online CPUs Versus Configured CPUs
Linux distinguishes between processors that exist and processors that are currently online.
Another related query is:
That reports the number of configured processors, which can differ from the online count on systems where CPUs can be hot-plugged or administratively disabled.
A small comparison program:
For most application-level scheduling decisions, the online count is the more useful number.
Linux-Specific Helper: get_nprocs
On glibc-based Linux systems, you can also use get_nprocs().
This is concise, but it is Linux-specific. If you want code that ports more easily to other Unix-like systems, sysconf is usually the safer default.
What This Number Actually Means
Be careful about terminology. The reported count is usually logical CPUs, not necessarily physical cores.
That means:
- Hyper-Threading or SMT can increase the count
- a machine with 8 physical cores may report 16 logical CPUs
- containers or affinity restrictions may change what is effectively available to your process
So the number is useful, but it is not always the same thing as "physical core count."
If Your Process Has CPU Affinity Limits
A process may be restricted to a subset of CPUs via affinity or cgroups. In those cases, the system-wide online CPU count may not reflect the CPUs your process can actually run on.
If you need the effective CPU set for the current process, inspect affinity directly with APIs such as sched_getaffinity.
That is a different question from "how many CPUs are online on the machine."
Avoid Parsing /proc/cpuinfo First
You can count CPU entries in /proc/cpuinfo, but it is usually not the best first choice. Parsing pseudo-files is more fragile and less direct than using system APIs already designed for this purpose.
Use /proc/cpuinfo only when you specifically need extra topology detail, not just a CPU count.
Common Pitfalls
- Using the online CPU count and calling it physical core count.
- Forgetting the difference between
_SC_NPROCESSORS_CONFand_SC_NPROCESSORS_ONLN. - Parsing
/proc/cpuinfounnecessarily whensysconfalready gives the answer. - Ignoring process affinity or container limits when what you really need is usable CPUs for the current process.
- Treating Linux-specific helpers such as
get_nprocs()as portable Unix APIs.
Summary
- On Linux,
sysconf(_SC_NPROCESSORS_ONLN)is the standard practical way to get the online CPU count in C. - '
get_nprocs()is a convenient Linux-specific alternative.' - Online CPU count is usually a logical processor count, not necessarily physical cores.
- Use
_SC_NPROCESSORS_CONFif you need configured CPUs instead of online CPUs. - If affinity or cgroup limits matter, inspect the effective CPU set separately.
Related reading
- How to get the Transaction ID or relate message with Dialogic DSI library in C?
- How to handle missing 'emplace_range' in C0x STL?
- How to handle promise when calling async JS method from C using Emscripten
- How to implement a unmanaged thread-safe collection when I get this error mutex is not supported when compiling with /clr
- How to implement classic sorting algorithms in modern C?
- How to implement LFU cache using STL?
- How to implement strlen as fast as possible
- How to increase thread priority in pthreads?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.