This is a very non-technical explanation:
First, understand that each request for a web page is
handled by a web server - usually in the form of what's
known as a child process. Just think of it this way,
Vito is the web server boss - when a client wants some
kind of service - Vito tells Louie or Frankie, or maybe
even Lucco Braccia to handle this service, this way,
Vito (the web server) doesn't have to worrry about it.
Let's say that Frankie handles the request - "Ahhh, this
request is for a CGI script," he notices. So, Frankie
executes the script.
Remember that this script opens some file on the system,
probably to record the latest business transcation - looks
like Sonny didn't pay all his share this week - so Frankie
records this down, and designates the special case with an
X.
At the same time - Vito gets another request, this time
he tells Lucco to take care of business. Lucco performs
the request, which happens to be the same CGI script that
Frankie executed. Looks like Sonny came up with all the
dough after all - so Lucco first checks for an entry
for Sonny with the special flag so he can fix it, see?
But what if Lucco and Frankie both tried to access that
file at the same time, and no precautions were taken to
avoid a bad thing (called a race condition). It is very
possible that Frankie recorded the entry in the
books with the X flag JUST before Lucco read the file. Then,
Frankie and Lucco will both put an entry in the file, not
a good thing, because Vito just sent a request to Eddie
'Stiff Fingers' Malone who is fixin to put a hit on Sonny.
Poor Sonny, at least we got our money.
Now, if this CGI script had used a technique called 'File
Locking' this would not happen:
open(FH, "+</etc/book") or die "$0 can't open the books\n";
flock(FH,2);
# the file is now locked, any processes trying
# to open this file will wait until I am finished
# notice the arg '2' . . . this means exclusive lock
# this area between the lock and the unlock is
# called the critical section - keep your code
# short, sweet, and fast as possible to prevent
# the file from being locked for too long
# do stuff
close(FH); # in Perl, close() will unlock your file
Type perldoc -f flock at your console command line
for more help, or try this link: flock. Also, check out
flock - lordy me.... - good stuff.
Jeff
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)
|