开发者

File I/O OS handling

This isn't a direct coding question but more of a OS handling mechanism. I was reading somebody's previous question regarding C# and file handling. Apparently C# was throwing an exception regarding a file being locked when trying to access this. So my question is, does C# use an internal lock to handle file I/O between processes, or does the OS use some type of mutual exclusion for file I/O?

From what I learned about operating systems, well at least unix, is that the开发者_JAVA技巧 OS doesn't implement any type of mutual exclusion for processes trying to access the same file.


When you open a file, you can signal to the operating system how you want to share access to the file. You can say "allow others to read this file while I have it opened, but not write to it" or "allow others to read and write to the file while I have it opened" or even "don't allow anybody to do do anything to this file while I have it open".

Typically, when you open a file for reading, you specify the "do not allow anybody to write (or delete) the file" because that can lead to corruption of the data you're reading in.

Linux does implement file locking, but it's not compulsory, as I understand it (that is, programs can ignore the locks and open the file anyway). On Windows, the locks are managed by the kernel and cannot be bypassed by applications.


Different systems do it different ways. Unix has the flock system call which creates an advisory lock, but programs can ignore it at their peril. Check out the flock(2) man page.

Also, the wikipedia file locking page has a pretty good overview; it could really be considered a complete answer to this question:

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜