Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-03-19
4
Medium Priority
?
773 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 1000 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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.
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.

610 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