http://qs1969.pair.com?node_id=1043858

The_Dj has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks.
I can't believe no-one has solved this so it must be that my google-fu has failed me.

Simple lockfile for concurrency.
use Fcntl ':flock'; open (LF,'>>',$file); flock (LF,LOCK_EX|LOCK_NB) or die "Just one at a time, please"; select LF; $|=1; print "$$\n"; select STDOUT; ...snip... truncate LF,0; close LF; unlink $file; __END__
This is on Windows 2003/ActivePerl 5.8, and the locking works.
The problem is...
Sometimes the process needs to be killed, and for that someone needs to read that file.
Notepad works fine, but for some reason the people who do this try to open the file in vim (despite being windows people :-|)
Vim honors the exclusive lock, and shows no content.
Vim will read the file if it's only a shared lock.

If I just flock (LF,LOCK_SH); the exclusive lock remains.
But if I flock (LF,LOCK_UN); flock(LF,LOCK_SH); then I risk a race condition.
A random stab at flock (LF,LOCK_UN|LOCK_SH) didn't work either.
I can't install extra modules because reasons.

Thanks for any help!

Replies are listed 'Best First'.
Re: File locking; downgrade an exclusive to a shared lock
by davido (Cardinal) on Jul 12, 2013 at 03:20 UTC

    Use the locked file for essentially nothing other than the Highlander lock. After obtaining the lock, write the most recent pid to a separate logfile that isn't flocked.

    The logfile could have the PID written into it and then be closed immediately. No other process will write to your logfile because the semaphore file is locked, assuring "only one." But when you find that you need to kill your process, you can open the log file and inspect it, since it's not actually the file that is getting locked.


    Dave

      I'd prefer not to create another file if it can be helped...

      I've seen it done where you lock DATA, but that prevents the next instance from running, so it can't log "Run skipped", which is pretty much mandatory for me.

      Any other Ideas?

Re: File locking; downgrade an exclusive to a shared lock
by Anonymous Monk on Jul 12, 2013 at 08:41 UTC
Re: File locking; downgrade an exclusive to a shared lock
by mtmcc (Hermit) on Jul 12, 2013 at 08:57 UTC
    I don't have much experience with flock, but I thought the OS cleared any locks automaticaly when the flocking process was killed? But I might be wrong.

    Does this help?

    - Michael

Re: File locking; downgrade an exclusive to a shared lock
by Anonymous Monk on Jul 12, 2013 at 05:01 UTC

    Hi,

    Delete all the copies of vim?

    J.C.

      +1 to lateral thinking.
      -5 to server usability.

        Another lateral approach, then. Add into your script a write of the PID to a second, non-locked file after it has successfully gained the exclusive lock. Then your kill-script just needs to read the second, unlocked file and compare its timestamp to that of the locked file (to make sure the second file is not stale).

        Caveat: I'm not a Windows user, so this may be no use at all on that platform.