Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are the concern regarding simultaneous read and write to a file?

consider the following scenario:

  1. Process 1 (Writer) continuously appends a line to a file ( sharedFile.txt )
  2. Process 2 (Reader) continuously reads a line from sharedFile.txt

my questions are:

In java is it possible that :

  1. Reader process somehow crashes Writer process (i.e. breaks the process of Writer)?
  2. Reader some how knows when to stop reading the file purely based on the file stats (Reader doesn't know if others are writing to the file)?

to demonsterate

Process one (Writer):

...
while(!done){
 String nextLine;//process the line
 writeLine(nextLine);
 ...
}
...

Process Two (Reader):

...
while(hasNextLine()){
  String nextLine= readLine();
  ...
}
...

NOTE:

Writer Process has priority. so nothing must interfere with it.

like image 251
nafas Avatar asked Oct 27 '25 20:10

nafas


1 Answers

Since you are talking about processes, not threads, the answer depends on how the underlying OS manages open file handles:

  1. On every OS I'm familiar with, Reader will never crash a writer process, as Reader's file handle only allows reading. On Linux, system calls a Reader can potentially invoke on the underlying OS are open(2) with O_RDONLY flag, lseek(2) and read(2) -- are known not to interfere with the syscalls that the Writer is invoking, such as write(2).
  2. Reader most likely won't know when to stop reading on most OS. More precisely, on some read attempt it will receive zero as the number of read bytes and will treat this as an EOF (end of file). At this very moment, there can be Writer preparing to append some data to a file, but Reader have no way of knowing it.

If you need a way for two processes to communicate via file, you can do it using some extra files that pass meta-information between Readers and Writers, such as whether there are Writer currently running. Introducing some structure into a file can be useful too (for example, every Writer appends a byte to a file indicating that the write process is happening).

For very fast non-blocking I/O you may want consider memory mapped files via Java's MappedByteBuffer.

like image 198
Sergey Mikhanov Avatar answered Oct 29 '25 12:10

Sergey Mikhanov