这是一个简单的演示:
#include <sys/stat.h>
#include <aio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
#ifdef _SC_AIO_MAX
printf("_SC_AIO_MAX is defined\n");
if (sysconf(_SC_AIO_MAX) == -1)
{
printf("unsupported\n");
printf("_SC_AIO_MAX = %d\n", _SC_AIO_MAX);
printf("sysconf(_SC_AIO_MAX) = %d\n", sysconf(_SC_AIO_MAX));
}
#else
printf("_SC_AIO_MAX is undefined\n");
#endif
return 0;
}
输出:
_SC_AIO_MAX已定义 不支持 _SC_AIO_MAX = 24 sysconf(_SC_AIO_MAX)= -1
实时演示:https://wandbox.org/permlink/7GDzyvEUgRwMHX95
如您所见,_SC_AIO_MAX
被定义为24
,但是sysconf(_SC_AIO_MAX)
返回-1
。
根据{{1}}
man 3 sysconf
但是限制已定义为 * If name corresponds to a maximum or minimum limit, and that limit is indeterminate, -1 is re‐
turned and errno is not changed. (To distinguish an indeterminate limit from an error, set er‐
rno to zero before the call, and then check whether errno is nonzero when -1 is returned.)
,为什么sysconf仍返回24
?
答案 0 :(得分:1)
_SC_AIO_MAX = 24
不是限制的值,它是您要访问的限制的标识符。
getconf(24) == -1
的意思是:
有错误(检查errno
以查看是否有错误);或
该限制是不确定的。
某些文档提到,应在调用errno
之前将0
设置为getconf
,以确保可以区分这两种情况。
(2)可能在功能可用但已被禁用等时发生。