Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Shared flocks on Solaris?

by BazB (Priest)
on May 06, 2003 at 19:57 UTC ( [id://256020]=perlquestion: print w/replies, xml ) Need Help??

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

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:

  • Is there a way to make both shared and exclusive locks work using Perl's flock on Solaris?
  • Have I done all that needs to be done with the recompile, or did I miss something?
  • Will I have to resort to Inline::C?
  • If I have to use Inline::C, can I mix plain perl flock() for exclusive locks with Inline::C code for shared locks, or am I better just handling all locking from the C side? (The latter is tidier, if nothing else).

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.

Replies are listed 'Best First'.
Re: Shared flocks on Solaris?
by bluto (Curate) on May 07, 2003 at 17:26 UTC
    Your code works fine for me on Solaris if I open the file for reading rather than writing (i.e. use "< $file") assuming the file already exists of course. The Solaris man page I have access to seems to indicate you must open the file for reading to use shared locks. You may want to explore opening the file for read and write if you need both kinds of locks. Update: In other words, try something like "+< $file" or use sysopen() and it's ilk instead.

    bluto

      It's always obvious when you know how to do it :-)

      bluto++ is right.

      After I reinvented the wheel using some C and the wonderful Inline::C, and more importantly spent more time grokking manpages, bluto came along and fixed my silly mistake :-).


      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.

Re: Shared flocks on Solaris?
by Anonymous Monk on May 07, 2003 at 21:35 UTC
    OK!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://256020]
Approved by Enlil
Front-paged by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found