Chinaunix

标题: sysconf和pathconf的区别?不理解测试结果 [打印本页]

作者: c_fanatic    时间: 2008-06-11 20:49
标题: sysconf和pathconf的区别?不理解测试结果
学习APUE第二章时,在Fedora8测试了sysconf和pathconf的代码,对结果不理解,请解释:

#ifdef        PATH_MAX
static int        pathmax = PATH_MAX;
#else
static int        pathmax = 0;
#endif

int main(){
   char        *ptr;

  if (pathmax == 0) {
      errno = 0;
      if ( (pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {  //pathconf函数
         if (errno == 0)
        pathmax = PATH_MAX_GUESS;
         else
        err_sys("pathconf error for _PC_PATH_MAX");
      } else
         pathmax++;       
  }
    printf("pathmax=%d\n", pathmax);   //这里打印4096. 单位是byte吗?

   printf("_PC_PATH_MAX=%ld\n", sysconf(_PC_PATH_MAX));  //这里打印1024,为什么?

}

我觉得它们的值应该是一样的,但是为什么不一样呢?该如何解释????
作者: figofuture    时间: 2008-06-11 20:57
1W = 4B?
1024W = 4096B?
作者: c_fanatic    时间: 2008-06-11 21:14
标题: 回复 #2 figofuture 的帖子
你的意思是sysconf得出来的值的单位是字(word)?而pathconf得出来的单位是byte?????


还有,如果把pathmax直接写成如下(即去掉预定义)
static int        pathmax = 0;
且pathconf的第一个参数为工作目录(或其他目录),结果都是4096.
感觉和man上说的不太一样:

_PC_PATH_MAX
  returns the maximum length of a relative pathname when path
is the current working directory.

我的理解是如果第一个参数是当前工作路径,那么返回的值应该是相对于该路径而言,即
比相对于根路径要小----即是说要比上面的程序得到的要小。

难道我理解有错? 请指出。
作者: figofuture    时间: 2008-06-11 22:48
_PCXXX: path configuration
这个宏可以用作sysconf的参数吗?你查一下,可能描述的意义不一样!
作者: figofuture    时间: 2008-06-11 23:08
Some values, such as {PATH_MAX}, are sometimes so large that they must not be used to, say, allocate arrays. The sysconf() function returns a negative value to show that this symbolic constant is not even defined in this case.摘自susv3
_PC_PATH_MAX这个宏定义适用于作为pathconf的参数,但是否同样适用于sysconf呢?我不这么认为,同样的数字在作为不同系统调用的参数时,意义可能会一样,但不是任何时候都一样。
作者: soul_of_moon    时间: 2008-06-12 10:49
原帖由 c_fanatic 于 2008-6-11 20:49 发表
学习APUE第二章时,在Fedora8测试了sysconf和pathconf的代码,对结果不理解,请解释:

#ifdef        PATH_MAX
static int        pathmax = PATH_MAX;
#else
static int        pathmax = 0;
#endif

int main(){
   char ...



加上这段代码:
printf("_PC_PATH_MAX = %d\n", _PC_PATH_MAX);
        printf("_SC_OPEN_MAX = %d\n", _SC_OPEN_MAX);
       
   printf("_PC_PATH_MAX=%ld\n", sysconf(_SC_OPEN_MAX));

另外man sysconf




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2