C programming skills-get the number of available processor (CPU) Cores

Source: Internet
Author: User
Requirement

During programming, especially in program development involving the system environment, we may need to dynamically obtain the number of available CPU cores in the current machine. For example, when we need to perform parallel computing, we may determine the degree of Parallelism Based on the number of CPU cores. Therefore, we need to obtain the number of available CPU cores in the current machine in the C program.


To meet cross-platform requirements, we need to consider obtaining the number of machine processor cores on different platforms. Here we consider Linux and Windows. different platforms have different methods to obtain the number of processor cores.
On Windows, we can use the getsysteminfo () function to obtain some hardware and software information of the current system. One of them is the number of processors in the current machine. Use the following statement to obtain the required information: system_info Info;
Getsysteminfo (& info );
Return info. dwnumberofprocessors;

On the Linux platform, we can use sysconf () or get_nprocs () to obtain the number of processor cores. The following sections describe:

Sysconf () is provided by unistd. H. To use this function, # include <unistd. h> is required. The parameter can be _ SC _nprocessors_conf or _ SC _nprocessors_onln. Sysconf (_ SC _nprocessors_conf) returns the number of available cores in the system, but the value includes the number of disabled cores in the system. Therefore, this value does not represent the number of available cores in the current system. The return value of sysconf (_ SC _nprocessors_onln) actually represents the number of available cores of the system.

The gnu c Library provides another method to obtain the number of available machine cores. The intget_nprocs_conf (void) function and INT get_nprocs (void) are defined in sys/sysinfo. H. These two functions can be used to obtain the number of machine cores. The returned value of get_nprocs_conf (void) is similar to that of sysconf (_ SC _nprocessors_conf), but the number of available cores of the table name is not true. The returned value of get_nprocs (void) is similar to that of sysconf, actually reflects the number of available cores.


Cross-platform Functions

The function for cross-platform acquisition of the current number of available cores of the system is as follows:

Int_t get_cpu_core_num () {# If defined (win32) system_info Info; getsysteminfo (& info); Return info. dwnumberofprocessors; # Elif defined (Linux) | defined (Solaris) | defined (Aix) return get_nprocs (); // GNU fuction # else # operating system not supported by Error # endif}
Summary

Obtaining the number of available cores of the system is a common method for determining the degree of parallelism in parallel computing. Through the small functions in this article, you can learn how to obtain the number of available cores.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.