jQuery

elevateZoom (Requires jQuery)

Facebook Messenger (Donation)


Many donors of this blog prefer to donate their money through Facebook Messenger!

Click here to go to this blog's Facebook Page!

Thank you!

Activation Code - Donation (Multiple Options)



Donate

Request Web\PHP\JavaScript\HTML Project !

Contact us if you want us to develop a Web\PHP\JavaScript\HTML project for you!

Advertise On This Blog!

If you want a Product\Service\Event to be advertised on this blog, feel free to contact us!

Ads (Reserved)

Tuesday, November 24, 2020

[PHP] wait_Read_Write_end_of($File) Functions

PHP "file_get_contents()" function may return an empty or incomplete file content if the file being read is also being written by another process!

To avoid the above problem, PHP "flock()" function must be used! In order to automate all the process, "wait_Write_end_of()" function can be used, as it makes use of "flock()" function!

Multiple processes writing at same time to a file, can cause all kind of problems we can imagine, thus, the "flock()" function must also be used here! In order to automate all the process, "wait_Read_Write_end_of()" function can be used, as it makes use of "flock()" function as well!

"wait_Write_end_of($File)", Read a file if the file is not being written! (If the file is being written, the execution is halted until the file is unlocked!)

"wait_Read_Write_end_of($File)", Write to a file if the file is not being read or written! (If the file is being read or written, the execution is halted until the file is unlocked!)

One minor thing to be noted is that "flock()" does not handle the requests in order of call!

The code below can be used to test how PHP handles Read\Write requests!

Test Notes:

- while "LOCK_EX Write" button is writing to file, "Read" button incorrectly returns empty file content!
- while "LOCK_EX Write" button is writing to file, "LOCK_SH Read" button returns the correct file content!

- for File content = "Oops_":
- if, for example, "Write" button is pressed 5 times at once, it incorrectly writes "zops_z" instead "Oops_yyyyz" to the file!
- if, for example, "LOCK_EX Write" button is pressed 5 times at once, it correctly writes "Oops_wwwwx" to the file as expected!

No comments:

Post a Comment

ADS - Multi Share Calculator

https://windowsportableapps.blogspot.com/2019/03/multi-share-calculator.html