?
Solved

Locking Files - Can I Do It?

Posted on 2001-06-16
8
Medium Priority
?
216 Views
Last Modified: 2010-03-05
Hello!

Imagine I have a file containing some data.  A CGI script is called and that data is edited.  The seqence is like this:-

     Script Called
     Opens File
     Reads Data
     Close File
     Proccess Data With CGI Inputs
     Opens File
     Overwrites With New Data
     Close File

Here's the problem.  The proccessing takes a little while.  It is possible that another instance of the script could be called during this time, which would of course mess it up (as part of the data is an incrementing value - and yes, I do need to keep it as one file).

Does anyone know of any way in Perl to "lock" a file so no other script can access it while the other is running and proccessing it?  It is essential that the other script will wait for the file to be available to, not just dump the proccessing!  :-)

Any ideas please?  It may be simple.....may be not!  By the way, I believe my web server runs some version of UNIX or Linux - but I could do with it to be fairly cross-platform.  I'm not bothered whether it works under Windows really though.....

Thanks a lot!

Jonathan
0
Comment
Question by:jwcs
[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
8 Comments
 
LVL 5

Expert Comment

by:amandeep
ID: 6198667
jwcs,

see perldoc -f flock
0
 
LVL 3

Expert Comment

by:rag2000
ID: 6199348
yes, flock is the answer
0
 

Author Comment

by:jwcs
ID: 6200149
Hi!

Thanks - that looks to be what I am after.  However, there's one thing I do need to check - that is what happens if the file is locked and another script tries to access it.  Will "open" simply wait until the file is unlocked?  Or have I missed something?

Thanks again!

Jonathan
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.

 

Author Comment

by:jwcs
ID: 6200163
Hi!

Thanks - that looks to be what I am after.  However, there's one thing I do need to check - that is what happens if the file is locked and another script tries to access it.  Will "open" simply wait until the file is unlocked?  Or have I missed something?

Thanks again!

Jonathan
0
 
LVL 5

Accepted Solution

by:
amandeep earned 400 total points
ID: 6200344
jwcs,

Yes if another script requests "open" for the locked file, it will wait. But it totally depends how u locked it.

If u see the perldoc for flock, u will find the details. By defailt it waits.

Hope this helps
:-)
0
 
LVL 84

Expert Comment

by:ozo
ID: 6200568
No, open will not wait.
When the other script tries to flock the same file the flock will wait until the first script unlocks the file.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6967719
All,

This question seems to be abandoned. As part of the ongoing cleanup effort, I will leave a note in
the Community Support topic area which will recommend to the Moderators that they close this question.

My recommendation will be to award the points to amandeep

PLEASE DO NOT AWARD THE POINTS TO ME.

Experts ===>: please leave your thoughts here. <====

maneshr
(NOT a moderator at EE)


0
 
LVL 5

Expert Comment

by:Netminder
ID: 6984665
Per recommendation, force-accepted by
Netminder
CS Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

741 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