Note: The flock call will block until the lock can be acquired.use Fcntl ':flock'; sub alter_file { open(HANDLE1, ...); flock(HANDLE1, LOCK_EX) # exclusive lock or die "unable to obtain lock: $!"; chop; chop; chop; close(HANDLE); # also releases lock } sub process { open(HANDLE2, ...); flock(HANDLE2, LOCK_SH) # shared lock - allows multiple readers or die "unable to obtain shared lock: $!"; ...read from file... close(HANDLE2); # also releases lock }
Another approach which may work for you is to use an atomic operation to replace the contents of file:
In this case no call to flock is needed in the process subroutine. Other processes accessing file may not always get the latest version of the file, but once they open file the contents won't change on them.sub alter_file { open(HANDLE1, ">/some/tmp/file"); ...write to HANDLE1... close(HANDLE1); rename("/some/tmp/file", "file"); }
In reply to Re: Two questions on interprocess communication
by pc88mxer
in thread Two questions on interprocess communication
by cypress
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |