in reply to Re: File locking
in thread File locking

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?

Replies are listed 'Best First'.
Re^3: File locking
by Joost (Canon) on May 09, 2007 at 11:51 UTC
    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.