Is mmap + madvise really a form of asynchronous I / O?

I am trying to find out if there is a mmap 'file, and then using madvise() or posix_madvise() with MADV_WILLNEED / POSIX_MADV_WILLNEED actually runs the background asynchronous I / O for reading. The man pages for madvise do not determine if this is so: the actual behavior of madvise remains largely unclear in order to provide flexibility in implementation.

But is any real POSIX implementation in the main environment (like Linux) actually performing I / O operations on aync files when madvise() MADV_WILLNEED with MADV_WILLNEED ? I can not get reliable information about this. This question suggests that it really works, at least on Linux, even if it is not perfect, since there is no callback mechanism.

This excerpt from the book states that posix_fadvise with POSIX_FADV_WILLNEED will do asynchronous read forward, but does not mention if madvise() does asynchronous forward read.

In addition, it would seem that the whole concept of open source I / O makes no sense if it is not asynchronous. If it was synchronous, it just makes the user application block for reading, and not later when it really reads the file, which does not seem to be a particularly strong optimization.

So, does madvise() with MADV_WILLNEED do asynchronous read ahead on any main platform (like Linux)?

+6
source share
2 answers

In general, you should assume that m * functions will not perform async readahead reading.

0
source

On Linux, you can always check the source code.

See fadvise.c :

 case POSIX_FADV_WILLNEED: ... force_page_cache_readahead(mapping, f.file, start_index, nrpages); break; 

So posix_fadvise calls force_page_cache_readahead to execute readahead.

Now consider madvise.c :

 static long madvise_willneed(...) { ... force_page_cache_readahead(file->f_mapping, file, start, end-start); return 0; } 

So, MADV_WILLNEED and POSIX_FADV_WILLNEED equivalent on Linux.

Can this be called asynchronous I / O? I do not think so. Async IO usually implies some kind of notification that allows you to retrieve data. Counseling is just advice: not only do you not know when the data is ready to be read, but if you are late, the data may be thrown away already.

0
source

All Articles