How to avoid file locks when the PHP / server process crashes?

I am new to PHP. I understand that I can use flock()to lock a file and avoid race conditions when two users reach the same php file by adding content to the file being locked.

However, what happens if the php process crashes? What happens to the next user waiting for a file to be locked? What happens if the server crashes (someone pulls the plug)? Is the lock automatically released? Will the file remain locked after server reboot?

To do this briefly, does PHP properly handle such critical situations (i.e., locks not explicitly released)? If not, how should these situations be dealt with? How to recover from them?

+5
source share
1 answer

Locks are handled by the OS. Therefore:

  • If a process crashes, all locks that it holds are released (along with any other resource that it holds)
  • if the system fails, the locks do not make sense, since they are not transferred to the next reboot.

PHP should not do anything special except use the file locking mechanism provided by the OS, so in general you are completely safe.

, - , , ( ), , -. , , , ( : - - " ", ).

+5

All Articles