What happens (at the OS level) when I read / write a file?

Say one program reads an F.txt file, and another program writes that file at the same time.

(When I think about how to implement this functionality if I were a system programmer), I understand that there can be ambiguity in:

  • what will the first program see?

  • where does the second program write new bytes? (ie write "in place", instead of writing to a new file, and then replace the old file with a new one)

  • how many programs can be written to the same file at the same time?

    .. and maybe something is not so obvious.

So my questions are:

  • What are the main strategies for reading / writing files?

  • which of them are supported in OS (Windows, Linux, Mac OS, etc.)?

  • Can it depend on a particular programming language? (I can assume that Java might try to provide some unified behavior for all supported OSs)

+3
source share
3 answers

Single-byte reading has a long journey to go from a magnetic plate / flash cell to your local Java variable. This is a single byte path:

  • Magnetic Plate / Flash Camera
  • Internal Hard Disk Buffer
  • SATA / IDE bus
  • SATA / IDE Buffer
  • PCI / PCI-X Bus
  • Computer data bus
  • Computer RAM via DMA
  • OS Cache Page
  • Libc reader buffer as well as user space fopen()reader buffer
  • Java Local Variable

, , , .

, Java :

FileInputStream fis = new FileInputStream("/home/vz0/F.txt");

// This byte comes from the user space buffer.
int oneByte = fis.read();

4 . , " ", , , .

, , , .

, , :

  • , . .
  • Undefined, .
  • , .

"" , . , :

  • 1kB (0; 1024).
  • 1kB (2096128; 2097152)

, . Java Channel IO , .

RAM , sync. , , -, , .

, - , , .

(Windows NT , Linux, MacOS, * BSD) .

+5

, . , SO - . . , (, MySQL MyISAM engine )

JVM , fsync(), SO, . JVM , , .

0

Source: https://habr.com/ru/post/1789379/


All Articles