Solved

Locking Files - Can I Do It?

Posted on 2001-06-16
8
209 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
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!

 

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 100 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

Independent Software Vendors: 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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

729 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