Solved

Locking Files - Can I Do It?

Posted on 2001-06-16
8
166 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
8 Comments
 
LVL 5

Expert Comment

by:amandeep
Comment Utility
jwcs,

see perldoc -f flock
0
 
LVL 3

Expert Comment

by:rag2000
Comment Utility
yes, flock is the answer
0
 

Author Comment

by:jwcs
Comment Utility
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
 

Author Comment

by:jwcs
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Accepted Solution

by:
amandeep earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Per recommendation, force-accepted by
Netminder
CS Moderator
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now