Solved

flock() not working in my Perl script.. Why??

Posted on 2007-03-19
4
760 Views
Last Modified: 2013-11-17
I'm trying to test if a file is opened by another process but am not getting any results for the flock command in Perl. I have a very simple test script:

#!/usr/bin/perl
use Fcntl qw(:DEFAULT :flock);
use strict;
open(FH, "<test.txt");
print flock(FH,LOCK_EX|LOCK_NB);
close(FH);

No mater if the file is opened or not, it returns 0 all the time, which means that it is locked. What am I doing wrong? It is running on Unix AIX. Is there a system command I should use or is flock pretty accurate at the OS level? I'm asking because not only my script is accessing this file, but a completely different software program is access the file. So I want to be able to detect is the file is open at all but I'm not getting any results. Please help! Thanks in advance.
0
Comment
Question by:bemara57
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 18749181
flock doesn't check whether a file is opened, it checks whether another process has a flock on the file
I notice you do not check whether the open succeded or not.
If the open failed, then so will the flock.
0
 

Author Comment

by:bemara57
ID: 18749771
So if another process is using the file but doesn't use any flock, how can I determine if the file is used by that other process? Any alternatives to flock? All i want to do is detect if the file is in use so I can modify it, otherwise I want it stuck in a loop until it is freed up (maybe 10 minute loop-kill).
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 18751823
All locking mechanisms work on the same method, ie:  *all* processes that write to a file have to use the *exact* same locking mechanism, otherwise it's useless.

If you have no control over the other processes that can write to the file, then probably your best bet is to use the output of 'lsof' to see if the file is already opened.
0
 
LVL 84

Expert Comment

by:ozo
ID: 20402062
using lsof can suffer from a race condition if another process opens a file in between the time you do the lsof and the time you modify the file.
If  the other process won't cooperate in using locking mechanism, and it is only doing reads,
and you are the only one doing writes, you might be able to do something like make a copy of the file, do your modifications on the copy, then rename the copy to the original.
Then any other process that opens the file will either get the original or the modification, and not some intermediate state.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question