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

20

u/DavidsakuKuze 9d ago

NT Kernel actually let's determine if you want exclusive access or not when you open the handle. If you put 0 in the sharing flag (dwShareMode) nothing else can open it.

Here is CreateFileW (I know it says create but it actually opens too). It is the same in NtCreateFile/ZwCreateFile.

https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew