阅读Darwin源码的时候看到这个方法,感觉挺有用处,且考虑了多种平台下的实现方式,直接贴代码,以后说不定会用到~

单一种平台下的实现方法可能很容易,但是把这些个系统都收集在一起,在一个函数中实现还是极好的

【转载请注明出处】:http://blog.csdn.net/longlong530

  1.  
    UInt32 OS::GetNumProcessors()
  2.  
    {
  3.  
    #if (__Win32__)
  4.  
    SYSTEM_INFO theSystemInfo;
  5.  
    ::GetSystemInfo(&theSystemInfo);
  6.  
     
  7.  
    return (UInt32)theSystemInfo.dwNumberOfProcessors;
  8.  
    #endif
  9.  
     
  10.  
    #if (__MacOSX__ || __FreeBSD__)
  11.  
    int numCPUs = 1;
  12.  
    size_t len = sizeof(numCPUs);
  13.  
    int mib[2];
  14.  
    mib[0] = CTL_HW;
  15.  
    mib[1] = HW_NCPU;
  16.  
    (void) ::sysctl(mib,2,&numCPUs,&len,NULL,0);
  17.  
    if (numCPUs < 1)
  18.  
    numCPUs = 1;
  19.  
    return (UInt32) numCPUs;
  20.  
    #endif
  21.  
     
  22.  
    #if(__linux__ || __linuxppc__)
  23.  
     
  24.  
    char cpuBuffer[8192] = "";
  25.  
    StrPtrLen cpuInfoBuf(cpuBuffer, sizeof(cpuBuffer));
  26.  
    FILE *cpuFile = ::fopen( "/proc/cpuinfo", "r" );
  27.  
    if (cpuFile)
  28.  
    { cpuInfoBuf.Len = ::fread(cpuInfoBuf.Ptr, sizeof(char), cpuInfoBuf.Len, cpuFile);
  29.  
    ::fclose(cpuFile);
  30.  
    }
  31.  
     
  32.  
    StringParser cpuInfoFileParser(&cpuInfoBuf);
  33.  
    StrPtrLen line;
  34.  
    StrPtrLen word;
  35.  
    UInt32 numCPUs = 0;
  36.  
     
  37.  
    while( cpuInfoFileParser.GetDataRemaining() != 0 )
  38.  
    {
  39.  
    cpuInfoFileParser.GetThruEOL(&line); // Read each line
  40.  
    StringParser lineParser(&line);
  41.  
    lineParser.ConsumeWhitespace(); //skip over leading whitespace
  42.  
     
  43.  
    if (lineParser.GetDataRemaining() == 0) // must be an empty line
  44.  
    continue;
  45.  
     
  46.  
    lineParser.ConsumeUntilWhitespace(&word);
  47.  
     
  48.  
    if ( word.Equal("processor") ) // found a processor as first word in line
  49.  
    { numCPUs ++;
  50.  
    }
  51.  
    }
  52.  
     
  53.  
    if (numCPUs == 0)
  54.  
    numCPUs = 1;
  55.  
     
  56.  
    return numCPUs;
  57.  
    #endif
  58.  
     
  59.  
    #if(__solaris__)
  60.  
    {
  61.  
    UInt32 numCPUs = 0;
  62.  
    char linebuff[512] = "";
  63.  
    StrPtrLen line(linebuff, sizeof(linebuff));
  64.  
    StrPtrLen word;
  65.  
     
  66.  
    FILE *p = ::popen("uname -X","r");
  67.  
    while((::fgets(linebuff, sizeof(linebuff -1), p)) > 0)
  68.  
    {
  69.  
    StringParser lineParser(&line);
  70.  
    lineParser.ConsumeWhitespace(); //skip over leading whitespace
  71.  
     
  72.  
    if (lineParser.GetDataRemaining() == 0) // must be an empty line
  73.  
    continue;
  74.  
     
  75.  
    lineParser.ConsumeUntilWhitespace(&word);
  76.  
     
  77.  
    if ( word.Equal("NumCPU")) // found a tag as first word in line
  78.  
    {
  79.  
    lineParser.GetThru(NULL,'=');
  80.  
    lineParser.ConsumeWhitespace(); //skip over leading whitespace
  81.  
    lineParser.ConsumeUntilWhitespace(&word); //read the number of cpus
  82.  
    if (word.Len > 0)
  83.  
    ::sscanf(word.Ptr, "%"_U32BITARG_"", &numCPUs);
  84.  
     
  85.  
    break;
  86.  
    }
  87.  
    }
  88.  
    if (numCPUs == 0)
  89.  
    numCPUs = 1;
  90.  
     
  91.  
    ::pclose(p);
  92.  
     
  93.  
    return numCPUs;
  94.  
    }
  95.  
    #endif
  96.  
     
  97.  
    #if(__sgi__)
  98.  
    UInt32 numCPUs = 0;
  99.  
     
  100.  
    numCPUs = sysconf(_SC_NPROC_ONLN);
  101.  
     
  102.  
    return numCPUs;
  103.  
    #endif
  104.  
     
  105.  
     
  106.  
    return 1;
  107.  
    }

PS:  函数摘自Darwin源码:\StreamServer\CommonUtilitiesLib\OS.cpp

【Darwin学习笔记】之获取系统处理器数量的方法的更多相关文章

  1. Jquery学习笔记:获取jquery对象的基本方法

    jquery最大的好处是将js对html页面的操作(读写)进行了封装,隔离了浏览器的差异性,并简化了操作,和提供了强大的功能. 在web页面中,所有的js操作都是围绕操作dom对象来的.而jquery ...

  2. Linux学习笔记(七) 查询系统

    1.查看命令 (1)man 可以使用 man 命令名称 命令查看某个命令的详细用法,其显示的内容如下: NAME:命令名称 SYNOPSIS:语法 DESCRIPTION:说明 OPTIONS:选项 ...

  3. openresty 学习笔记二:获取请求数据

    openresty 学习笔记二:获取请求数据 openresty 获取POST或者GET的请求参数.这个是要用openresty 做接口必须要做的事情.这里分几种类型:GET,POST(urlenco ...

  4. C#获取CPU处理器核心数量的方法_C#教程

    https://yq.aliyun.com/ziliao/89096 摘要: 本文讲的是C#获取CPU处理器核心数量的方法_C#教程, 有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量.核 ...

  5. Android获取系统时间的多种方法

    Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现. 现总结如下: 方法一: ? 1 2 3 4 5 ...

  6. vue学习笔记之:为何data是一个方法

    vue学习笔记之:为何data是一个方法 在vue开发中,我们可以发现,data中的属性值是在function中return出来的.可为何data必须是一个函数呢?我们先看官方的解释: 当一个组件被定 ...

  7. WPF 获取系统 DPI 的多种方法

    原文:WPF 获取系统 DPI 的多种方法 WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 ...

  8. [学习笔记] Numpy基础 系统学习

    [学习笔记] Numpy基础 上专业选修<数据分析程序设计>课程,老师串讲了Numpy基础,边听边用jupyter敲了下--理解+笔记. 老师讲的很全很系统,有些点没有记录,在PPT里就不 ...

  9. SAS学习笔记59 OPTIONS系统选项

    带VALUE选项的OPTIONS过程将指定选项的值.范围及该值如何设置的信息打印到日志窗口 在日志窗口打印的输出如下图所示 将GETOPTION函数作为%SYSFUNC宏函数的参数,从而获取系统选项设 ...

随机推荐

  1. Gym101986: Asia Tsukuba Regional Contest(寒假自训第12场)

    A .Secret of Chocolate Poles 题意:有黑白两种木块,黑色有1,K两种长度: 白色只有1一种长度,问满足黑白黑...白黑形式,长度为L的组合种类. 思路:直接DP即可. #i ...

  2. opencv3中SurfFeatureDetector、SurfDescriptorExtractor、BruteForceMatcher的使用

    opencv2中SurfFeatureDetector.SurfDescriptorExtractor.BruteForceMatcher在opencv3中发生了改变.具体如何完成特征点匹配呢?示例如 ...

  3. php基础-2

    php的逻辑运算 &&符号 <?php function tarcrnum() { for ($i = 0; $i <= 100; $i++) { if ($i % 2 = ...

  4. 实验吧—隐写术——WP之 我喜欢培根

    打开解题链接: 有一点点基础的同学大概都知道这是摩尔斯电码,那么我们对他进行解密: 解密后得到: MORSEnullISnullCOOLnullBUTnullBACONnullISnullCOOLER ...

  5. LeetCode – All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  6. 使用VUE搭建tab标签组件

    Vue2.0 多 Tab切换组件简单封装,满足自己简单的功能,可以直接拿去使用! 首先上效果图: 功能简单介绍: 1.支持tab切换 2.支持tab定位 3.支持tab自动化 仿React多Tab实现 ...

  7. oracle重做日志文件硬盘坏掉解决方法

    rman target/ list backup; list backup summary; 删除数据库数据文件夹下的log日志,例如/u01/app/oracle/oradata/ORCL下的所有后 ...

  8. Tomcat三种项目部署方式

    第一种:直接将项目放在webapps目录下 材料:项目文件名hello,文件hello.html如图: 访问地址: 注释:hello  项目文件名 hello.html 访问的文件名字 第二种:修改c ...

  9. 几个OOD概念

    Composition vs. Aggregation Composition和Aggregation都是”包含”的关系 (part of, made up of) ,不同的是生命周期.对于Compo ...

  10. 【KiCad】 如何给元件给元件的管脚加上划线?

    如何给元件给元件的管脚加上划线? 在一线元件需要注明一些引脚是低电位使能的. 比如这样. 每款 EDA 软件有不同的做法,有的是在前后使用 /,有的是给每个字母加上 /. KiCad 不一样,使用的是 ...