What goes on behind the curtains during disk I/O?

后端 未结 2 1137
花落未央
花落未央 2020-12-02 23:41

When I seek to some position in a file and write a small amount of data (20 bytes), what goes on behind the scenes?

My understanding

To my k

2条回答
  •  借酒劲吻你
    2020-12-03 00:32

    Indeed, at least on my system with GNU libc, it looks like stdio is reading 4kB blocks before writing back the changed portion. Seems bogus to me, but I imagine somebody thought it was a good idea at the time.

    I checked by writing a trivial C program to open a file, write a small of data once, and exit; then ran it under strace, to see which syscalls it actually triggered. Writing at an offset of 10000, I saw these syscalls:

    lseek(3, 8192, SEEK_SET)                = 8192
    read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1808) = 1808
    write(3, "hello", 5)                    = 5
    

    Seems that you'll want to stick with the low-level Unix-style I/O for this project, eh?

提交回复
热议问题