Greetings, follow Monks.

I've been tinkering with some code which flock()s a semaphore file.

With some help from bart in the CB, it appears that shared locks are not available. To quote perldoc -f flock:

Note that the emulation built with lockf(3) doesn't provide shared locks, and it requires that FILEHANDLE be open with write intent. These are the semantics that lockf(3) implements. Most if not all systems implement lockf(3) in terms of fcntl(2) locking, though, so the differing semantics shouldn’t bite too many people.
Unfortunately, I seem to have been bitten. I need both exclusive (LOCK_EX) and shared locks (LOCK_SH).
Changing the permissions on the open statement also causes exclusive locks to fail, as foretold by the perldoc.

I tried recompiling my Perl binary with sh Configure -Ud_flock, but the problem persists.

The test code is shown below. It works as expected on Linux, but not Solaris. I have tried both 5.6.1 and 5.8.0.

On Solaris, the code fails with a Bad file number error.

#!/usr/bin/perl use strict; use warnings; use Fcntl qw(:flock); my $file = shift; open (F, "> $file") or die "Unable to open $file: $!"; my $lock_p = flock(F, LOCK_SH | LOCK_NB); if ($lock_p) { print "Got shared lock on semaphore file!"; my $wait = <>; # For testing - hold lock. flock(F, LOCK_UN) or warn "Problems unlocking $file: $!"; exit 0; } else { die "Ack! Didn't get a shared lock on $file: $!"; }

My questions are as follows:

Cheers in advance,

BazB


If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
That way everyone learns.


In reply to Shared flocks on Solaris? by BazB

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.