473,473 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2965
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.