Link to home
Start Free TrialLog in
Avatar of jwcs
jwcs

asked on

Locking Files - Can I Do It?

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
Avatar of amandeep
amandeep
Flag of United Kingdom of Great Britain and Northern Ireland image

jwcs,

see perldoc -f flock
Avatar of rag2000
rag2000

yes, flock is the answer
Avatar of jwcs

ASKER

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
Avatar of jwcs

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of amandeep
amandeep
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
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.
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)


Per recommendation, force-accepted by
Netminder
CS Moderator