473,224 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 2957
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Halcrow | last post by:
Hi I've spent ALL DAY trying to re-install SQL Server 2000 on Windows XP. I continually get the error 'cannot configure server' just at the end of the installation. I've tried the following: ...
2
by: Henrik | last post by:
Hi Im trying to make an app that uses threading, I want to write to a file in a loop. How can i ensure that only one thread writes to the file a the time??? Im accessing the fil with: ...
22
by: Smutny30 | last post by:
Hello, I am preparing a database that will store 10 n * GBs - 100 n * GBs of data. I calculated to have 1,2 GB of bufferpools. I run the DB2 v. 8.2.1 alone on 4 GB box. I obtain : ...
1
by: Nand Belani | last post by:
Hi, I have a MS Access 2000 database on the network, it is secured and the mdw file is on the network. The database works for most of the users on the network, but does not open for two of the...
1
by: teddysnips | last post by:
FRONT END: ACCESS 2000 SP3 BACK END: SQL SERVER 2000 I have been making some modifications to the front end, and a completely unrelated error has occurred. It is unrelated, because the...
4
by: news.microsoft.com | last post by:
I need some help here guys.... i have been simply trying to delete a file from the webserver using asp.net using the following code. System.IO.File.Delete(FullPath); however the error message...
1
by: Jeff Fisher | last post by:
The development team is having a problem when they open an asp page edit the code and then try to save it they receive an error. "The process cannot access the file because it is being used by...
2
by: Ying Lu | last post by:
Hello all, I met a problem about cannot start postmaster. The situation I met is that I was running postmaster, but because of the server closed all my konsole. As a result, I did not...
5
by: pgdown | last post by:
Hi, I have several processes accessing files from one folder, but only one process should ever access each file. Once one process has the file, no other process should be allowed to access it,...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.