How can I read a file with read() and O_DIRECT in C++ on Linux?

后端 未结 1 1095
挽巷
挽巷 2020-12-20 00:48

I\'m searching for a solution to solve the above described problem.

Here\'s my \"doesn\'t working code\". charsInCurrentBuffer returns always -1!

#d         


        
相关标签:
1条回答
  • 2020-12-20 01:14

    When you read from an O_DIRECT fd, the "alignment of the user buffer and the file offset must all be multiples of the logical block size of the file system" (quoted from the open man page) on Linux. Other environments might have different constraints on this, and it's in fact filesystem dependent.

    That's not going to be the case with new generally (unless you get lucky).

    You should consider using a posix_memalign function if your platform has that, or simply allocate a larger buffer (BLOCK_SIZE + BUFSIZE) and use the block-size aligned part of it.

    If you want to stick with new, you're going to need to use some form of placement new combined with the above, but I'm not familiar enough with that to show how that would work.

    For reference, see for example this thread on LKML, or the Notes section of the above-quoted man page.

    0 讨论(0)
提交回复
热议问题