14

As I understand, when a file is being written, the process writing to the file obtains an exclusive lock. So other processes cannot access this file for read.

With the above knowledge, I'm unable to understand how I'm able to play a video in media player, when the browser is still downloading it.

3
  • 2
    Please specify the reason for the downvote. Thanks.
    – Sorter
    Jun 27, 2015 at 8:42
  • 2
    This is platform-dependent, so please specify the OS you are asking about. I know that Linux doesn't provide any implicit locks, but Windows may be different.
    – amon
    Jun 27, 2015 at 8:55
  • I'm using Linux
    – Sorter
    Jun 27, 2015 at 8:58

3 Answers 3

16

Your understanding is wrong. Several Linux processes can write to the same file at once (and what happens then might be unspecified, except when the processes overwrite different segments of that file). And some other Linux process (e.g. your browser) can read a file which is written to.

You could adopt a convention to avoid that. For example, you could use advisory file locking (adopting the convention that every relevant program should do that), e.g. with flock(2), lockf(3), etc.... But that won't forbid other processes (not following your conventions) to access (or even write to) that file. You might use permission machinery (e.g. by dedicating a system user to your setuid program).

See also this & that answers on StackOverflow (some of them mentioned the deprecated mandatory locking Linux-specific mechanisms). Read about ACID properties, consider using some database (sqlite, PostGreSQL, MongoDB, etc...) or some indexed file (gdbm).

I don't know Windows, but heard the rumor that it does not allow concurrent access or writes to the same file.

4

The files usually aren't locked - unix file locking. It is also fairly common to have multiple readers of file but only one writer. Problems only occur when you would have multiple writers.

Also, most videos file formats (and especially on the web) are streamable, which means that you don't have to have the whole video to play it.

1
  • Problems can occur if you are counting on a file's content to not change in between reads, e.g. when trying to avoid loading big files into memory.
    – yyny
    Dec 7, 2017 at 2:08
3

Unix OSes do not use compulsory locking (Gnu/Linux is a Unix, other examples are UNIX, BSD, Mac OSX). Locking as avoided where ever possible.

Some other OSes use compulsory locking all the time (Notably Microsoft's Windows OS). This will stop you playing until fully downloaded, it will also stop you renaming a file or directory that is open (that an application is in). But on Unix it is OK. If an application needs locking it can use it. But not normally.

4
  • You can choose to not lock files on Windows as well. I play half downloaded videos all the time. Locking is only required for loaded executables. Jun 27, 2015 at 21:19
  • @CodesInChaos Too true, but most apps lock, do you know if you can go in a directory with out locking? Also I am just thinking, how do executables on Linux work with out locking (Both systems use just in time loading, so bits can be loaded a long time after program start), may be it uses its super cow powers. Jun 28, 2015 at 13:16
  • 1
    I think the big difference between Linux and Windows is that Linux treats the file as separate from its file system entry. So you can unlink a filename while its open. Windows on the other hand delays the deletion until it gets closed. Does Linux allow the modification of running executable files? Or does it just allow deletion+recreation with different content? Jun 28, 2015 at 14:43
  • @CodesInChaos Both I would guess, I was wondering what would happen if you modified it, not delete+create. My guess is that when an app is loaded, the OS opens the file in COW mode (Copy On Write), the if and only if Some process changes the content, the OS will make a copy (of the modified blocks). One copy is linked to the process(es), the other to the file. When all processes using the original are closed, then the original copy is deleted(as no file-system entry). This is just a guess, based on what I know the OS can do. Jun 28, 2015 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.