in reply to Re: Trying to understand flock
in thread Trying to understand flock

Hi,
Thanks for the code.
I had a question about that code.
I do not understand why you would want to truncate the file you are trying to append to ? I also do not understand why
you would use seek . I am really getting confused
I know its just an example. I am just wondering the reasoning behind it?

Replies are listed 'Best First'.
Re^3: Trying to understand flock
by ikegami (Patriarch) on Nov 15, 2010 at 00:41 UTC
    We want to clobber the file, but we don't want to clobber the file until we've locked it. The catch is that one can't lock a file one doesn't have open, so we can't use open ">" to clobber the file. That means we have to start by opening the file without clobbering it (e.g. by using open ">>") and by locking it. Then, we're free to clobber it.
      Thanks for the explanation. I understand now.
Re^3: Trying to understand flock
by aquarium (Curate) on Nov 14, 2010 at 22:54 UTC
    i stay away from file locking as much as possible. for the most part it is advisory locking, so you need to write programs that use flock and read/write clode blocks in an atomic way that prevents race conditions or lockup. there's specific ways you are meant to write perl code when using flock, to avoid said lockups..and the code sometimes looks like the martians have landed. the flock perldoc has some of the gory details. Even with all the contrived code, the possibility of deadlock is still real, and hence it's probably better to avoid the situation altogether.
    i'm sure some of the higher order perlmonks have memorised the particular idioms/code to use, and can write pretty stable code to share a file. personally i don't think it's best practice to share a file via a locking mechanism.
    the hardest line to type correctly is: stty erase ^H