您的位置:首页 > 运维架构 > Linux

linux 系统调用sysconf函数使用

2016-06-11 21:48 633 查看
在看开源代码的时候,尤其是获取cpu核数的时候,发现了一个很好用的一个函数

[cpp] view
plain copy

#include <unistd.h>  

  

      long sysconf(int name);  

通过名字可以猜到,该函数是获取一些系统的参数。然后通过man sysconf

我们可以知道该函数的使用条件,

 POSIX  allows an application to test at compile or run time whether certain options are supported, or what the value is of certain configurable

       constants or limits.

       At compile time this is done by including <unistd.h> and/or <limits.h> and testing the value of certain macros.

       At run time, one can ask for numerical values using the present function sysconf().  On can ask for numerical values that  may  depend  on  the

       file system a file is in using the calls fpathconf(3) and pathconf(3).  One can ask for string values using confstr(3).

大概 意思就是我们可以通过相关选项,来获取编译或者运行时的一些系统参数值。比如或许cpu核数,内存大小,一个进程大打开文件的大小

使用下面的一个实例,看一下我的电脑的一些配置信息

[cpp] view
plain copy

#include <stdio.h>  

#include <stdlib.h>  

#include <unistd.h>  

  

int main()  

{  

    printf("Size of a page in bytes:%ld\n",sysconf(_SC_PAGESIZE));  

    printf("Max length of a  hostname:%ld\n",sysconf(_SC_HOST_NAME_MAX));  

    printf(" The maximum number of files that a process can have open at any time.:%ld\n",sysconf(_SC_OPEN_MAX));  

    printf("  The  number  of  clock  ticks  per  second.:%ld\n",sysconf(_SC_CLK_TCK));   

    printf("The number of processors currently online .:%ld\n",sysconf(_SC_NPROCESSORS_ONLN));   

    printf("The number of processors configured..:%ld\n",sysconf(_SC_NPROCESSORS_CONF));   

    return 0;  

}  

输出信息:

[cpp] view
plain copy

Size of a page in bytes:4096  

Max length of a  hostname:64  

 The maximum number of files that a process can have open at any time.:1024  

  The  number  of  clock  ticks  per  second.:100  

The number of processors currently online .:1  

The number of processors configured..:1  

这里只列举了一点点:具体还是要看man page里的东西。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: