Solved

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

Posted on 2007-03-19
4
764 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

688 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