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 readv() system call reads iovcnt buffers from the file associated with the file descriptor fd into the buffers described by iov ("scatter input"). The writev() system call writes iovcnt buffers of data described by iov to the file associated with the file descriptor fd ("gather output").
Scatter/gather mappings are a special type of streaming DMA mapping where one can transfer several buffer regions in a single shot, instead of mapping each buffer individually and transferring them one by one.
Data Type: struct iovec. The iovec structure describes a buffer. It contains two fields: void *iov_base. Contains the address of a buffer.
The readv() function reads data from a file or a socket with descriptor fs and stores it in a set of buffers. The data is scattered into the buffers specified by iov[0]… iov[iovcnt-1]. Parameter Description fs. The file or socket descriptor.
The main convenience offered by readv
, writev
is:
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:
struct foo *my_foo; struct bar *my_bar; struct baz *my_baz; my_foo = get_my_foo(); my_bar = get_my_bar(); my_baz = get_my_baz();
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 write
you have to choose between:
memcpy
(overhead), followed by a single write
call. Then the write will be atomic.write
(overhead). Also, write
calls from other processes can intersperse between these writes (not atomic).If you use writev
instead, its all good:
memcpy
to make a single buffer from the three.So you would do something like:
struct iovec iov[3]; iov[0].iov_base = my_foo; iov[0].iov_len = sizeof (struct foo); iov[1].iov_base = my_bar; iov[1].iov_len = sizeof (struct bar); iov[2].iov_base = my_baz; iov[2].iov_len = sizeof (struct baz); bytes_written = writev (fd, iov, 3);
Sources:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With