Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7931437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:43:08+00:00 2026-06-03T20:43:08+00:00

In scatter and gather (i.e. readv and writev ), Linux reads into multiple buffers

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:43:09+00:00Added an answer on June 3, 2026 at 8:43 pm

    The main convenience offered by readv, writev is:

    1. It allows working with non contiguous blocks of data. i.e. buffers need not be part of an array, but separately allocated.
    2. The I/O is ‘atomic’. i.e. If you do a 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:

    1. Copying them over into one block of memory using, say, memcpy (overhead), followed by a single write call. Then the write will be atomic.
    2. Making three separate calls to write (overhead). Also, write calls from other processes can intersperse between these writes (not atomic).

    If you use writev instead, its all good:

    1. You make exactly one system call, and no memcpy to make a single buffer from the three.
    2. Also, the three buffers are written atomically, as one block write. i.e. if other processes also write, then these writes will not come in between the writes of the three vectors.

    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:

    1. http://pubs.opengroup.org/onlinepubs/009604499/functions/writev.html
    2. http://linux.die.net/man/2/readv
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scatter plot in R (with ggplot2). The data has a numeric
In a 3D scatter graph of MATLAB I have 15 different clusters of data
I have a scatter plot with 10 points, some points are above and below
I want to fix the color range on multiple scatter plots and add in
I have created a scatter graph with three plot spaces. One for two y-axis
i have a scatter plot graph i would like each dot to have a
Does anyone know of a good Scatter Plot/Chart component that I can use with
I want to have two scatter series on my chart. One with a linewidth
I have a layout which contains three 3 scatter views. I am looking to
I have thousands of points to Plot on a JFreeChart scatter plot. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.