gpt4 book ai didi

c - "iovcnt"中 "writev"参数的允许最大值?

转载 作者:行者123 更新时间:2023-12-04 09:14:00 25 4
gpt4 key购买 nike

我已经阅读了 writev 的手册页并发现 ERRORS 部分指出

EINVAL ... the vector count iovcnt is less than zero or greater than the permitted maximum.



但是我怎么能得到最大值呢?

PS:在我的操作系统(Ubuntu 14.04 x64)上它似乎是 1024。我通过以下代码检查它
#include <stdlib.h>
#include <fcntl.h>
#include <sys/uio.h>

char content[4000];
struct iovec vec[4000];

int main()
{
int n, i;
// int cnt = 1024; // OK
int cnt = 1025; // writev error
int fd = open("tmp.txt", O_WRONLY);

if (fd == -1) {
perror("open");
exit(1);
}

for (i = 0; i < cnt; ++i) {
content[i] = 'a' + i % 26;
vec[i].iov_base = content + i;
vec[i].iov_len = 1;
}

n = writev(fd, vec, cnt);
if (n == -1) {
perror("writev");
exit(1);
}
return 0;
}

最佳答案

这是我在一些手册页中找到的内容:

POSIX.1-2001 allows an implementation to place a limit on the number of items that can be passed in iov. An implementation can advertise its limit by defining IOV_MAX in <limits.h> or at run time via the return value from sysconf(_SC_IOV_MAX). On Linux, the limit advertised by these mechanisms is 1024, which is the true kernel limit. However, the glibc wrapper functions do some extra work if they detect that the underlying kernel system call failed because this limit was exceeded.

关于c - "iovcnt"中 "writev"参数的允许最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27271801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com