I find extremely rude posts that do nothing but contradict the parent when the poster could have verified that the parent was correct.
You're wrong. flock does NOT use its own advisory locking. If flock implemented some kind of advisory locking, Windows's type command would know nothing of it, yet it can't read a file locked using flock.
use Fcntl qw( :flock );
open(my $fh, '>', 'file')
or die("Unable to open file: $!\n");
print $fh ("ok\n");
print("type without lock:\n");
system("type file");
print("\n");
flock($fh, LOCK_EX|LOCK_NB)
or die("Unable to lock file: $!\n");
print("type with lock:\n");
system("type file");
unlink('file');
outputs
>perl 590735.pl
type without lock:
ok
type with lock:
The process cannot access the file because another process has locked
+a portion of the file.
Tested with 5.6.0, 5.6.1, 5.8.0 and 5.8.8.
Update: Updated to show that type works when the file isn't locked by flock.
|