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!
- Alcatel (6)
- Android (6)
- Blogger (10)
- Calculators (1)
- Facebook (1)
- Games (4)
- GitHub (1)
- Html-Css-Js (43)
- Illustrator (2)
- InfoSec (13)
- Photoshop (4)
- PHP (11)
- Unicode (2)
- Windows (2)
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!
Click here to go to this blog's Facebook Page!
Thank you!
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 - Multi One Password
Ads (Reserved)
Tuesday, November 24, 2020
Wednesday, November 18, 2020
Thursday, November 5, 2020
[PHP] Check_File_Write_Queue(Function)
Like PHP built-in "flock()" function, "Check_File_Write_Queue()" function can be used to queue almost all, if not all, Requests\Tasks in PHP, and as the former, the latter function was mainly written to handle file write queue!
An interesting difference between the 2 functions is that, "Check_File_Write_Queue()" is more precise than "flock()", for example, if "Add A" is clicked 5 times at once, then "Add B" is also clicked 5 times at once, the output in "Edit_File_Content.txt" file from "Check_File_Write_Queue()" will always be "AAAAABBBBB" and from "flock()" will vary, "AAABBABBBA", "BBAABBAABA", "ABABBBAABA", "BBBBBAAAAA", "AAAAABBBBB", etc, which means that "Check_File_Write_Queue()" always handles the requests in the correct order and "flock()" sometimes does and sometimes does not!
"Check_File_Write_Queue()" function:
Same as above, but "flock()" function used instead:
An interesting difference between the 2 functions is that, "Check_File_Write_Queue()" is more precise than "flock()", for example, if "Add A" is clicked 5 times at once, then "Add B" is also clicked 5 times at once, the output in "Edit_File_Content.txt" file from "Check_File_Write_Queue()" will always be "AAAAABBBBB" and from "flock()" will vary, "AAABBABBBA", "BBAABBAABA", "ABABBBAABA", "BBBBBAAAAA", "AAAAABBBBB", etc, which means that "Check_File_Write_Queue()" always handles the requests in the correct order and "flock()" sometimes does and sometimes does not!
"Check_File_Write_Queue()" function:
Same as above, but "flock()" function used instead:
Subscribe to:
Posts (Atom)