Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Intermediate
Feb 17, 1999

Locking a File With the Perl Flock Function

Many CGI programs, such as guestbook programs, require that only one user at a time be allowed to access a data file. You can lock a file with the Perl flock function (flock stands for file lock). You use it with a filehandle like so:
 
open (FILE, "data.txt");
flock (FILE, 2);
The file is now locked, and no one else can write to it until it is unlocked. It's possible to explicitly unlock the file with flock, but it's cleaner to let the system unlock the file when the filehandle is closed. Closing the file will unlock it and also flush the buffer.
Steve Renaker
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap