Linux
C programming
CPU detection
system programming
code example

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.

Browse interview questions

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.

c
1#include <stdio.h>
2#include <unistd.h>
3
4int main(void) {
5    long cpus = sysconf(_SC_NPROCESSORS_ONLN);
6
7    if (cpus == -1) {
8        perror("sysconf");
9        return 1;
10    }
11
12    printf("Online CPUs: %ld\n", cpus);
13    return 0;
14}

_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:

c
sysconf(_SC_NPROCESSORS_CONF)

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:

c
1#include <stdio.h>
2#include <unistd.h>
3
4int main(void) {
5    long configured = sysconf(_SC_NPROCESSORS_CONF);
6    long online = sysconf(_SC_NPROCESSORS_ONLN);
7
8    printf("Configured CPUs: %ld\n", configured);
9    printf("Online CPUs: %ld\n", online);
10    return 0;
11}

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().

c
1#include <stdio.h>
2#include <sys/sysinfo.h>
3
4int main(void) {
5    int cpus = get_nprocs();
6    printf("Online CPUs: %d\n", cpus);
7    return 0;
8}

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_CONF and _SC_NPROCESSORS_ONLN.
  • Parsing /proc/cpuinfo unnecessarily when sysconf already 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_CONF if you need configured CPUs instead of online CPUs.
  • If affinity or cgroup limits matter, inspect the effective CPU set separately.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.