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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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