Re: Keeping a file in memory

From: Allin Cottrell (cottrell_at_wfu.edu)
Date: 10/26/04


Date: Mon, 25 Oct 2004 22:09:11 -0400

G.D. wrote:

> I'm using a third-party library which writes some useful
> information into a file. In order to do so, I just have
> to pass a file name to a library function and the file
> gets written.
>
> The problem is that I need the information that has been
> written to the file immediately. I other words, the library
> writes the info to a file (on disk) and my code reads this
> file immediately afterwards, which makes writing the data
> on the disk a waste of time.
>
> I was wondering if there is a (ideally standardized and portable)
> way to keep a file in memory indefinitly, where it presumably
> can be read much faster?

If you want the information that is generated by the library
function _immediately_, the best solution is to have the library
write the info not to a file, but to a buffer -- a buffer to
which the clietnt program is passed a pointer. If you are not
in a position to make this modification to the library, you're
stuck with reading the file from disk.

As for "keeping a file in memory", _after_ is it read from disk,
any decent operating system will do this automagically, using
cache memory. Reading the file for the first time might be time-
consuming, if it is big file, but thereafter it will be read from
cache -- until some more pressing items fill the cache.

Allin Cottrell



Relevant Pages

  • Re: Request: I/O request recording
    ... > file, ready for optimized reading. ... Linux caches disk data on a per-file basis. ... we don't go looking for the same disk blocks in the cache of /dev/hda1. ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Determining size of file
    ... >Is there any C library function that returns the size of a given file? ... The number of characters you can read with fgetcfrom the file ... end of the file from the last byte written to the end of a disk block ... If you had ideas of reading the whole file into memory in binary ...
    (comp.lang.c)
  • Re: Invalidating the file cache
    ... will force a cache flush to ensure that the data is to disk. ... I write to a file (with buffering). ... I start reading from the same file with no buffering. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Disk access speed question
    ... (reading and writing in the process). ... You have memory buffers ... that can be written and read into disk as virtual memory, ... which it puts into a cache somewhere (either disk cache on the disk ...
    (comp.programming)
  • Re: Flushing disk cache
    ... >> the cache is cleared of the file in question? ... > When one has synced buffers to disk and then read more than RAM from ... > Cache is on the way in, buffers are on the way out. ... The process is reading from disk ...
    (comp.os.linux.misc)