question about using lock files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Gill

    question about using lock files

    I have a PHP user form that I supply a serial number for response
    tracking. The serial number is kept in a simple text file
    (tracknum.dat). When someone submits the form, PHP reads the number,
    increments it, writes the new number to the file, and sends the number
    back to the user for tracking purposes. To prevent two users from
    getting the same serial number I put a delay loop in the script using
    while(file_exis ts(tracknum.loc k). tracknum.lock is created before
    tracknum.dat is opened, and deleted after the new serial number is
    written. This seems to work, but it could cause an infinite loop if for
    some reason the lock file doesn't get deleted by the previous user. Am I
    being paranoid about the possible infinite loop? Is there a smarter way
    to do this?

    Thanks

    Bill
  • Treefrog

    #2
    Re: question about using lock files

    William Gill wrote:[color=blue]
    > I have a PHP user form that I supply a serial number for response
    > tracking. The serial number is kept in a simple text file
    > (tracknum.dat). When someone submits the form, PHP reads the number,
    > increments it, writes the new number to the file, and sends the number
    > back to the user for tracking purposes. To prevent two users from
    > getting the same serial number I put a delay loop in the script using
    > while(file_exis ts(tracknum.loc k). tracknum.lock is created before
    > tracknum.dat is opened, and deleted after the new serial number is
    > written. This seems to work, but it could cause an infinite loop if for
    > some reason the lock file doesn't get deleted by the previous user. Am I
    > being paranoid about the possible infinite loop? Is there a smarter way
    > to do this?[/color]

    Nope, it works with poplock files. If you're worried, find out how old
    the file is, if it's more than say 5 minutes, assume a previous error,
    and delete it/carry on.

    Comment

    • Sjoerd

      #3
      Re: question about using lock files


      William Gill wrote:[color=blue]
      > To prevent two users from
      > getting the same serial number I put a delay loop in the script using
      > while(file_exis ts(tracknum.loc k)[/color]

      You have a typical race condition:

      You wait until the file is deleted and then create it. Suppose that the
      file is deleted and created immediatly after file_exists. The file
      would be created by another process, e.g. another user requesting this
      page. After the while() loop, you assume that the file still does not
      exist in the next instruction, which is an unfair assumption.

      Comment

      • Andy Jeffries

        #4
        Re: question about using lock files

        On Thu, 04 May 2006 09:30:13 -0700, Sjoerd wrote:[color=blue][color=green]
        >> To prevent two users from
        >> getting the same serial number I put a delay loop in the script using
        >> while(file_exis ts(tracknum.loc k)[/color]
        >
        > You have a typical race condition:
        >
        > You wait until the file is deleted and then create it. Suppose that the
        > file is deleted and created immediatly after file_exists. The file would
        > be created by another process, e.g. another user requesting this page.
        > After the while() loop, you assume that the file still does not exist in
        > the next instruction, which is an unfair assumption.[/color]

        From what I remember when studying for the ZCE, mkdir is atomic and
        returns true or false as to whether the directory was created or not - and
        hence is the best choice for locking...

        Cheers,


        Andy

        --
        Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
        http://www.gphpedit.org | PHP editor for Gnome 2
        http://www.andyjeffries.co.uk | Personal site and photos

        Comment

        • Bob_M (remove X for email)

          #5
          Re: question about using lock files

          On Thu, 04 May 2006 14:56:30 GMT, William Gill <noreply@gcgrou p.net>
          wrote:
          [color=blue]
          >I have a PHP user form that I supply a serial number for response
          >tracking. The serial number is kept in a simple text file
          >(tracknum.dat) . When someone submits the form, PHP reads the number,
          >increments it, writes the new number to the file, and sends the number
          >back to the user for tracking purposes. To prevent two users from
          >getting the same serial number I put a delay loop in the script using
          >while(file_exi sts(tracknum.lo ck). tracknum.lock is created before
          >tracknum.dat is opened, and deleted after the new serial number is
          >written. This seems to work, but it could cause an infinite loop if for
          >some reason the lock file doesn't get deleted by the previous user. Am I
          >being paranoid about the possible infinite loop? Is there a smarter way
          >to do this?
          >
          >Thanks
          >
          >Bill[/color]
          Why not try "flock"ing the file(s). YOUR process will wait for the
          other (owner) task to release it before continuing.

          B

          Comment

          • Andy Jeffries

            #6
            Re: question about using lock files

            On Thu, 04 May 2006 18:51:28 -0400, Bob_M (remove X for email) wrote:[color=blue]
            > Why not try "flock"ing the file(s). YOUR process will wait for the
            > other (owner) task to release it before continuing.[/color]

            Errr, because of the warnings on the flock manual page:

            flock() will not work on NFS and many other networked file systems. Check
            your operating system documentation for more details.

            On some operating systems flock() is implemented at the process level.
            When using a multithreaded server API like ISAPI you may not be able to
            rely on flock() to protect files against other PHP scripts running in
            parallel threads of the same server instance!

            flock() is not supported on antiquated filesystems like FAT and its
            derivates and will therefore always return FALSE under this environments
            (this is especially true for Windows 98 users).


            It's not to say it won't work in his situation nor that it wouldn't be a
            better solution than the one he's using, but there are other ways.

            When studying for the ZCE I seem to remember either the study guide or one
            of the practice exams saying to use mkdir for locks as it's a guaranteed
            atomic operation and returns true/false for completion.

            Cheers,


            Andy


            --
            Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
            http://www.gphpedit.org | PHP editor for Gnome 2
            http://www.andyjeffries.co.uk | Personal site and photos

            Comment

            Working...