我已经阅读了手册页writev并找到了 ERRORS 部分指出
EINVAL ...向量计数
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;
}
Run Code Online (Sandbox Code Playgroud)
这是我在一些手册页中找到的内容:
POSIX.1-2001 允许实现对可以在 iov 中传递的项目数量进行限制。实现可以通过在 <limits.h> 中定义 IOV_MAX 或在运行时通过来自 sysconf(_SC_IOV_MAX) 的返回值来通告其限制。在 Linux 上,这些机制公布的限制是 1024,这是真正的内核限制。但是,如果 glibc 包装函数检测到底层内核系统调用因超出此限制而失败,则它们会做一些额外的工作。