In this blog post we will know How to get number of processors in Linux. Here number of processors means number of available cpu cores. Every Linux distribution comes with some available bash shell commands and ‘nproc‘ shell commands gives total available cpu(s). We will also write a  simple C  program using sysconf system call to find number of cpu cores.

How to get number of processors in Linux

Image credit:https://superuser.com/questions/317936/cpu-core-temperature-variation

How to get number of processors in Linux using nproc command

We can use nproc command to find available cpu cores.

bosch@bosch-Inspiron-N5050:~$ nproc 
4

How to get number of cpu cores using lscpu command in linux

bosch@bosch-Inspiron-N5050:~$ lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel

C program to get number of processors in Linux

#include<stdio.h>
#include<unistd.h>

int main()
{
    printf("No. of cpu(s) are : %ld\n",sysconf(_SC_NPROCESSORS_CONF));
    return 0;
}

Using proc file system to know cpu information

(Some output of cat /proc/cpuinfo)

bosch@bosch-Inspiron-N5050:~$ cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 42
model name	: Intel(R) Core(TM) i3-2330M CPU @ 2.20GHz
stepping	: 7
microcode	: 0x1b
cpu MHz		: 799.992
cache size	: 3072 KB
physical id	: 0
siblings	: 4
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
core id		: 1
cpu cores	: 2
apicid		: 2
initial apicid	: 2
fpu		: yes

physical id	: 0
siblings	: 4
core id		: 0
cpu cores	: 2
apicid		: 1
initial apicid	: 1
fpu		: yes

clflush size	: 64
cache_alignment	: 64
address sizes	: 36 bits physical, 48 bits virtual
power management:

processor	: 3
vendor_id	: GenuineIntel
cpu family	: 6
model		: 42
model name	: Intel(R) Core(TM) i3-2330M CPU @ 2.20GHz
stepping	: 7
microcode	: 0x1b
cpu MHz		: 799.992
cache size	: 3072 KB
physical id	: 0
siblings	: 4
core id		: 1
cpu cores	: 2
apicid		: 3
initial apicid	: 3
fpu		: yes
fpu_ex


Related Contents to follow