zoukankan      html  css  js  c++  java
  • [apue] sysconf 的四种返回状态

    众所周知,sysconf 用来返回某种常量的定义或者资源的上限,前者用于应用动态的判断系统是否支持某种标准或能力、后者用于决定资源分配的尺寸。
    但是你可能不知道的是,sysconf 可以返回四种状态:

    • 常量定义本身或资源上限 (>=0, 整型值)
    • 无限制 (no limit)
    • 不支持
    • 出错

    那一个小小的 int 返回类型,如何能容纳这许多含义? 各位看过下面这段代码,就一目了然了:

    static void 
    pr_sysconf (char *msg, int name) 
    {
      long val; 
      fputs (msg, stdout); 
      errno = 0; 
      if ((val = sysconf (name)) < 0) { 
        if (errno != 0) { 
          if (errno == EINVAL)
            fputs ("(not supported)
    ", stdout); 
          else 
            err_sys ("sysconf error"); 
        }
        else
          fputs ("(no limit)
    ", stdout); 
      }
      else
        printf ("%ld
    ", val); 
    }
    

    conf.c


    这段代码用来打印 sysconf 的返回值,可以看到基本是通过 '返回值 + errno' 的方式实现的:

    • 返回值 >= 0: 常量定义或资源本身
    • 返回值 < 0:
      • errno == 0: 无限制
      • errno != 0:
        • errno == EINVAL: 不支持
        • 其它:出错

    其实看下 sysconf 的手册页的话,确实是这么说的:

    RETURN VALUE
    If name is invalid, -1 is returned, and errno is set to EINVAL. Otherwise, the value returned is the value of the system resource
    and errno is not changed. In the case of options, a positive value is returned if a queried option is available, and -1 if it is
    not. In the case of limits, -1 means that there is no definite limit.

  • 相关阅读:
    网上图书商城项目学习笔记-001工具类测试
    SpringMVC,MyBatis商品的增删改查
    3.Spring-用反射模拟IoC
    2.Sprng-IoC-Java反射例子
    1.Spring IoC简单例子
    HTML5 文件API(二)
    HTML5 文件API(一)
    HTML5 编辑 API 之 Range 对象(二)
    hdu 5384 Danganronpa
    hdu 3065 病毒侵袭持续中
  • 原文地址:https://www.cnblogs.com/goodcitizen/p/13100669.html
Copyright © 2011-2022 走看看