file lock

Posted on 2000-03-05
Last Modified: 2013-12-25
I don't know when should I use file lock, and how to use it.
if i don't use file lock, any problem? why there is problem? how is the problem?

can any one explain in details with examples to me?
thanks very much.
Question by:mwhuen
  • 2
LVL 16

Accepted Solution

maneshr earned 10 total points
ID: 2585341
Q) What is file locking??

A) To make a file or other piece of data inaccessible. File locking is a critical component of all multi-user computer systems, including local-area networks. When users share files, the operating system must ensure that two or more users do not attempt to modify the same file simultaneously. It does this by locking the file as soon as the first user opens it. All subsequent users may read the file, but they cannot write to it until the first user is finished.
Here is an example of file locking in PERL.

This example is based on "Building and using counters"

Counters are widely used on the World Wide Web to see how many people examine HTML documents. A counter is usually implemented as an ordinary file that is used to hold a single value--the number of accesses to a particular document. To build a counter, you must first create a count file. This file contains only the (ASCII) number at which you would like the counter to start. You can build a count file using any editor on your system. Assuming this file is called counter.file, a Perl script to use it would look like

open(COUNTER, "+< /tmp/counter.file");  
             # open the counter file with read and write access.
$COUNT= <COUNTER>;           #read the current value.
$COUNT++;                    #increment it by one.
seek(COUNTER, 0 , 0);        #rewind the file.
print COUNTER $COUNT;        #write the new value to the file.
close COUNTER;
The line $COUNT++; adds one to the value of the variable $COUNT.

To include the number from the count file in a document, use the print command as before. For example, you might build a Perl script like the following:

print "Content-type:text/html\n\n";
#First, increment the counter:
open(COUNTER, "+< /tmp/counter.file");
             # open the counter file with read and write access.
$COUNT= <COUNTER>;           #read the current value.
$COUNT++;                    #increment it by one.
seek(COUNTER, 0 , 0);        #rewind the file.
print COUNTER $COUNT;        #write the new value to the file.
close COUNTER;
#Then use the counter in the returned HMTL.
print <<END_OF_PAGE;
<title>My first return page</title>
<h1>Thank you for selecting this document.</h1>
You are visitor number $COUNT

This script reads the counter file into a variable called COUNT, increments the counter variable by one, stores the new value in the counter file, and then returns an HTML document that uses the counter variable.

Sharing files among scripts
If multiple users run this script at the same time, strange things may occur. It is possible, for example, that the two running scripts would read the current count at the same time, increment it by one at the same time, and store the new value in the file, reflecting access by just one user instead of both. The chances of experiencing difficulties due to such concurrent access vary according to the type and frequency of access. In many cases, the frequency of document access is small, so the chances of simultaneous access are small enough to ignore.
However, Perl does include facilities for synchronizing file accesses. Among others, the flock function allows users to lock a file for private use. You could use the line

flock (COUNTER, 2);
to lock the file immediately after opening the file and the line

flock (COUNTER, 8);
to unlock the file immediately before closing it.

This problem applies to other Perl building blocks and to scripts that simultaneously write to, or simultaneously write to and read from the same file, written in any language . In general, the problem can be controlled by giving any writing script private access. That is, if a script is writing, no other writers or readers will be given access until that script is finished. The Perl flock function grants such privacy.

On the other hand, any solution will have drawbacks. For example, it may be inconvenient to lock the file for a long period of time, in which case other approaches may be needed

Q) How can I lock a file?

A) Perl's built-in flock function will call flock if that exists, fcntl if it doesn't, and lockf if neither of the two previous system calls exists. On some systems, it may even use native locking. Some gotchas with Perl's flock:

* Produces a fatal error if none of the three system calls (or their equivalent) exists.

* lockf does not provide shared locking, and requires that the filehandle be open for writing (or appending, or read/writing).

* Some versions of flock can't lock files over a network (e.g. on NFS file systems), so you'd need to force the use of fcntl when you build Perl

The CPAN module File::Lock offers similar functionality and (if you have dynamic loading) won't require you to rebuild perl if your flock can't lock network files.

Hope that helps

Author Comment

ID: 2612241
If i am now using database to store data,
do i need locking too?
I use mysql, how to lock ?
LVL 16

Expert Comment

ID: 2612436
you would need locking if more than one process is trying to update data in a given column and the value of that column is crucial to the proper working of the application.

you can check for more details on the same.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now