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)

In reply to (jeffa) Re: what happens when... by jeffa
in thread what happens when... by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.