Techniques for handling short reads/writes with scatter-gather?

后端 未结 3 1335
独厮守ぢ
独厮守ぢ 2021-02-06 01:28

Scatter-gather - readv()/writev()/preadv()/pwritev() - reads/writes a variable number of iovec structs in a single system call. Basically it reads/write each buffer sequentiall

相关标签:
3条回答
  • 2021-02-06 01:40

    AFAICS the vectored read/write functions work the same wrt short reads/writes as the normal ones. That is, you get back the number of bytes read/written, but this might well point into the middle of a struct, just like with read()/write(). There is no guarantee that the possible "interruption points" (for lack of a better term) coincide with the vector boundaries. So unfortunately the vectored IO functions offer no more help for dealing with short reads/writes than the normal IO functions. In fact, it's more complicated since you need to map the byte count into an IO vector element and offset within the element.

    Also note that the idea of using vectored IO for individual structs or data items might not work that well; the max allowed value for the iovcnt argument (IOV_MAX) is usually quite small, something like 1024 or so. So if you data is contiguous in memory, just pass it as a single element rather than artificially splitting it up.

    0 讨论(0)
  • 2021-02-06 01:45

    Vectored write will write all the data you have provided with one call to "writev" function. So byteswritten will be always be equal to total number of bytes provided as input. this is what my understanding is.

    Please correct me if I am wrong

    0 讨论(0)
  • 2021-02-06 02:02

    Use a loop like the following to advance the partially-processed iov:

    for (;;) {
        written = writev(fd, iov+cur, count-cur);
        if (written < 0) goto error;
        while (cur < count && written >= iov[cur].iov_len)
            written -= iov[cur++].iov_len;
        if (cur == count) break;
        iov[cur].iov_base = (char *)iov[cur].iov_base + written;
        iov[cur].iov_len -= written;
    }
    

    Note that if you don't check for cur < count you will read past the end of iov which might contain zero.

    0 讨论(0)
提交回复
热议问题