开发者

Reading In A File While It's Being Changed

开发者 https://www.devze.com 2023-01-22 02:44 出处:网络
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:

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

精彩评论

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