Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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!

In reply to File locking; downgrade an exclusive to a shared lock by The_Dj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-26 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found