Solved

file lock

Posted on 2000-03-05
3
185 Views
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.
0
Comment
Question by:mwhuen
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
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

#!/usr/local/bin/perl
#
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:

#!/usr/local/bin/perl
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;
<html>
<title>My first return page</title>
<h1>Thank you for selecting this document.</h1>
You are visitor number $COUNT
</html>
END_OF_PAGE

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
0
 

Author Comment

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

Expert Comment

by:maneshr
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 http://www.tcx.se/ for more details on the same.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

10 Experts available now in Live!

Get 1:1 Help Now