The ministry of strange syscalls

My favorite syscall today:

$ man 2 readahead
"readahead() populates the page cache with data from a file so that subsequent reads from that file will not block on disk I/O."

I don't really know when should I use that, but it sounds cool. Just an implementation of prefetching on yet another layer. Wait a moment...

"readahead() blocks until the specified data has been read. "

I'm lost. If it blocks, why not to just use read(2)?

Second syscall:
$ man 2 madvise
"The madvise() system call advises the kernel about how to handle paging input/output in the address range beginning at address start and with size length bytes"
"The kernel is free to ignore the advice."

I don't get when I should use it.

And the last one:
$ man 2 mincore
"mincore() returns a vector that indicates whether pages of the calling process’s virtual memory are resident in core (RAM), and so will not cause a disk access (page fault) if referenced."

Cool, I can check if my memory page is in swap. I doubt it can help me in anything. If I don't want my pages to be in swap I'll just use mlock(2)...

Probably there are dozens of strange syscalls out there!


Denton Gentry said...

madvise(MADV_DONTNEED) is useful for malloc implementations. Once the page has been free()'d, there is no reason to write its contents out to swap. If the kernel needs to reclaim the page it can discard its contents, even if dirty. If malloc needs to use the page again it will be faulted back in as a fresh zero-filled page.

majek said...

Denton Gentry said...
Once the page has been free()'d, there is no reason to write its contents out to swap
That's a great use case. It would be great to be able to implement memset(,0,) using this syscall.

I still claim that this syscall is a bit weird.

niklasro said...

keyword here is "subsequent". All you really know is sequence, main thing, what was subsequently and what consequently. You can have the right to a subsequence (random) that however excludes determinism which is strongly biased to consequences (causality)