neuront
neuront

Reputation: 9612

c - The permitted maximum of the "iovcnt" argument in "writev"?

I've read man page for writev and found the ERRORS section notes that

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

But how could I get the max value?

PS: On my OS (Ubuntu 14.04 x64) it seems to be 1024. I check it via the following code

#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;
}

Upvotes: 5

Views: 2228

Answers (1)

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36401

This is what I found in some manual page :

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.

Upvotes: 7

Related Questions