Readv/writev function __ Function

Source: Internet
Author: User

Name:: Readv/writev
Function: Scatter read/gather write
Usage: #include <sys/uio.h>
Function prototype:
ssize_t readv (int filedes,const struct iovec*iov,int iovcnt);
ssize_t Writev (int filedes,const struct iovec*iov,int iovcnt);
Parameters:
Filedes File Descriptor

Iov A pointer to an array of IOVEC structures.

Number of iovcnt array elements

return value:
If successful, returns the number of bytes read and written, and returns 1 if an error occurs
The READV and Writev functions are used to read and write multiple noncontiguous buffers in a single function call. These two functions are sometimes also distributed as read and aggregated.

The second parameter of the two functions is a pointer to an array of IOVEC structures:

struct iovec{

void *iov_base;

size_t Iov_len;

};

Writev aggregates output data from the buffer in order Iov[0] to iov[iovcnt-1]. Writev returns the total number of bytes in the output, which should normally be equal to the sum of all buffer lengths.

Readv reads the read data into the buffer in the same order as above. Readv always fills a buffer first and then fills in the next one. READV returns the total number of bytes read. Returns 0 if the end of the file is encountered and no data is readable.

Here is an example of reading multiple buffers:

#include <sys/uio.h>

#include <stdio.h>

#include <fcntl.h>

int main (int argc,char *argv[])

{

ssize_t size;

Char buf1[9];

Char buf2[9];

struct Iovec iov[2];

Fd1=open (argv[1],o_rdonly);

Fd2=open (argv[2],o_rdonly);

Fd3=open (argv[3],o_wronly);

Size=read (fd1,buf1,sizeof (BUF1));

printf ("%s size is:%d\n", argv[1],size);

Size=read (fd2,buf2,sizeof (BUF2));

printf ("%s size is:%d\n", argv[2],size);

IOV[0].IOV_BASE=BUF1;

Iov[0].iov_len=sizeof (BUF1);

IOV[1].IOV_BASE=BUF2;

Iov[1].iov_len=sizeof (BUF2);

Size=writev (fd3,iov,2));

printf ("%s size is:%d\n", argv[3],size);

Close (FD1);

Close (FD2);

Close (FD3);

}


First three files (TEST1,TEST2,TEST3) are created with VI or cat, and test write 123456789,test write ABCDEFGHI.TEST3 null.

And then run the command:

#./12_4 test1 test2 test3

On the screen will output:

Test1 size Is:9

Test2 size Is:9

Test3 size is:18

Open Test3, the file content is 123456789abcdefghi.

The program first reads the contents of Test1 and test2 into buffer buf1 and BUF2 respectively. Then write the contents of BUF1 and Buf2 to Test3.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.