?
Solved

Check whether a file is locked or not

Posted on 2003-03-19
7
Medium Priority
?
687 Views
Last Modified: 2009-07-29
Hi
In perl I can lock a file using flock(). But is there any way of knowing whether a file to be opened is already locked using flock() or not.
Regards
Nitin
0
Comment
Question by:nitindn
[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
7 Comments
 
LVL 3

Expert Comment

by:prady_21
ID: 8172169
this is what i used in a pgm from perl cook book, hope it helps, it is straight of the book

unless(flock(FH, LOCK_EX|LOCK_NB)) {
 warn "cant immediately write-lock the file , blocking..";
 unless (flock(FH, LOCK_EX)) {
    die "cant get write lock on file:$!";
  }
}

If LOCK_NBis used and LOCK_SH is refused, then some one has a LOCK_EX on the file, if LOCK_EX is refused then some one else holds LOCK_SH or LOCK_EX, so do not update the file.
0
 
LVL 2

Expert Comment

by:Itatsumaki
ID: 8177953
Yup, if you can't flock(2) a file, then someone else has it locked.

if (!flock(OUT, 2)) {
  die "Can't lock file!\n";
  }
0
 

Author Comment

by:nitindn
ID: 8179310
Hi,
What you all say is fine, but in my case I have two perl scripts running simultaneously. Both are trying to write to a file.
My need is that if one of the scripts find out that the file has been locked by the other one, then it should exit. But what is happening is that the second script wais for the first script to release the lock and then it proceeds.
The code for the script is something like this:
Script 1:
open( _MCpidfile_ , ">C:\\TestThreadsforMC\\abc.dat" );
flock(_MCpidfile_, 2 ) or die "cannot lock file: C:\\TestThreadsforMC\\abc.dat ";
for ($count = 1 ;$count < 51; $count++ )
{
     print _MCpidfile_ "\n$count" ;
}
close (_MCpidfile_) ;

Script 2:
open( _MCpidfile_ , ">>C:\\TestThreadsforMC\\abc.dat" );
if (! flock ( _MCpidfile_ , 2))
{
     print "\ncan't lock file" ;
        exit(1);
}
print _MCpidfile_ "\nhi..just testing" ;
close (_MCpidfile_);

Now can you suggest what i should do..?
Regards
Nitin
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Accepted Solution

by:
ozo earned 225 total points
ID: 8179360
use LOCK_NB as prady_21 said
flock(_MCpidfile_, 2|4) or die "cannot lock file: C:\\TestThreadsforMC\\abc.dat ";
0
 

Author Comment

by:nitindn
ID: 8179400
Hi,
Can you tell me what does this LOCK_NB do and hat it means..
Regards
Nitin
0
 
LVL 84

Expert Comment

by:ozo
ID: 8179437
see
perldoc -f flock
If LOCK_NB is bitwise-or'ed with LOCK_SH or
LOCK_EX then `flock' will return immediately
rather than blocking waiting for the lock (check
the return status to see if you got it).
0
 

Author Comment

by:nitindn
ID: 8179533
Hi,
Can you tell me what does this LOCK_NB do and hat it means..
Regards
Nitin
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

771 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