Re: When does `write' return?



Andrew Xue wrote:
> Hello,
>
> As far as I know, the system call `write' is asynchronous, which would
> leave the `data' in page cache. The `data' would be flushed in the
> background, thus the `write' could return quickly, in order to improve
> the performance.
>
> My question is: when would the `write' return? For example:
>
> write(fd, buf, 1GB)
>
> The 1GB buf will be written in to disk file represented by `fd'. When the
> `write' returns, how much of buf has been written? Does the kernel cache
> the buf with copy-on-write or just making a full memory copy?
>
>
> I am not so familiar with linux kernel, please forgive me if the question
> is too silly.
>
It is not silly.

It depends on a coupla things. If you have much more than 1GBytes of ram,
the write could return as soon as the data get to a memory buffer. Possibly
some of the cache would have to be paged out, but not necessarily.

It also depends if file fd is a raw file or not (if you are using raw file
systems, you probably already know enough to answer your questions; so
probably not a raw file). If it is, I believe you must wait until the write
completes, but OTOH, you do not need to take the time to find system buffer
space and copy from user space to system space, and you might be able to do
it in a giant "spiral write" as DEC used to call it. Depends on hardware and
the low level driver, I would expect.

And if you are really worried about the data getting onto the disk platter,
consider that many drives have 8 to 16 MByte buffers inside, so even when
the kernel thinks it has written to the hard drive, there may be up to 8
MBytes that might not get there at all if Murphy is on the job.

--
.~. Jean-David Beyer Registered Linux User 85642.
/V\ PGP-Key: 9A2FC99A Registered Machine 241939.
/( )\ Shrewsbury, New Jersey http://counter.li.org
^^-^^ 08:55:00 up 13 days, 23:41, 3 users, load average: 4.14, 4.12, 4.14
.



Relevant Pages

  • Re: [9fans] cwfs
    ... yes, here is a console... ... outin("cwfscmd", buf, sizeof buf); ... pis our end of the pipe to cwfs stdin. ... garbage to the cache device, ...
    (comp.os.plan9)
  • When does `write return?
    ... would leave the `data' in page cache. ... The 1GB buf will be written in to disk file represented by `fd'. ... cache the buf with copy-on-write or just making a full memory copy? ... I am not so familiar with linux kernel, ...
    (comp.os.linux.misc)
  • Re: Cached file read performance with 6.2-PRERELEASE
    ... Glancing at the 'top' man page, "150M Wired" seems to be the data file cache, while "111M Buf" is block-level caching... ... That should give you an idea as to how much RAM is being used for the buffer/block IO cache, and the VM disk cache. ... It might be worthwhile trying with a series of different file size to determine if there is a point where the caching performance drops... ...
    (freebsd-stable)
  • [PATCH 04/15] ps3vram: Replace mutex by spinlock + list
    ... Remove the mutex serializing access to the cache. ... struct ps3vram_cache cache; ... buf += avail; ...
    (Linux-Kernel)
  • [PATCH] ps3vram: Replace mutex by spinlock + list
    ... Remove the mutex serializing access to the cache. ... struct ps3vram_cache cache; ... buf += avail; ...
    (Linux-Kernel)