What is the problem of simultaneous reading and writing to a file?

consider the following scenario:

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

my questions:

In java it is possible that:

  • Does the reading process somehow crash the Writer (i.e. interrupt the Writer process)?
  • Reader , who knows when to stop reading a file based solely on the file's statistics (the reader doesn't know if other files are writing to the file)?

for demonstration

Process one ( Writer ):

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

      

Process two ( Reader ):

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

      

Note:

Writer Process takes precedence. therefore nothing should hinder him.

+3


source to share


4 answers


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

  • On every OS I am familiar with, Reader will never crash the write process, as reading the Reader file only allows reading. On Linux, system calls that Reader can potentially call on the underlying OS are open(2)

    flagged O_RDONLY

    , lseek(2)

    and read(2)

    are not known to affect system calls that Writer calls, for example write(2)

    .
  • The reader will most likely not know when to stop reading on most operating systems. More precisely, on some attempt to read, it will receive zero as the number of bytes read and will treat this as EOF (end of file). At this very moment, there may be a Writer ready to add some data to the file, but the Reader does not know this.


If you want a way for the two processes to communicate through a file, you can do this by using some additional files that pass meta information between Readers and Writers, for example, is there currently a Writer. Representing some structure to a file can also be useful (for example, each Writer adds a byte to the file to indicate that the write process is in progress).

For very fast non-blocking IO, you might consider memory mapped files via Java MappedByteBuffer

.

+3


source


The code will not crash. However, when the end is reached, the reader will stop, even if the writer can still write. You will need to sync somehow!



0


source


Concern:

Your thread of readers may be reading the stale value even if you think another thread has updated the value of the variable

Even if you are writing to a file, if there is no synchronization there, you will see a different value when reading

0


source


Java File IO and plain files were not designed to be written and read at the same time. Either your reader will comprehend your writer, or your reader will never finish.

JB Nizet provided an answer in his comment. You are using BlockingQueue to store write data while reading. Either the queue will be empty or the reader will never finish. You have the means with BlockingQueue methods to detect any situation.

0


source







All Articles