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

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.

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

Replies are listed 'Best First'.
Re: Re: Re: Can I Create A Persistent Flock Under CGI.pm?
by merlyn (Sage) on May 30, 2001 at 01:03 UTC
      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."
        My program is not in a mod_perl environment, so your solution might work for others but not for me.

        mod_perl seems to require that I recompile Apache and I am frightened of doing it. In addition, I think that mod_perl is a Fool-Killer and breaks scripts that are written by fools like me who do not always do things the CGI.pm way.

        Actually, it looks as though the semaphore solution offered by Japhy below will work and is within my capabilities to implement.