Re: Understanding flock()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Gerard Samuel wrote:
Im trying to simulate conditions, to see how flock works.
Can anyone verify with the example code below,
that data, never gets written to the file.
//1 & //2 are supposed to be processes???
For me, the var_dump() reports ->
int(7) bool(false)
Thanks

$fp = fopen('foo.txt', 'w'); //1
flock($fp, LOCK_EX) or die('Unable to lock'); //1
unlink('foo.txt') or die('Unable to remove foo.txt'); //2
$bytes = fwrite($fp, 'testing'); //1
fclose($fp); //1
var_dump($bytes, file_get_contents('foo.txt')); //1


To my knowledge (and might apply to unices only) unlink only "unlinks" file, it does not delete it. Unlinking removes link (reference, filename) to an inode that holds the file. Inode is deleted by the kernel once the link count referencing the inode reaches zero and the inode is no longer opened by any process. I hope I did not confuse you :-)


So the result is that even if process 2 unlinked the filename link, process 1 had opened the file (inode) so it's not removed untill it's closed and process 1 can write to it. file_get_contents() failed because the link no longer exists.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux