Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2004
    Posts
    295

    If a file is read/written to multiple times simultaneously the content gets lost,why?

    If a Perl/PHP script or even a C program accesses a file multiple times the contents of the file get lost. For example if you have a counter.pl file which has 100 accesses per second or something the contents of the file will be lost every couple of minutes. What can one do to prevent this ? It must be possible since MySQL obviously works without losing data.

    Locking the file does not help !

  2. #2
    Php/perl locking is only advisory. I think you have to use file locking correctly for it to work. Ie. if you make an exclusive lock on a file before changing it, but in another place in one of your scripts/threads open the file for reading without getting a read lock you will not necesarily be getting the right "version" of the file. You must use locking/unlocking at each and every place you access the file. If you access a lot of files, you may use "bounding" lock files. Ie. just have an empty file placed somewhere that you use to lock/unlock whenever you access or change any of your files (this makes it easier if you understand my meaning) - you must use consistancy in your locking. Data gets lost when locking is not implemented (correctly) whenever your application is multithreaded - all web applications (php/perl/asp) are by their way of working multithreaded (ie. multiple visitors at the same time calling the script) C/C++ programs or whatever else programming language may also be multithreaded depending on: do they spawn multiple threads for processing/allow multiple instances.

  3. #3
    Join Date
    Sep 2002
    Location
    Illinois
    Posts
    2,304
    Here is a tutorial I worte about this issue

    http://phpnull.com/showthread.php?t=42
    How's my programming? Call 1-800-DEV-NULL

  4. #4
    Join Date
    Sep 2002
    Location
    Illinois
    Posts
    2,304
    It is also recommended to LOCK TABLES when using mysql
    How's my programming? Call 1-800-DEV-NULL

  5. #5
    Join Date
    Apr 2004
    Posts
    52
    As was said above, everything has to use flock in order for it to work.

    Not only do you have to use flocking right, you have to understand the open call you are using to open the file. You can not use an open that is going to truncate the file when it opens. If you do that, the file will be truncated before it is locked, which defeats the purpose of locking it.

    null's tutorial, besides the fact that the "correct" code doesn't even work because the variable and file names are different within it, it is not correct and won't work. People who use it are going to be in for a surprise, since it will not prevent the file from being clobbered.

    Why? Because as I said above, you have to understand how the open function you are using works.

    Code:
    $fp=fopen("hits.dat","w+");
    flock($fp, 2); // lock
    From the PHP manual for fopen, the section on w+: " Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.". See the part in bold? Truncate the file to zero length. When $fp=fopen("hits.dat","w+"); executes, the file is going to be truncated. However, the lock is not done until the next line! You are truncating the file before locking it! How can trying to lock the file before writing do you any good if you destroy the file before the lock?

    The problem is easily seen if you put a few hits in the file and then insert a sleep right after the fopen for writing and before the flock. Run two processes at the same time, the count will get reset because process two is going to read from a null file.

    How do you solve this? Simple, you use an open method that doesn't truncate.

    Since this thread is on flocking, not hit counters, I'm not even going to address the race condition created in null's tutorial by doing the read and write in seperate opens.

    taketo, to answer your original question, as beowulfdk suggested, the locking is probably not being done correctly. If you are using flock, it may be due to the open issue I described above. flock does work, it just needs to be done correctly and obviously used consistently.

  6. #6
    Join Date
    Aug 2002
    Location
    Hong Kong
    Posts
    417
    does perl have "open for read and write" function?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •