转自:https://blog.csdn.net/wallwind/article/details/49696021
在看开源代码的时候,尤其是获取cpu核数的时候,发现了一个很好用的一个函数
#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核数,内存大小,一个进程大打开文件的大小
使用下面的一个实例,看一下我的电脑的一些配置信息
#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;
}
输出信息:
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里的东西。
————————————————
版权声明:本文为CSDN博主「wintree」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wallwind/article/details/49696021