SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Race Conditions & File Appendages

    Do race conditions usually exist when writing OR appending to an existing file?

    Code:
    $f1 = fopen($filename1, 'r+');
    $f2 = fopen($filename2, 'a');
    flock($f1, LOCK_EX);
    flock($f2, LOCK_EX);
    fwrite($f1, 'foo');
    fwrite($f2, 'foo');
    flock($f1, LOCK_UN);
    flock($f2, LOCK_UN);
    fclose($f1);
    fclose($f2);
    What would happen if this code ran at the exact same time? Is there a chance that the data could be corrupted in the aforementioned code example?

  2. #2
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately flock is not thread-safe, this means that if this code runs in the EXACT same time (which is very very unlikely), then you will probably get file corruptions.


Bookmarks

Posting Permissions

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