开发者

linux -- concurrently reading/writing from/to a file [closed]

It's difficul开发者_StackOverflow社区t to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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 ?


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 */;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜