CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
prevent concurrent writing to a text file

 



biyan
Deleted

Oct 31, 2000, 3:40 PM

Post #1 of 9 (3495 views)
prevent concurrent writing to a text file Can't Post

I've a poll script that allows web users to vote. For any one poll, a data file (txt file) is created to keep track of the poll options and the number of votes received. However, if 2 users access the file at the same time, the data file will be over-written and its content will become blank.

Anyone knows how I can solve this problem? Quite a lot of people access the poll, and this problem has occurred 3 times yesterday.

Any help or hint is greatly appreciated.



sleuth
Enthusiast

Oct 31, 2000, 5:53 PM

Post #2 of 9 (3495 views)
Re: prevent concurrent writing to a text file [In reply to] Can't Post

 
Try using flock (File Locking), note that this only works on unix type systems, well not win nt anyway.

use Fcntl;
open(file, ">>file.db");
flock(file, 2);
print file "bla\n";
close(file);

the 2 means to use the file exclusively and 1 means to share it. perldoc should have more on flock.

Sleuth


biyan
Deleted

Oct 31, 2000, 7:13 PM

Post #3 of 9 (3495 views)
Re: prevent concurrent writing to a text file [In reply to] Can't Post

Thank you very much, Sleuth.
Will try out and let you know if I'm successful.

Have a good day.


biyan
Deleted

Oct 31, 2000, 9:44 PM

Post #4 of 9 (3495 views)
Re: prevent concurrent writing to a text file [In reply to] Can't Post

I tried using the suggested statements, but I get an "Internal Server Error" very often. How can I get around this? Is it because someone is trying to access the locked file?
Can someone enlighten me on this?

Arigato gozaimesu.


sleuth
Enthusiast

Nov 1, 2000, 9:14 PM

Post #5 of 9 (3495 views)
Re: prevent concurrent writing to a text file [In reply to] Can't Post

 
Sorry, I skip this forum sometimes cause it's not in my first view.

That should be ok, there is no coding problem.

You should go to the begginers forum and under the topic "help :)" i posted a script from a site that is called server check, it will list out the modules installed on your server. Get that & run it to see if "Fcntl" is on your server. If it isn't then their are different ways of locking files that I can research for you.

Also, make sure that you have permissions on the directory that your script is in set to 773, so that the script can write to the directory, I doubt it's that though.

Sleuth


biyan
Deleted

Nov 5, 2000, 7:43 PM

Post #6 of 9 (3495 views)
Re: prevent concurrent writing to a text file [In reply to] Can't Post

Your guess is right. 'Fcntl' is indeed not found on my server. Where can I get a copy?
Or what other ways of locking files would you recommend?

Thanks,
BiYan


sleuth
Enthusiast

Nov 5, 2000, 7:52 PM

Post #7 of 9 (3495 views)
Re: prevent concurrent writing to a text file [In reply to] Can't Post

 
You could probably complain, he he

This is also a good way.

Put these two subs at the top of your script,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub Lock {
local ($lockname) = shift;
local ($endtime);
$endtime = time + 10;

while ((-e "$lockname") && (time < $endtime)) {
sleep(1);
}
open (LOCKFILE, ">$lockname");
}

sub Unlock {
local ($lockname) = shift;
close (LOCKFILE);
unlink ("$lockname");
}
</pre><HR></BLOCKQUOTE>

And when you want to open a file do this.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


&Lock("lock.file");
open(db, ">file.db");
print db "bla";
close(db);
&Unlock("lock.file");
</pre><HR></BLOCKQUOTE>

lock.file is the name of the lock file you will be creating. As long as the file doesn't exist in the directory that your script is in, your ok with that name.

Sleuth


sleuth
Enthusiast

Nov 5, 2000, 7:54 PM

Post #8 of 9 (3495 views)
Re: prevent concurrent writing to a text file [In reply to] Can't Post

 
Oh yea, and chmod the directory that your script is in to 773

Sleuth


biyan
Deleted

Nov 5, 2000, 8:09 PM

Post #9 of 9 (3495 views)
Re: prevent concurrent writing to a text file [In reply to] Can't Post

What's there for me to complain? If there is a need for me to write, it's more to send in a compliment rather than a complaint.

Thanks a lot for the quick reply. I'll try it out and let you know the result.

Pardon my ignorance - for I'm a real beginner in Perl programming.

BiYan

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives