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

x
?
Solved

Locking Files - Can I Do It?

Posted on 2001-06-16
8
Medium Priority
?
226 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

604 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