Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i am a newbie to this wonderful world of perl. please explain what happens when... there is cgi script which reads and writes to a few files and this cgi-script is accessed by many users at the same instant of time as is the case on the web. how are these files handled by perl?

Replies are listed 'Best First'.
(jeffa) Re: what happens when...
by jeffa (Bishop) on Jan 17, 2001 at 00:45 UTC
    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)
    
Re: what happens when...
by Beatnik (Parson) on Jan 17, 2001 at 00:38 UTC
    if the files are not locked, which in some cases doesnt matter/work, scripts that read/rewrite files can clash. One script is reading a file that suddenly disappears (rewritten by another script). There are ofcourse ways to prevent it :) Either use flock (on platforms that support it;pray some none flock obeying process doesnt alter your file), or use system calls to avoid I/O errors, roll your own flock routine (a huge NoNo according to many resident monks), or use one of the more advanced file locking modules listed on CPAN.