Concurrent access and locks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruno Barberi Gnecco

    #1

    Concurrent access and locks

    I'm developing a collaborative application in PHP which accesses
    local files and may modify them. How can I synchronize the multiple
    accesses? Note that I'm not worried about the algorithmic part of the
    problem, or how to avoid conflicts, which I know how to solve; I just
    need a few atomic primitive shared among any PHP processes that may be
    running. I could even have a separate process serializing all operations
    if such a thing is possible. flock() seems to be too limited for my needs.
    Thanks a lot,

    --
    Bruno Barberi Gnecco <brunobg_at_use rs.sourceforge. net>
    http://www.lsi.usp.br/~brunobg/
  • ZeldorBlat

    #2
    Re: Concurrent access and locks

    What about flock() is too limited for your needs?

    Comment

    • Bruno Barberi Gnecco

      #3
      Re: Concurrent access and locks

      ZeldorBlat wrote:[color=blue]
      > What about flock() is too limited for your needs?[/color]

      Doesn't work over NFS and other FSs, is slow, does not
      let me lock directory hierarchies (which is important for atomically
      changing many files at once) or even many files atomically.

      --
      Bruno Barberi Gnecco <brunobg_at_use rs.sourceforge. net>
      http://www.lsi.usp.br/~brunobg/

      Comment

      • mlemos

        #4
        Re: Concurrent access and locks

        Hello,

        on 04/03/2005 11:35 AM Bruno Barberi Gnecco said the following:[color=blue]
        > ZeldorBlat wrote:[color=green]
        >> What about flock() is too limited for your needs?[/color]
        >
        > Doesn't work over NFS and other FSs, is slow, does not[/color]

        Not true. That is only a problem of some implementations .

        If you are concerned with speed, you should not be using NFS anyway.

        [color=blue]
        > let me lock directory hierarchies (which is important for atomically
        > changing many files at once) or even many files atomically.[/color]

        flock is exactly for what you need. If you want to change many files
        with a single lock, you can pick a special file and lock it when you
        want to access or change many files.


        --

        Regards,
        Manuel Lemos

        PHP Classes - Free ready to use OOP components written in PHP
        http://www.phpclasses.org/

        PHP Reviews - Reviews of PHP books and other products
        http://www.phpclasses.org/reviews/

        Metastorage - Data object relational mapping layer generator

        Comment

        • Bruno Barberi Gnecco

          #5
          Re: Concurrent access and locks

          mlemos wrote:
          [color=blue][color=green][color=darkred]
          >>> What about flock() is too limited for your needs?[/color]
          >> Doesn't work over NFS and other FSs, is slow, does not[/color]
          > Not true. That is only a problem of some implementations .
          > If you are concerned with speed, you should not be using NFS anyway.[/color]

          I'm not using it, but I wanted to support it. Let's not into
          another "why NFS sucks" thread because I think we all agree with that :)
          [color=blue][color=green]
          >> let me lock directory hierarchies (which is important for atomically
          >> changing many files at once) or even many files atomically.[/color]
          > flock is exactly for what you need. If you want to change many files
          > with a single lock, you can pick a special file and lock it when you
          > want to access or change many files.[/color]

          Sorry, but I can't see how that solves the problem. If I create
          a file containing the names of the locked files and lock it, there's
          still the possibility of racing. If I use just one file for all
          processes, listing all locked files, how can I know when a certain
          file is unlocked?

          --
          Bruno Barberi Gnecco <brunobg_at_use rs.sourceforge. net>
          http://www.lsi.usp.br/~brunobg/
          It is easy when we are in prosperity to give advice to the afflicted.
          -- Aeschylus

          Comment

          • mlemos

            #6
            Re: Concurrent access and locks

            Hello,

            on 04/03/2005 06:18 PM Bruno Barberi Gnecco said the following:[color=blue][color=green][color=darkred]
            >>>> What about flock() is too limited for your needs?
            >>> Doesn't work over NFS and other FSs, is slow, does not[/color]
            >> Not true. That is only a problem of some implementations .
            >> If you are concerned with speed, you should not be using NFS anyway.[/color]
            >
            > I'm not using it, but I wanted to support it. Let's not into
            > another "why NFS sucks" thread because I think we all agree with that :)[/color]

            NFS does not suck. Files under NFS are remotely stored and you must deal
            with the network access overhead. That could be useful in many
            circumstances on which such overhead is bearable.

            Your inconsistent is that while you claim flock is slow (I don't know
            where did you get this idea), you are not concerned with supporting NFS
            when it is much slower due to network overead.

            [color=blue][color=green][color=darkred]
            >>> let me lock directory hierarchies (which is important for atomically
            >>> changing many files at once) or even many files atomically.[/color]
            >> flock is exactly for what you need. If you want to change many files
            >> with a single lock, you can pick a special file and lock it when you
            >> want to access or change many files.[/color]
            >
            > Sorry, but I can't see how that solves the problem. If I create
            > a file containing the names of the locked files and lock it, there's
            > still the possibility of racing. If I use just one file for all
            > processes, listing all locked files, how can I know when a certain
            > file is unlocked?[/color]

            You can use shared locks for reading and exclusive locks for writing.


            --

            Regards,
            Manuel Lemos

            PHP Classes - Free ready to use OOP components written in PHP
            http://www.phpclasses.org/

            PHP Reviews - Reviews of PHP books and other products
            http://www.phpclasses.org/reviews/

            Metastorage - Data object relational mapping layer generator

            Comment

            • Bruno Barberi Gnecco

              #7
              Re: Concurrent access and locks

              mlemos wrote:
              [color=blue][color=green]
              >> I'm not using it, but I wanted to support it. Let's not into
              >> another "why NFS sucks" thread because I think we all agree with that :)[/color]
              > NFS does not suck. Files under NFS are remotely stored and you must deal
              > with the network access overhead. That could be useful in many
              > circumstances on which such overhead is bearable.[/color]

              NFS has several known problems. It has an awful lag (not due to
              the network overhead, but to its caching), lack of a safe lock/unlock
              system to avoid inconsistency, etc...
              [color=blue]
              > Your inconsistent is that while you claim flock is slow (I don't know
              > where did you get this idea),[/color]

              The web. Since I may have to lock several hundred files in
              some operations, I was worried when I read that flock was slow.
              Now, if someone is running it on NFS, they are probably likely
              to have a small number of files or know the potential problems.
              [color=blue][color=green]
              >> Sorry, but I can't see how that solves the problem. If I create
              >> a file containing the names of the locked files and lock it, there's
              >> still the possibility of racing. If I use just one file for all
              >> processes, listing all locked files, how can I know when a certain
              >> file is unlocked?[/color]
              > You can use shared locks for reading and exclusive locks for writing.[/color]

              I still can't get it.
              From what I understood of your solution, I could keep a separate
              metafile with entries for all files that are currently "locked", and flock
              only this file. Suppose now that process A locks "file1" for reading, and
              process B wants to write to "file1". Do I need to stat the metafile
              manually every N milliseconds to check when process A released the lock?

              --
              Bruno Barberi Gnecco <brunobg_at_use rs.sourceforge. net>
              http://www.lsi.usp.br/~brunobg/
              It is easier to fight for one's principles than to live up to them.
              -- Alfred Adler

              Comment

              Working...