如果有python脚本这样做:
with open('large_input_file.log', 'rb') as f :
for each_line in f :
do something .....
我们称这个脚本 a.py
large_input_file.log
约为16GB。a.py
将需要几个小时来处理此文件。
如果我这样做(在Linux下),将会发生什么:
继续a.py
跑
删除 large_input_file.log
large_input_file.log
用不同的内容替换但名称相同
是否a.py
能得到正确的数据large_input_file.log
之前,我删除了吗?(我想这将会发生。)
或将以a.py
新的偏移量开始的新数据获取新数据large_input_file.log
您可以在内核级别还是文件系统级别解释它?(Linux如何做到这一点)?
-----------------在一些答案之后添加以下内容----------
如果我的磁盘大小为16Gb怎么办,那么只能存储一个large_input_file.log
。
如果我删除large_input_file.log
并创建另一个16Gblarge_input_file.log
文件会怎样?
让我们创建一个文件:
# echo foo > test.txt
现在,我们将使用tail
它来监视更改:
# tail -f test.txt
foo
让我们在终端上打开另一个选项卡,然后检查tail
过程的pid :
# ps aux | grep -i tail
root 5458 0.0 0.0 7484 724 ? S Sep15 0:13 tail -f -n 0 /var/log/syslog
root 5919 0.0 0.0 7484 784 ? S Sep15 0:13 tail -f -n 0 /var/log/syslog
root 6381 0.0 0.0 7484 840 ? S Sep15 0:14 tail -f -n 0 /var/log/syslog
emil 27789 0.0 0.0 8852 784 pts/8 S+ 12:26 0:00 tail -f test.txt
emil 27826 0.0 0.0 15752 1016 pts/9 S+ 12:26 0:00 grep -i tail
因此,在我的情况下,pid为27789。我们可以通过检查/proc/27789/fd
目录来查看进程的打开文件:
# ls -lah /proc/27789/fd/
total 0
dr-x------ 2 emil emil 0 Sep 20 12:26 .
dr-xr-xr-x 9 emil emil 0 Sep 20 12:26 ..
lrwx------ 1 emil emil 64 Sep 20 12:26 0 -> /dev/pts/8
lrwx------ 1 emil emil 64 Sep 20 12:26 1 -> /dev/pts/8
lrwx------ 1 emil emil 64 Sep 20 12:26 2 -> /dev/pts/8
lr-x------ 1 emil emil 64 Sep 20 12:26 3 -> /home/emil/test.txt
lr-x------ 1 emil emil 64 Sep 20 12:26 4 -> anon_inode:inotify
在这里,我们看到tail
有一个名为3至的文件描述符test.txt
。如果我们删除文件怎么办?
# rm test.txt
# ls -lah /proc/27789/fd
total 0
dr-x------ 2 emil emil 0 Sep 20 12:26 .
dr-xr-xr-x 9 emil emil 0 Sep 20 12:26 ..
lrwx------ 1 emil emil 64 Sep 20 12:26 0 -> /dev/pts/8
lrwx------ 1 emil emil 64 Sep 20 12:26 1 -> /dev/pts/8
lrwx------ 1 emil emil 64 Sep 20 12:26 2 -> /dev/pts/8
lr-x------ 1 emil emil 64 Sep 20 12:26 3 -> /home/emil/test.txt (deleted)
lr-x------ 1 emil emil 64 Sep 20 12:26 4 -> anon_inode:inotify
文件描述符仍然存在,但ls
可以帮助我们知道该文件已被删除。
正如Igor所说,每个文件在磁盘上都有原始数据所在的物理位置。为了查找文件,系统维护一个将文件名映射到实际数据的索引节点表。删除文件不会擦除磁盘上的数据,而只会修改索引节点。数据将仍然存在,直到被其他内容显式覆盖为止。但是,在这种特定情况下,内核包含额外的代码,以确保文件继续存在-并且不会被覆盖-直到不再由任何进程打开文件为止。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句