Like Tree1Likes
  • 1 Post By Nominal Animal

How can my program find out how many cores/threads a computer has?

This is a discussion on How can my program find out how many cores/threads a computer has? within the C Programming forums, part of the General Programming Boards category; I want to make a program that checks the computer and finds out how many threads it can process simultaneously ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    27

    How can my program find out how many cores/threads a computer has?

    I want to make a program that checks the computer and finds out how many threads it can process simultaneously and use that information to alter my program so that it processes the maximum number of threads allowed. Thank you

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    What operating system? This task is platform dependent, so you will want to use OS provided libraries.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    I was hoping to be able to code for all of them

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Quote Originally Posted by Once-ler2 View Post
    I was hoping to be able to code for all of them
    That's unrealistic. You can certainly account for a subset of common operating systems using conditional compilation, but recognizing all of them is a broad and moving target.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Once-ler2 View Post
    I was hoping to be able to code for all of them
    You'll need to use some logic to separate Linux, OSX, and Windows systems. That's not hard to do. Each OS will tell your program if it's the operating system.

    I'm going to skip over that for the time being, and show one simple way to get the info you want, on a Windows OS. Just a caution: this may not be the easiest way to do this, and may not work with other versions of Windows before Windows 7.
    Code:
    system("WMIC CPU Get /Format:List >sysInfo.txt");
    [Which I googled from http://superuser.com/questions/22655...-on-windows-7]

    Will put this info, into a file named sysInfo.txt. Now just open that file, and read in each line of text with fgets(), until you reach "Number of logical Processors: "

    And that is the number of processes your system can run simultaneously. i.e. an i7 may have 4 cores, but 8 logical processors.

    Example of file:

    AddressWidth=64
    Architecture=9
    Availability=3
    Caption=Intel64 Family 6 Model 26 Stepping 4
    ConfigManagerErrorCode=
    ConfigManagerUserConfig=
    CpuStatus=1
    CreationClassName=Win32_Processor
    CurrentClockSpeed=3500
    CurrentVoltage=12
    DataWidth=64
    Description=Intel64 Family 6 Model 26 Stepping 4
    DeviceID=CPU0
    ErrorCleared=
    ErrorDescription=
    ExtClock=175
    Family=1
    InstallDate=
    L2CacheSize=1024
    L2CacheSpeed=
    L3CacheSize=8192
    L3CacheSpeed=0
    LastErrorCode=
    Level=6
    LoadPercentage=0
    Manufacturer=GenuineIntel
    MaxClockSpeed=3500
    Name=Intel(R) Core(TM) i7 CPU 940 @ 2.93GHz
    NumberOfCores=4
    NumberOfLogicalProcessors=8
    OtherFamilyDescription=
    PNPDeviceID=
    PowerManagementCapabilities=
    PowerManagementSupported=FALSE
    ProcessorId=BFEBFBFF000106A4
    ProcessorType=3
    Revision=6660
    Role=CPU
    SocketDesignation=LGA1366
    Status=OK
    StatusInfo=3
    Stepping=
    SystemCreationClassName=Win32_ComputerSystem
    SystemName=ADAK-NV
    UniqueId=
    UpgradeMethod=1
    Version=
    VoltageCaps=
    Last edited by Adak; 06-30-2013 at 04:14 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You will have to be in the PATH on your system, or you'll get a "WMIC is not recognized as an internal or external command" error.

    One easy fix is to run it from the C:\Users\Your User Name
    directory. That one is in the PATH, by default.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    868
    Quote Originally Posted by Once-ler2 View Post
    I want to make a program that checks the computer and finds out how many threads it can process simultaneously and use that information to alter my program so that it processes the maximum number of threads allowed. Thank you
    The most important thing is that you let the user easily configure the number of threads used. A lot of us have configurations where that number of threads is definitely not the optimal choice.

    In FreeBSD, you can use the sysctl -a command to gather CPU information. See the lines that start with hw.

    In Linux, you can read the /proc/cpuinfo pseudofile (see e.g. here), or the lscpu command to gather CPU information.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C & MPI, Number of computer threads
    By Nooby 1 Kenooby in forum C Programming
    Replies: 3
    Last Post: 11-05-2012, 03:50 AM
  2. Replies: 13
    Last Post: 05-22-2009, 11:37 AM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 01:25 PM
  4. how to find the computer name
    By Sephiroth222 in forum C++ Programming
    Replies: 9
    Last Post: 08-13-2005, 01:53 PM
  5. How can I find the default software on a computer?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-16-2002, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21