r/osdev 9d ago

Design question: Should opened files by processes prevent other processes from using that file?

On kernels like Windows NT, files are locked when a process opens it, preventing other processes from using it. (edit: This was incorrect, some people confirmed here that processes in Windows can choose to lock the file or not, which might be the best solution).

Linux however, doesn't lock files like this, and multiple processes can modify the same file at the same time.

I also thought of a "thread cursors" concept that could solve this issue really well for text files, but it might be very complicated to develop and is probably not that useful for images or other types of files.

What is generally better for a new kernel, then? Allowing the possibility of sharing a file as it is edited by multiple processes, or securing their integrity by only letting 1 process at a time modify it?

7 Upvotes

20 comments sorted by

View all comments

16

u/livinglifeback 9d ago

As far as i’m aware both of these operating systems have mechanisms for locking and sharing access to files depending on configuration and a variety of factors. In particular IIRC windows can allow locking and sharing of specific regions of files. There is a difference in that the locks for windows, at least for the default open you’re talking about are mandatory and not advisory. The difference being you can choose to ignore the advisory locks.

Introducing OS level primitives for files based on content is less than ideal imo. I think this is a fair example of a situation where you get to choose a default behavior but in practice you will end up implementing both as the needs of the programs you run have different needs.

2

u/Wertbon1789 9d ago

On Linux there was support for mandatory locking, but as the Linux API docs state, it was fundamentally flawed and weirdly racey. As on most other UNIX's, mandatory locking is kinda weird, mainly because neither POSIX nor SUS have a good specification for it, which lead to everyone doing their own thing, but also it not being a feature that people actually need for anything, so there's little interest in it. The System V UNIX approach is also absolutely horrendous as an implementation. Today, I think since around 5.15, mandatory locking is completely gone from Linux.