开发者

Reading In A File While It's Being Changed

I'm curious what happens in this scenero. Suppose I open a file for reading, and begin reading the contents in a loop. Like this:

$fp = fopen('test.txt', 'r');
while(!feof($fp)) {
    fread($fp, 1024);
}
fclose($fp);

What happens if another process starts appending to the file while I'm reading it开发者_StackOverflow中文版?


On UNIX/Linux:

All processes just see the file as a bunch of bytes with a length. If someone else changes the bytes or changes the length, all other processes immediately see this new data.

An open file refers to an inode. If you create a completely new file then it's a new inode. If you rename the new file over the old file, the filename in the directory now references the new inode, whereas you have the old inode open (even though it can now no longer be seen as it's not linked anywhere from any directory.) In this case, you continue to see the old file and any process opening/modifying the new file sees only the new file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜