In scatter and gather (i.e. readv and writev), Linux reads into multiple buffers and writes from multiple buffers.
If say, I have a vector of 3 buffers, I can use readv, OR I can use a single buffer, which is of combined size of 3 buffers and do fread.
Hence, I am confused: For which cases should scatter/gather be used and when should a single large buffer be used?
The main convenience offered by
readv,writevis:writev, all the elements in the vector will be written in one contiguous operation, and writes done by other processes will not occur in between them.e.g. say, your data is naturally segmented, and comes from different sources:
Now, all three ‘buffers’ are not one big contiguous block. But you want to write them contiguously into a file, for whatever reason (say for example, they are fields in a file header for a file format).
If you use
writeyou have to choose between:memcpy(overhead), followed by a singlewritecall. Then the write will be atomic.write(overhead). Also,writecalls from other processes can intersperse between these writes (not atomic).If you use
writevinstead, its all good:memcpyto make a single buffer from the three.So you would do something like:
Sources: