不同程序同时对文件做写入操作时产生冲突的原理

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

In simple terms, open establishes an access path to a file or device. If successful, it returns a file descrip-
tor that can be used in read, write, and other system calls. The file descriptor is unique and isn’t shared
by any other processes that may be running. If two programs have a file open at the same time, they
maintain distinct file descriptors. If they both write to the file, they will continue to write where they left
off. Their data isn’t interleaved, but one will overwrite the other. Each keeps its own idea of how far into
the file (the offset) it has read or written. We can prevent unwanted clashes of this sort by using file lock-
ing

 

译文:在一些术语中,Open 就是打开一条访问文件或设备的通道。如果成功,将会返回一个可用于 Write、Read操作或者系统调用的文件描述符。这个文件描述符是唯一的,而且不能和其它正在运行的进程共享。如果两各程序同时打开一个文件,那么它们将分别持有不同的文件描述符。如果某个程序对文件进行写操作,那么写入的位置将会是该程序上次离开这个文件时的位置(而不是文件的最后)。两个程序不会交错写入数据,而是一个程序的写入可能覆盖另一个程序写入的数据。每个程序都只记住自己在文件中的位置,也只管从这个位置开始读写。我们可以通过给文件加锁来防止这样的冲突发生。

转载于:https://my.oschina.net/zengsai/blog/1308

你可能感兴趣的:(不同程序同时对文件做写入操作时产生冲突的原理)