topics that matter; ideas worth sharing

share a tip, submit a link, add something new

Scatter/Gather I/O

June 14, 2002, 12:00 AM —  ITworld — 

The classic read() and write() sycalls enable you to handle a single
buffer at a time. The problem is that applications often need to write
various data types to a contiguous region of a file. Although you can
use multiple write() calls to accomplish that, this isn't an atomic
operation; another thread might access the same file region between
every pair of write() calls. As a workaround, you can pack all the data
into a single buffer before writing it to a file. However, the
performance overhead incurred by copying numerous memory blocks is
noticeable.

Scatter/Gather Read and Write
The readv() and writev() solve this problem easily. Instead of handling
a single data buffer at a time, they take an array of records, each of
which represents a distinct buffer. These buffers are read from or
written to in the same order that they are listed in the array. More
importantly, the writev() and readv() calls are atomic.

readv() and writev() are called "scatter and gather read and write"
because readv() scatters data from various sources across memory and
writev() gathers data from various memory addresses. These functions are
declared as follows:

int readv(int fd, const struct iovec* vec, size_t count);
int writev(int fd, const struct iovec* vec, size_t count);

The first argument is the file descriptor to be read from or written to.
The second argument is a pointer to one or more structs of type iovec,
which represents a data buffer. The third argument is the number of
iovec structs passed in vec. Struct iovec is declared as follows:

struct iovec{
void * iov_base; /*buffer's address*/
size_t iov_len; /*buffer's size in bytes*/
};

The return value of both functions is the total number of bytes read or
written.

Example
The following program gathers three buffers (strings, in this case) and
outputs them using a single writev() call. The result is displayed on
the standard output:

#include
int main
{
iovec vecs[3];
vecs[0].iov_base="first";
vecs[0].iov_len=5;

vecs[1].iov_base=" second ";
vecs[1].iov_len=8;

vecs[2].iov_base="last\n";
vecs[2].iov_len=5;
writev(1, vecs, 3); /*1 is equal to stdout*/
}

» posted by ITworld staff

ITworld

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff
Featured Sponsor

Get a broad understanding of important regulations and how you can make sure your site is in adherence.





Learn how VeriSign SGC-enabled SSL Certificates can help improve site security and customer confidence in the free white paper, "How to Offer the Strongest SSL Encryption." In this paper you will learn the differences between weak and strong encryption and what they mean for your site's performance.

Get VeriSign's free white paper: "The Latest Advancements in SSL Technology" and learn about the benefits of strong SSL encryption, Extended Validation (EV) SSL and security trust marks and what these SSL offerings can do for your site.

Now with Extended Validation (EV) SSL available from VeriSign, you can show your customers that they can trust your site. Learn about EV SSL benefits in this free VeriSign white paper.

More Resources