On success, readv()andwritev()return the number of bytes read or written, respectively. This number should be the sum of allcount iov_lenvalues. On error, the system calls return-1, and seterrnoas appropriate. These system calls can experience any of the errors of theread()andwrite()system calls, and will, upon receiving such errors, set the sameerrnocodes. In addition, the standards define two other error situations. First, because the return type is anssize_t, if the sum of allcount iov_lenvalues is greater thanSSIZE_MAX, no data will be transferred,-1will be returned, anderrnowill be set toEINVAL. Second, POSIX dictates thatcountmust be larger than zero, and less than or equal toIOV_MAX, which is defined in<limits.h>. In Linux,IOV_MAXis currently1024. Ifcountis0, the system calls return0.* Ifcountis greater thanIOV_MAX, no data is transferred, the calls return-1, anderrnois set toEINVAL. Optimizing the Count
writev( ) example Let’s consider a simple example that writes out a vector of three segments, each containing a string of a different size. This self-contained program is complete enough to demonstrate writev(), yet simple enough to serve as a useful code snippet: #include <stdio.h> int main () char *buf[] = { fd = open ("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC); /* fill out three iovec structures */ /* with a single call, write them all out */ if (close (fd)) { return 0; Running the program produces the desired result: $ ./writev As does reading the file: $ cat buccaneer.txt
blog comments powered by Disqus |
|
|
|
|
|
|
|