linux下getrlimit与sysconf函数

#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>

int main(int argc, char *argv[])
{
  struct rlimit nofile_rlmt;
  if (getrlimit(RLIMIT_NOFILE, &nofile_rlmt) != -1) {
    printf("获取进程最大能打开的文件描述符个数信息:\n"
        "rlim_cur(Soft limit):%d"
        "\nrlim_max(Hard limit (ceiling for rlim_cur)): %d\n",
        nofile_rlmt.rlim_cur, nofile_rlmt.rlim_max);
  }
}
getrlimit与setlimit函数详解可以参照:

http://www.cnblogs.com/niocai/archive/2012/04/01/2428128.html

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>		//sysconf

#define ONE_MB (1024 * 1024)

int main(int argc, char **argv[])
{
  long num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
  printf("CUP的个数:%ld\n", num_cpu);
  
  long page_size = sysconf(_SC_PAGESIZE);
  printf("系统页面的大小:%ld K\n", page_size);
  long num_pages = sysconf(_SC_PHYS_PAGES);
  printf("系统中物理页面个数:%ld 个\n", num_pages);
  long free_pages = sysconf(_SC_AVPHYS_PAGES);
  printf("系统中可用的页面个数:%ld 个\n", free_pages);
  long long mem = (long long)((long long)num_pages * (long long)page_size);
  mem /= ONE_MB;
  long long free_mem = (long long)((long long)free_pages * (long long)page_size);
  free_mem /= ONE_MB;
  printf("总共有 %lld MB的物理内存,空闲的物理内存有 %lld MB\n", mem, free_mem);
  
  long open_max = sysconf(_SC_OPEN_MAX);
  printf("每个进程最大打开的文件个数:%ld\n", open_max);  
  
  return 0;
}
sysconf、pathconf、fpathconf函数详解参照:

http://xingyunbaijunwei.blog.163.com/blog/static/7653806720119122344450/

你可能感兴趣的:(sysconf,getrlimit,setrlimit)