Blocking with fopen when file is already open for writing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Efi Merdler

    Blocking with fopen when file is already open for writing

    Hi,
    I created a very simple application that reads and writes to a file, I
    was wondering if fopen when opening a file for write access blocks
    this file, i.e. when another user runs this script the script will
    halt until the first user closes its file handle.

    If not how can I change php behavior to block such access?

    Thank you,
    Efi

  • Gordon Burditt

    #2
    Re: Blocking with fopen when file is already open for writing

    >I created a very simple application that reads and writes to a file, I
    >was wondering if fopen when opening a file for write access blocks
    >this file, i.e. when another user runs this script the script will
    >halt until the first user closes its file handle.
    You can pretty much count on PHP *not* automatically doing this for
    you. For one thing, there are safe ways of two processes both
    having the same file open for write and *not* clobbering each other
    (e.g. each writes only to its own pre-existing record) that PHP
    shouldn't prohibit it.
    >If not how can I change php behavior to block such access?
    All users of the file can use flock().


    Comment

    • Colin McKinnon

      #3
      Re: Blocking with fopen when file is already open for writing

      Efi Merdler wrote:
      Hi,
      I created a very simple application that reads and writes to a file, I
      was wondering if fopen when opening a file for write access blocks
      this file, i.e. when another user runs this script the script will
      halt until the first user closes its file handle.
      >
      What happenned when you tested it?

      Here's a hint:
      <?php
      print "ready to start\n";
      flush();
      $fh=fopen('/tmp/test','w');
      if (is_resource($f h)) {
      print "opened...\ n";
      flush();
      sleep(30);
      print "Exiting\n" ;
      fclose($fh);
      } else {
      print "failed to open\n";
      }
      ?>

      (if you run two instances of this script, the second will do one of three
      things - you've only considered two possibilities so far)
      If not how can I change php behavior to block such access?
      >
      flock()

      C.

      Comment

      • Efi Merdler

        #4
        Re: Blocking with fopen when file is already open for writing


        Colin McKinnon wrote:
        Efi Merdler wrote:
        >
        Hi,
        I created a very simple application that reads and writes to a file, I
        was wondering if fopen when opening a file for write access blocks
        this file, i.e. when another user runs this script the script will
        halt until the first user closes its file handle.
        >
        What happenned when you tested it?
        >
        Here's a hint:
        <?php
        print "ready to start\n";
        flush();
        $fh=fopen('/tmp/test','w');
        if (is_resource($f h)) {
        print "opened...\ n";
        flush();
        sleep(30);
        print "Exiting\n" ;
        fclose($fh);
        } else {
        print "failed to open\n";
        }
        ?>
        >
        (if you run two instances of this script, the second will do one of three
        things - you've only considered two possibilities so far)
        >
        If not how can I change php behavior to block such access?
        >
        flock()
        >
        C.
        Thanks,
        I'll probably use a semaphore in order achieve this block behavior.

        Efi

        Comment

        • Drazen Gemic

          #5
          Re: Blocking with fopen when file is already open for writing

          On Mar 4, 12:21 am, Colin McKinnon
          <colin.thisisno tmysurn...@ntlw orld.deletemeun lessURaBot.comw rote:

          I think that Windows will lock file exclusively, by default. UNIX and
          similar systems will
          not, by default.

          DG

          Comment

          • NC

            #6
            Re: Blocking with fopen when file is already open for writing

            On Mar 3, 5:06 am, "Efi Merdler" <foo...@gmail.c omwrote:
            >
            I created a very simple application that reads and writes to a file, I
            was wondering if fopen when opening a file for write access blocks
            this file, i.e. when another user runs this script the script will
            halt until the first user closes its file handle.
            Nothing of the sort. You need to expressly lock the file using
            flock(),
            which, alas, will not help you on Windows due to Windows' thread
            model.

            Cheers,
            NC

            Comment

            • NC

              #7
              Re: Blocking with fopen when file is already open for writing

              On Mar 4, 6:15 am, "Drazen Gemic" <trench...@yaho o.comwrote:
              >
              I think that Windows will lock file exclusively, by default.
              Maybe, but since all instances of the script will run under the same
              process ID (although they may have different thread IDs), it's not
              going to help; the file will remain writeable to all instances of the
              script.

              Cheers,
              NC

              Comment

              • Efi Merdler

                #8
                Re: Blocking with fopen when file is already open for writing


                Colin McKinnon wrote:
                Efi Merdler wrote:
                >
                Hi,
                I created a very simple application that reads and writes to a file, I
                was wondering if fopen when opening a file for write access blocks
                this file, i.e. when another user runs this script the script will
                halt until the first user closes its file handle.
                >
                What happenned when you tested it?
                >
                Here's a hint:
                <?php
                print "ready to start\n";
                flush();
                $fh=fopen('/tmp/test','w');
                if (is_resource($f h)) {
                print "opened...\ n";
                flush();
                sleep(30);
                print "Exiting\n" ;
                fclose($fh);
                } else {
                print "failed to open\n";
                }
                ?>
                >
                (if you run two instances of this script, the second will do one of three
                things - you've only considered two possibilities so far)
                >
                If not how can I change php behavior to block such access?
                >
                flock()
                >
                C.
                Hi,
                Thanks for the hint.
                I tried to use a semaphore, however my php was not compiled with the
                right flag, so I'm left with flock, I tested its behavior using your
                scripts, it works as expected.

                Efi

                Comment

                Working...