Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

do i need to worry about multiple scritpts accessing one file?

Status
Not open for further replies.

bobbybobbertson

Programmer
Nov 9, 2001
119
0
0
US
Hello,

Say I write a feedback form, where I get a bunch of data from the user and send that data to a cgi script. With that CGI script, I then put the user's data into a file called 'information.txt'.


Now, lets say that my feedback form gets called by two people that submit it close enough to eachother that while one person is submitting the other submition has the 'information.txt' file open.

if my open statement is:
open FILE, "information.txt or die "couldn't open";

Will the second submitter get an internal error page?
How do I write a feedback for such that this problem won't occor (if it does)?
 
Yes, you do need to worry.

If you're using a unix type system you should lock the file with flock(<FILE_HANDLE>, 2), where 2 is exclusive lock. flock is only voluntary, and will only preserve the file form other scripts using flock as well. Un(f)lock with flock(<FILE_HANDLE>, 8) when you're done.

If you have a windows system I'm not sure what the equivalent mechanism is (if there is one).

I don't know how you want to use the file but opening for append rather than read/write will help to prevent multiple writes from separate scripts from wrecking the file.

paulf
 
ok, thanks.

I read up on 'flock' and it seems that it waits for the opportunity to get a lock on the file.

What I havn't found yet is does 'flock' lock the file from read and write? or just write?

Can I read from a file when there is an 'flock' on it?

Also, what exactly is a shared lock?
 
Ok - UNIX file locks are a little .... lax

Unix Locks are &quot;advisory&quot; - if you try to write to a locked DOS or NTFS or FAT32 file the operation will fail, but not a UNIX file. UNIX assumes we know what we are doing and lets us do pretty much anything, including writing to a file already locked by someone else... So you have to write code that looks like this:

Try to lock the file.
if the lock attempt failed, wait a second and try again.
if the lock attempt worked, open the file.

This is complicated a bit by the fact that you can request a lock (never mind what kind of lock for the moment) in two different ways: Blocking and Non-Blocking

Blocking Mode. Your script will wait until the lock request (the call to flock()) succeeds. Simple enough, but not very flexible.

Non-Blocking Mode. Your flock() call will return a value, true if the request succeeded and false if it failed. Flexible, you can tell your user to try again in a minute, but not very simple.

Types of lock:

Shared locks. Go for one of these when you don't want to write to the file but *do* need to be sure that no one is changing it while you read it. Many shared locks can be held on a file at once. You can't get a shared lock on a file that something else already has an exclusive lock on.

Exclusive locks. Go for an exclusive lock when you need to write to a file that might be written to by something else. Only one exclusive lock at one time. You can't get an exclusive lock on a file that something else already has an shared lock on. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top