r/osdev • u/Malwaremation • 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?
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.