linux-同时从文件中读取/写入文件 [英] linux -- concurrently reading/writing from/to a file

查看:97
本文介绍了linux-同时从文件中读取/写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux对我来说很奇怪,我在考虑如何对程序进行编码以安全地操作linux文件.如果我的程序对其他进程正在操作的文件(普通文件或开发文件等)进行操作(打开,读取或写入),我应该考虑哪些方面.您能给我一些想法,还是建议我任何有用的文章/链接?

Linux is strange for me, I am thinking how to code my program to safely operate linux files. what aspects should I regard if my program operate(open, read or write) to a file(normal file or dev file and so on) that is being operated by other process. can you give me some ideas, or suggest me any helpful articles/links ?

推荐答案

一个简单的规则:如果每个人都在阅读,则让他们阅读,但是如果每个人之间也在写,那么请始终使用锁.查看 flock 以获得详细信息和示例.对于Unix,即使文件被锁定,打开文件也不会失败.需要尝试咨询锁定.这样的事情会做到这一点:

one simple rule: If every one is reading, let them read, but if some one is writing as well in between, always use locks. Check flock for details and example. For unix, even if file is locked file open would not fail. A try for advisory lock needs to be done. Something like this would do it:

err = lockf(fd, F_TLOCK, 0);
if (err < 0)
    /* err = sharing error */;

这篇关于linux-同时从文件中读取/写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆