Reputation: 2489
Is it possible to read from a file at different offsets in one system call (with out seeks inbetween), like windows overlapped IO?
Upvotes: 2
Views: 1992
Reputation: 1294
The readv
and writev
system calls are the "scatter read" and "gather write" calls and if I understand your question do what you want. The man page is short but should be sufficient.
Edit: I misread the question, as pointed out in the comments. What is being asked for looks more like preadv
, which (along with its parallel pwritev
) was introduced in Linux 2.6.30 and is described at http://lwn.net/Articles/164887/
Further edit: I previously thought that the iovec extensions made it into preadv
/pwritev
, but they didn't: see http://www.kernel.org/doc/man-pages/online/pages/man2/readv.2.html
Upvotes: 1
Reputation: 42448
You cannot do it in a single system call, but you can do it without seeking* by using the pread(2)
system call. This system call takes an offset from the start of the file of where to read the data from. This allows you to read from a file descriptor without altering the current offset. lseek(2)
will change the current offset.
* I am differentiating between a disk seek and an OS seek. Reading consecutive blocks in a file may or may not result in a disk seek. An OS seek is changing the current offset of a file for the next read/write position.
Upvotes: 2
Reputation: 881303
Your question doesn't make sense. Windows overlapped I/O is simply an asynchronous method for reading (in this case) the file. It allows your thread to do something else while waiting for the I/O to complete.
You cannot use it to read multiple sections of a file without intervening seeks, in a single call.
You can use it to have concurrent accesses going on at the same time but you have to specify each time what segment of the file you want (in other words, implicit seeking) but you have to do this as multiple calls, one per segment.
If you want asynchronous I/O under Linux, DeveloperWorks has an interesting article on the aio
stuff here, available from Kernel version 2.6 onwards.
Keep in mind that asynchronous I/O (even under Windows) is probably not going to benefit you that much for fast "devices" like a local hard disk. It's probably not worth the extra effort in coding for that use case. Where it comes into its own is with relatively slow devices like network storage or socket communication, where you may be able to get quite a bit of work done before the I/O completes.
Upvotes: 1