Suppose there are 2 processes P1 and P2, and they access a shared file Foo.txt
.
Suppose P2 is reading from Foo.txt
. I don't want P1 to write to Foo.txt
while P2 is reading it.
So I thought I could make P1 write to Foo.tmp
and as a last step, rename Foo.tmp
to Foo.txt
. My programming language is Java
So my question is, would this ensure that P2 reads the correct data from Foo.txt
? Would the rename operation be committed once P2 completes reading the file?
EDIT
I tried to recreate this scenario as follows:
My P1 code is something like this:
File tempFile = new File(path1);
File realFile = new File(path2);
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
for(int i=0;i<10000;i++)
writer.write("Hello World
");
writer.flush();
writer.close();
tempFile.renameTo(realFile);
and my P2 code is :
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while(true) {
while((line=br.readLine())!=null){
System.out.println(line);
Thread.sleep(1000);
}
br.close();
}
My Sample shared File:
Test Input
Test Input
Test Input
I'm starting P1 and P2 almost simulataneously (P2 starting first).
So according to my understanding, even though P1 has written a new Foo.txt, since P2 is already reading it, it should read the old Foo.txt content until it re-opens a BufferedReader to Foo.txt.
But what actually happens is P2 reads Test Input
thrice, as is expected from the input, but after that it reads the new content which was written by P1.
Output from P2:
Test Input
Test Input
Test Input
Hello World
Hello World
Hello World
.
.
.
So it doesn't work as it should. Am I testing this scenario wrong? I feel like there's something I'm missing out.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…