244 Chapter 13
Figure 13-1: Summary of I/O buffering
13.5 Advising the Kernel About I/O Patterns........................................................................
The posix_fadvise() system call allows a process to inform the kernel about its likely
pattern for accessing file data.
The kernel may (but is not obliged to) use the information provided by
posix_fadvise() to optimize its use of the buffer cache, thereby improving I/O per-
formance for the process and for the system as a whole. Calling posix_fadvise() has
no effect on the semantics of a program.
The fd argument is a file descriptor identifying the file about whose access pat-
terns we wish to inform the kernel. The offset and len arguments identify the region
of the file about which advice is being given: offset specifies the starting offset of the
region, and len specifies the size of the region in bytes. A len value of 0 means all
Kernel memory
User Memory
Make flushing automatic
on each I/O call
Kernel buffer cache
printf(),
stdio buffer
Disk
fsync()
fdatasync()
sync()
fflush() setbuf(stream, NULL)
open(path, flags |
O_SYNC, mode)
User data
I/O system calls
write(), etc.
Kernel-initiated write
To force
buffer flush
stdio library calls
fputc(), etc.
#define _XOPEN_SOURCE 600
#include <fcntl.h>
int posix_fadvise(int fd, off_t offset, off_t len, int advice);
Returns 0 on success, or a positive error number on error