As I understand it, when you mmap a file you are basically mapping the pages from the page cache for that file directly into your process, and when you use O_DIRECT you are bypassing the page cache. Does it ever make sense to use the two together? If my understanding is right how would it even work? mmap seems to rely on the file being in the page cache and O_DIRECT seems to prevent it from going there (assuming nothing else on the system has the file open). I found this question but the answerer seems to think it's perfectly normal to do.
Can mmap and O_DIRECT be used together?
I think it would not have a lot of sense.
O_DIRECT means that all I/O should be reflected in storage, as soon as possible (without cache).
The mapped pages is a copy of storage (file) in the memory. Reflecting every read from and write to memory would have to do some I/O and this would be huge performance hit.
© 2022 - 2024 — McMap. All rights reserved.
O_DIRECT
with a different file than the one being mapped. – Bolenmmap()
would simply ignore the fact that the file is opened inO_DIRECT
mode -- that option just affects how theread()
andwrite()
calls behave. – Bolenmmap
will use it. But if it's not, it maps it from the disk location. – BolenO_DIRECT
mode, and then callsmmap
on that descriptor. Are you actually asking what happsn if process A maps the file, and process B opens the file in O_DIRECT mode -- will they see each other's changes to the file? – Bolen