linux 系统调用sysconf函数使用

在看开源代码的时候,尤其是获取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里的东西。







  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值