In the previous subsection, we looked at providing advice on memory mappings. In this section, we will look at providing advice to the kernel on normal file I/O. Linux provides two interfaces for such advice-giving: posix_fadvise() and readahead(). The posix_fadvise( ) System Call The first advice interface, as its name alludes, is standardized by POSIX 1003.1-2003: #include <fcntl.h> int posix_fadvise (int fd, A call to posix_fadvise() provides the kernel with the hint advice on the file descriptor fd in the interval[offset,offset+len). Iflenis0, the advice will apply to the range[offset,length of file]. Common usage is to specify0forlenandoffset, applying the advice to the entire file. The availableadvice options are similar to those formadvise(). Exactly one of the following should be provided foradvice: POSIX_FADV_NORMAL POSIX_FADV_RANDOM POSIX_FADV_SEQUENTIAL POSIX_FADV_WILLNEED POSIX_FADV_NOREUSE POSIX_FADV_DONTNEED As withmadvise(), the actual response to the given advice is implementation-specific—even different versions of the Linux kernel may react dissimilarly. The following are the current responses: POSIX_FADV_NORMAL POSIX_FADV_RANDOM POSIX_FADV_SEQUENTIAL POSIX_FADV_WILLNEED POSIX_FADV_NOREUSE POSIX_FADV_DONTNEED As an example, the following snippet instructs the kernel that the entire file represented by the file descriptorfdwill be accessed in a random, nonsequential manner: int ret; ret = posix_fadvise (fd, 0, 0, POSIX_FADV_RANDOM); On success, posix_fadvise() returns 0. On failure, EBADF EINVAL The readahead( ) System Call The posix_fadvise() system call is new to the 2.6 Linux kernel. Before, the readahead() system call was available to provide behavior identical to the POSIX_FADV_WILLNEEDhint. Unlikeposix_fadvise(),readahead()is a Linux-specific interface: #include <fcntl.h> ssize_t readahead (int fd, A call to readahead() populates the page cache with the region [offset,offset+count) from the file descriptor fd. On success, readahead() returns 0. On failure, it returns -1, anderrnois set to one of the following values: EBADF EINVAL
blog comments powered by Disqus |
|
|
|
|
|
|
|