470,561 Members | 1,909 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,561 developers. It's quick & easy.

Cannot lock a file.

Hi all,

I have following code:

use 5.004'
use Fcntl ':flock';

sub check_file_status
{
$attempt_lock = 0;
$max_attempts = 3;
open(in_file, "$check_in_file_name");
while (!flock(in_file, LOCK_EX) && $attemp_lock < $max_attempts)
{
sleep(5);

}
close(in_file);
}

I cannot get the exlusive lock for the $check_in_file_name variable
file name. Any idea? How should I correct my mistake?
Jul 19 '05 #1
3 2868
hs*****@usa.com (Hon Seng Phuah) wrote in message news:<38**************************@posting.google. com>...
Hi all,

I have following code:

use 5.004'
use Fcntl ':flock';

sub check_file_status
{
$attempt_lock = 0;
$max_attempts = 3;
open(in_file, "$check_in_file_name");
while (!flock(in_file, LOCK_EX) && $attemp_lock < $max_attempts)
{
sleep(5);

}
close(in_file);
}

I cannot get the exlusive lock for the $check_in_file_name variable
file name. Any idea? How should I correct my mistake?


tried it with perl 5.6.0, no problemo, note flock does not
work for network files (e.g. files retrieved via nfs mount point).
see perldoc -f flock

use 5.004;
use Fcntl ':flock';

sub check_file_status
{
$attempt_lock = 0;
$max_attempts = 3;
my $check_in_file_name = 'file1.txt';
open(in_file, "<$check_in_file_name")||print "$check_in_file_name:$!\n";
while (!flock(in_file, LOCK_EX) && $attemp_lock < $max_attempts)
{
print "lock attempt $!\n";
sleep(5);
$attemp_lock++;

}
close(in_file);
}

check_file_status();
Jul 19 '05 #2
If I change the open to be write. the flock return 1. Why open read
file cannot use exclusive lock like my case below? Does anyone have
any idea?

-HS Phuah

hs*****@usa.com (Hon Seng Phuah) wrote in message news:<38**************************@posting.google. com>...
Hi all,

I have following code:

use 5.004'
use Fcntl ':flock';

sub check_file_status
{
$attempt_lock = 0;
$max_attempts = 3;
open(in_file, "$check_in_file_name");
while (!flock(in_file, LOCK_EX) && $attemp_lock < $max_attempts)
{
sleep(5);

}
close(in_file);
}

I cannot get the exlusive lock for the $check_in_file_name variable
file name. Any idea? How should I correct my mistake?

Jul 19 '05 #3
hs*****@usa.com (Hon Seng Phuah) wrote in message news:<38**************************@posting.google. com>...
If I change the open to be write. the flock return 1. Why open read
file cannot use exclusive lock like my case below? Does anyone have
any idea?


To prevent denial of service attacks. (It's an OS feature, nothing to
do with Perl).

Some OSs go futher and disallow taking a shared lock if you have the
file open write-only, which, IMNSHO is just plain dumb.
while (!flock(in_file, LOCK_EX) && $attemp_lock < $max_attempts)
{
sleep(5);
}


You have not specified LOCK_NB. There's no point having a re-try
loop. If a blocking flock() fails it is usually because the
filehandle (or the file) in question is of a type that doesn't support
the type of locking locking you are asking for. This isn't something
that will change in 5 seconds.

Oh, and BTW you forgot to increment your counter.

This newsgroup does not exist (see FAQ). Please do not start threads
here.

Oh, and please don't top-post it is considered rude.
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nand Belani | last post: by
1 post views Thread by teddysnips | last post: by
1 post views Thread by Jeff Fisher | last post: by
5 posts views Thread by pgdown | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.