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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值