Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will happen if two process access the same file without a lock? [closed]

Tags:

file

linux

In linux: if two process access the same file without any lock, what will happen? Considering the following cases:

  1. Process A is reading a file while process B is writing it.
  2. Both process A and process B are writing the same file.
  3. Process A is reading a file, process B is copying/moving another file to replace the original file
  4. Process A is reading a file, process B is removing the file
  5. Process A is writing a file, while process B is copying/moving another file to replace the original file
  6. Process A is writing a file, process B is removing the file
like image 203
Patrick Avatar asked Oct 25 '25 10:10

Patrick


1 Answers

1, 2. The read and write requests are essentially broken into smaller requests and these are performed in no particular order. The result is a complete jumble up. There are some exceptions to this, such as several processes appending to a file. In this case each write is atomic.

4, 6. File removal has no effect on file handles that are already open.

3,5. Same as either 1,2 or 4,6, depending on whether it's a copy or a move (== delete the target and rename the source).

like image 84
n. 1.8e9-where's-my-share m. Avatar answered Oct 27 '25 00:10

n. 1.8e9-where's-my-share m.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!