gpt4 book ai didi

c - 为什么 _SC_AIO_MAX 定义明确,而 sysconf(_SC_AIO_MAX) 返回 -1?

转载 作者:行者123 更新时间:2023-11-30 14:33:44 26 4
gpt4 key购买 nike

这是一个简单的演示:

#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 is defined unsupported _SC_AIO_MAX = 24 sysconf(_SC_AIO_MAX) = -1

现场演示:https://wandbox.org/permlink/7GDzyvEUgRwMHX95

如您所见,_SC_AIO_MAX 定义为 24,但 sysconf(_SC_AIO_MAX) 返回 -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.)

但是限制已经定义为24,为什么sysconf仍然返回-1

最佳答案

_SC_AIO_MAX = 24 不是限制的值,而是您要访问的限制的标识符。

getconf(24) == -1 表示:

  1. 有错误(检查errno以查看是否有错误);或

  2. 该限制是不确定的。

一些文档提到,在调用 getconf 之前,您应该将 errno 设置为 0,以确保您可以区分这两种情况。

(2) 可能会在功能可用但已被禁用等情况下发生。

关于c - 为什么 _SC_AIO_MAX 定义明确,而 sysconf(_SC_AIO_MAX) 返回 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59168164/

26 4 0
文章推荐: javascript - 将