linux -- concurrently reading/writing from/to a file [closed]
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 */;
精彩评论