473,811 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ 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_stat us
{
$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 2982
hs*****@usa.com (Hon Seng Phuah) wrote in message news:<38******* *************** ****@posting.go ogle.com>...
Hi all,

I have following code:

use 5.004'
use Fcntl ':flock';

sub check_file_stat us
{
$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_stat us
{
$attempt_lock = 0;
$max_attempts = 3;
my $check_in_file_ name = 'file1.txt';
open(in_file, "<$check_in_fil e_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_stat us();
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.go ogle.com>...
Hi all,

I have following code:

use 5.004'
use Fcntl ':flock';

sub check_file_stat us
{
$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.go ogle.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
2823
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: - Removing SQL server from 'Program Files' folder following an unsuccessful attempt to re-install, and entirely removing the registry entry 'HKEY_CURRENT_USER > SOFTWARE > Microsoft > MSSQLServer', as well as the corresponding entry under...
2
2856
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: Dim xFile As StreamWriter
22
5598
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 : "SQL1478W The defined buffer pools could not be started. Instead, one small
1
8718
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 users. It gives "CANNOT LOCK FILE" error. There is really no difference between these two users and the other users who can log on to the database. The rights to the shared directory, where the database resides are the same. can somebody advice me...
1
2804
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 changes I have made have been to neither the form, nor the underlying query, nor to the underlying tables. I have a subform in Datasheet view based on the SQL below. Yesterday, when the user tried to add a new record to the subform, he had a dialog
4
2399
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 i get is the following The process cannot access the file "e:\inetpub\wwwroot\client_dev\brands\Product_images\1450.jpg" because it is
1
1244
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 another process." If I reboot the server it breaks the lock. If I reboot the client it breaks the lock. The only thing that has changed is their OS from Windows 2000 to XP. The application they
2
13693
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 shutdown postmaster successfully. Next time, when I tried to run postmaster -D /usr/local/postgresql/data, I got the following error: FATAL: lock file "/usr/local/postgresql/data/postmaster.pid" already exists
5
4098
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, even after the first process is finished with it, except in the case where the first process crashes. In pseudo code.. * Open file with exclusive lock * Process file - failure will throw exception, skipping 'Remove
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10395
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6887
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5553
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3865
muto222
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.