我已经阅读了writev
的手册页,发现错误部分指出
英瓦尔...向量计数
iovcnt
小于零或大于允许的最大值。
但是我怎样才能获得最大值呢?
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 允许实现对数量进行限制 之 可以在 IOV 中传递的项目。 实现可以通告其 通过在
中定义IOV_MAX或在运行时通过 return 定义 来自 sysconf(_SC_IOV_MAX) 的值。 在 Linux 上,公布的限制由 这些机制是 1024,这是真正的内核限制。 然而, glibc 包装器函数如果检测到 基础内核系统调用失败,因为已超出此限制。