in reply to File locking

All of that is explained in the documentation for flock().

Note that to check for a lock, you'll need to try a flock() with LOCK_NB, otherwise, if the file is already locked, the flock() will block untill the lock is released.

Replies are listed 'Best First'.
Re^2: File locking
by Nalina (Monk) on May 09, 2007 at 11:46 UTC
    But in my case I have some processes running in parallel and accessing the same file. Suppose Process1 is writing to a file, it should lock the file. and when procees2 has to write into the same file, it should check if the file is locked, if so wait till it is released and then write into it. How to implement this?
      If you lock a file for writing, it will automatically wait untill any other lock is released. In other words you can do this in both processes and it'll work:
      use Fcntl qw(:flock); # import LOCK_* constants open F,">>",$somefile or die $!; flock(F,LOCK_EX); # lock exclusively - waits for lock # write stuff here close(F); # close() will release the lock.
      A reply falls below the community's threshold of quality. You may see it by logging in.