2014-12-03 30 views
1

我讀過手冊頁writev,發現錯誤部分指出,c - 「writev」中允許的最大「iovcnt」參數?

EINVAL ...矢量計數iovcnt大於允許的最大大於小於零或

但我怎麼能得到最大值?

PS:在我的OS(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; 
} 

回答

3

這是我在一些手冊中找到:

POSIX.1-2001允許實施限制 可以通過iov傳遞的項目。通過在<limits.h>中定義IOV_MAX,或者在運行時通過從sysconf(_SC_IOV_MAX)返回值 的值定義IOV_MAX,實現可以通告其限制 。在Linux上,由 公佈的限制是1024,這是真正的內核限制。但是,如果glibc包裝函數檢測到底層內核系統調用因超出此限制而失敗,則會執行一些額外的工作。

+0

非常感謝。看起來'IOV_MAX'沒有在我的系統上定義(編譯錯誤),但'sysconf(_SC_IOV_MAX)'工作。 – neuront 2014-12-03 12:58:15

+1

使用'-D_XOPEN_SOURCE'編譯來獲取limits.h來定義'IOV_MAX'。 – mark4o 2014-12-03 12:59:04