sysconf函数

sysconf 函数用来获取系统执行的配置信息。例如页大小、最大页数、cpu个数、打开句柄的最大个数等等。
函数原型
#include 
       long sysconf(int name);
以下为常用的name参数:
_SC_CHILD_MAX:每个user可同时运行的最大进程数
_SC_HOST_NAME_MAX:hostname最大长度,需小于_POSIX_HOST_NAME_MAX (255)
_SC_OPEN_MAX:一个进程可同时打开的文件最大数
_SC_PAGESIZE:一个page的大小,单位byte
_SC_PHYS_PAGES:物理内存总page数
_SC_AVPHYS_PAGES:当前可获得的物理内存page数
_SC_NPROCESSORS_CONF:配置的处理器个数
_SC_NPROCESSORS_ONLN:当前可获得的处理器个数
_SC_CLK_TCK:每秒对应的时钟tick数
实例程序
#include 
#include 

#define ONE_MB (1024 * 1024)

int main()
{
    printf("The number of processors configured is :%ld\n",
        sysconf(_SC_NPROCESSORS_CONF));
    printf("The number of processors currently online (available) is :%ld\n",
        sysconf(_SC_NPROCESSORS_ONLN));
    printf ("The pagesize: %ld\n", sysconf(_SC_PAGESIZE));  
    printf ("The number of pages: %ld\n", sysconf(_SC_PHYS_PAGES));  
    printf ("The number of available pages: %ld\n", sysconf(_SC_AVPHYS_PAGES)); 
    printf ("The memory size: %lld MB\n", 
        (long long)sysconf(_SC_PAGESIZE) * (long long)sysconf(_SC_PHYS_PAGES) / ONE_MB );  
    printf ("The number of files max opened:: %ld\n", sysconf(_SC_OPEN_MAX));  
    printf("The number of ticks per second: %ld\n", sysconf(_SC_CLK_TCK));  
    printf ("The max length of host name: %ld\n", sysconf(_SC_HOST_NAME_MAX));  
    printf ("The max number of simultaneous processes per user: %ld\n", sysconf(_SC_CHILD_MAX)); 
    return 0;
}
使用的机器为4核,内存2G的虚拟机,运行结果如下:
sysconf函数_第1张图片


你可能感兴趣的:(C/C++)