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里的东西。
[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里的东西。
相关文章推荐
- 聊聊Linux动态链接中的PLT和GOT(3)——公共GOT表项
- Linux 基础知识(三)
- linux 切换c++版本
- 1、Linux文本处理基础命令
- ubuntu-14.04.3-server-amd64下源码安装mysql-5.6.27-linux-glibc2.5-x86_64
- 权限管理[Linux]
- 文件管理[Linux]
- Centos中部署WordPress无法更新问题
- Linux下定时执行任务的方法
- Linux系统修改编码
- Linux进程同步之POSIX信号量(非原创)
- informix数据库Linux/Unix下安装配置
- Linux常用命令
- Linux下安装配置JDK
- Linux下vim的使用
- Windows下体验Linux Ubuntu系统&&安装JDK,Android SDK,Android Studio
- 【Linux】简单实现进度条
- 【Linux】文件查找:find
- 【Linux】vim的配置
- 【Linux】make的工作原理和makefile文件