SORU
9 EKİM 2009, Cuma


Nasıl CPU Çekirdek Sayısını) bulmak için .NET/C#?

Bir yol üzerinden.NET/C#CPU çekirdek sayısını bulmak için?

PS Bu "kullanmalıyım Çoklu-işlem" soru!? değil düz bir kod bir soru. :-)

CEVAP
19 NİSAN 2010, PAZARTESİ


Bilgi alabileceğin işlemci ile ilgili birkaç farklı parça vardır:

  1. Fiziksel işlemci sayısı
  2. Çekirdek sayısı
  3. Mantıksal işlemci sayısı.

Bu farklı olabilir; dual-core hyper-etkin 2 işlemcili bir makine durumda, 2 fiziksel işlemci, 4 çekirdek, 8 mantıksal işlemci var.

Numarasını mantıksal işlemciler aracılığıyla kullanılabilir Environment sınıf, ancak diğer bilgileri yalnızca içinden WMI (ve varsa yüklemek bazı hotfixes or service packs etmek için bazı sistemler):

Fiziksel İşlemci:

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
    Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);
}

Çekirdek:

int coreCount = 0;
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
{
    coreCount  = int.Parse(item["NumberOfCores"].ToString());
}
Console.WriteLine("Number Of Cores: {0}", coreCount);

Mantıksal İşlemci:

Console.WriteLine("Number Of Logical Processors: {0}", Environment.ProcessorCount);

YA

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
    Console.WriteLine("Number Of Logical Processors: {0}", item["NumberOfLogicalProcessors"]);
}

İşlemciler Windows: hariç

Ayrıca Windows API çağrılarını kullanabilirsinizsetupapi.dllWindows (boot ayarları gibi) dışında bırakılmıştır işlemci keşfetmek ve algılanabilir yukarıdaki araçları kullanarak değil. Aşağıdaki kod, varolan mantıksal işlemci (fiziksel ve mantıksal işlemcileri ayırt etmek için nasıl anlamaya mümkün olmamıştır.) toplam sayısı, Windows dahil edilmiş olanlar da dahil olmak üzere verir:

static void Main(string[] args)
{
    int deviceCount = 0;
    IntPtr deviceList = IntPtr.Zero;
    // GUID for processor classid
    Guid processorGuid = new Guid("{50127dc3-0f36-415e-a6cc-4cb3be910b65}");

    try
    {
        // get a list of all processor devices
        deviceList = SetupDiGetClassDevs(ref processorGuid, "ACPI", IntPtr.Zero, (int)DIGCF.PRESENT);
        // attempt to process each item in the list
        for (int deviceNumber = 0; ; deviceNumber  )
        {
            SP_DEVINFO_DATA deviceInfo = new SP_DEVINFO_DATA();
            deviceInfo.cbSize = Marshal.SizeOf(deviceInfo);

            // attempt to read the device info from the list, if this fails, we're at the end of the list
            if (!SetupDiEnumDeviceInfo(deviceList, deviceNumber, ref deviceInfo))
            {
                deviceCount = deviceNumber - 1;
                break;
            }
        }
    }
    finally
    {
        if (deviceList != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceList); }
    }
    Console.WriteLine("Number of cores: {0}", deviceCount);
}

[DllImport("setupapi.dll", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,
    [MarshalAs(UnmanagedType.LPStr)]String enumerator,
    IntPtr hwndParent,
    Int32 Flags);

[DllImport("setupapi.dll", SetLastError = true)]
private static extern Int32 SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet,
    Int32 MemberIndex,
    ref SP_DEVINFO_DATA DeviceInterfaceData);

[StructLayout(LayoutKind.Sequential)]
private struct SP_DEVINFO_DATA
{
    public int cbSize;
    public Guid ClassGuid;
    public uint DevInst;
    public IntPtr Reserved;
}

private enum DIGCF
{
    DEFAULT = 0x1,
    PRESENT = 0x2,
    ALLCLASSES = 0x4,
    PROFILE = 0x8,
    DEVICEINTERFACE = 0x10,
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • DigitalRev TV

    DigitalRev T

    30 AĞUSTOS 2007
  • Pocketnow

    Pocketnow

    14 EKİM 2007
  • Sarah's YouTube Channel

    Sarah's YouT

    27 Temmuz 2009