in reply to Can I Create A Persistent Flock Under CGI.pm?

In general Perl terms, a flock persists until you release it, or you close the filehandle. The recommended approach is to close the filehandle and let Perl clean up for you. If you don't close explicitly, Perl will do that for you when the process exits.

With regard to sharing a lock with multiple scripts, you may as well not flock at all. What's the point? A lock is there to prevent concurrent access from corrupting your data or your file. If you allow more than once process to access a single lock at a time, you've subverted that.

I suspect there's a better way to accomplish the goal, but it's hard to see what the goal actually is. :)

  • Comment on Re: Can I Create A Persistent Flock Under CGI.pm?

Replies are listed 'Best First'.
Re: Re: Can I Create A Persistent Flock Under CGI.pm?
by sierrathedog04 (Hermit) on May 30, 2001 at 00:02 UTC
    The Requirement: Allow a user to submit Form #1 that saves a stored CGI query object and presents the user with Form #2. When the user submits Form #2 it should retrieve the query object saved by Form #1, read its data and then delete the flat file that stores the query object.

    The Problem: What if the user logs in twice and submits Form #1 twice in a row? Then information from one login's submittal could bleed into the next. But if Form #1 could gain an exclusive lock on the query object file and hold it until Form #2 releases it then the problem would be solved: the potential race condition would be avoided because the second login would never get to submit Form #1 if someone else had locked it.

        Looking at the previous discussions on this, I've seen the code below. I was wondering if you were in a mod_perl enviroment, couldn't the same process call the function to generate the session twice in a second? I know with the random component that it's unlikely to have a collision but to be safe I've been using Timer::HiRes, is this overkill?
        # How I've seen it suggested use MD5; sub generate_id { return substr(MD5->hexhash(time(). {}. rand(). $$. 'blah'), 0, 16) +; } # Is this better or overkill? use Time::HiRes qw(gettimeofday usleep); use Digest::MD5 qw(md5_hex); sub generate_sessionID { my ($s, $usec) = gettimeofday; # Get time of day in seconds and + useconds usleep(1); # Sleep for one usec so a persistant process can't c +all it twice in a usec (for mod_perl) # Sessionkey is MD5 hash of seconds_since_epoch+usecs+process_id+r +andom return substr(md5_hex($s.$usec.$$.rand()), 0, 32); }


        -Lee

        "To be civilized is to deny one's nature."