Now, let’s consider an example program that uses the readv() system call to read from the previously generated text file using vectored I/O. This self-contained example is likewise simple yet complete: #include <stdio.h int main () fd = open ("buccaneer.txt", O_RDONLY); /* set up our iovec structures */ /* read into the structures with a single call */ for (i = 0; i < 3; i++) if (close (fd)) { return 0; Running this program after running the previous program produces the following results: $ ./readv A naïve implementation of readv() andwritev()could be done in user space as a simple loop, something similar to the following: #include <unistd.h> ssize_t naive_writev (int fd, const struct iovec *iov, int count) for (i = 0; i < count; i++) { nr = write (fd, iov[i].iov_base, iov[i].iov_len); } return ret; Thankfully, this is not the Linux implementation: Linux implementsreadv()andwritev()as system calls, and internally performs scatter/gather I/O. In fact, all I/O inside the Linux kernel is vectored;read()andwrite()are implemented as vectored I/O with a vector of only one segment. Please check back next week for the continuation of this article.
blog comments powered by Disqus |
|
|
|
|
|
|
|