This article has participated in the call for good writing activities, click to view: back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!

Seller concept

(1) physical CPU

Number of cpus on slots in the actual Server Number of physical cpus that cannot be repeated

(2) logical CPU

The /proc/cpuinfo file is no stranger to Linux users. It is used to store CPU hardware information. It lists the specifications of processor 0 to N. One thing to note here is that if you think that n is the true number of cpus, you’re wrong. In general, we think that a CPU can have multiple cores, With Intel’s hyper-threading (HT) technology, you can logically double the number of CPU cores. Number of logical cpus = number of physical cpus x CPU cores x 2(if HT is supported and enabled) In Linux, the number of cpus in top view is also the number of logical cpus

(3) the number of CPU cores

The number of chipsets that can process data on a CPU, such as the current i5 760, a dual-core, four-thread CPU, and the i5 2250, a four-core, four-thread CPU

In general, the number of physical cpus x number of cores should equal the number of logical cpus, and if it is not equal, it indicates that the server’s CPU supports hyperthreading

(2) View CPU information

When we cat /proc/cpuinfo, cpus with the same core ID are hyperthreads with the same core and cpus with the same physical ID are threads or cores encapsulated by the same CPU

(iii) Examples follow

① Check the number of physical cpus

#cat /proc/cpuinfo |grep “physical id”|sort |uniq|wc -l 2

② Check the number of logical cpus

#cat /proc/cpuinfo |grep “processor”|wc -l 8

③ Check the CPU cores

#cat /proc/cpuinfo |grep “cores”|uniq 4

I should have 2 cpus here, and each Cpu has 4 cores, so the logical Cpu is 8

On Windows, we can use the **GetSystemInfo()** function to get some software and hardware information about the current system. One of these is the number of processor cores currently in the machine. To obtain the desired information, use the following statement:

SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
Copy the code

On Linux, you can use **sysconf() or get_nprocs()** to get the number of cores. The following are introduced respectively:

**#include

**

Sysconf (_SC_NPROCESSORS_CONF) returns the number of cores available to the system, but its value includes the number of disabled cores on the system, so it does not represent the number of cores currently available on the system. The return value of sysconf(_SC_NPROCESSORS_ONLN) really represents the number of cores currently available on the system.

The GNU C library provides another way to get the number of cores available to a machine. The intget_nprocs_conf (void) and intget_nprocs (void) functions are defined in sys/sysinfo.h to get the number of cores on the machine. The return value of get_nprocs_conf (void) is similar to sysconf(_SC_NPROCESSORS_CONF). The return value of get_nprocs (void) is similar to sysconf(_SC_NPROCESSORS_ONLN), which truly reflects the number of available cores.

1. Linux obtains the number of CPU cores

CPP: Defines the entry point for the console application.
//


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

#include<unistd.h>	// sysconf( )
#include<sys/sysinfo.h> // 
int main(a)
{
    //usleep(10 * 1000);

    int cpu_num;
    cpu_num = sysconf(_SC_NPROCESSORS_CONF);
    printf("_SC_NPROCESSORS_CONF=%d\n", cpu_num);

    cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
    printf("_SC_NPROCESSORS_ONLN=%d\n", cpu_num);

    cpu_num = get_nprocs();
    printf("get_nprocs=%d\n", cpu_num);

    cpu_num = get_nprocs_conf();
    printf("get_nprocs_conf=%d\n", cpu_num);

    return 0;
}
/* * - _SC_NPROCESSORS_CONF * The number of processors configured. * * - _SC_NPROCESSORS_ONLN * The number of processors  currently online (available). */

Copy the code

2. The number of cpus Windows obtains

CPP: Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
DWORD GetNumberOfProcessors(a)
{
    SYSTEM_INFO si;

    // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
    PGNSI pfnGNSI = (PGNSI)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetNativeSystemInfo");
    if (pfnGNSI)
    {
        pfnGNSI(&si);
    }
    else
    {
        GetSystemInfo(&si);
    }
    return si.dwNumberOfProcessors;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int nCpuSum = GetNumberOfProcessors();
    printf("Total number of cpus not =[%d]\n", nCpuSum);

    system("pause");

	return 0;
}


Copy the code