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

Dear Gracious Monks,

I am setting up some software which requires Perl 5.0 or greater, on a Mac OS X Server 10.2 system (which ships with perl v5.6.0 built for darwin). This software that I'm using asserts that it runs a test to determine whether or not the version of perl in use on one's system (in this case the packaged perl 5.6.0 for darwin) "locks files correctly" using flock. This software program further asserts:

"Perl5 can appear to be built correctly but still fail to lock files correctly using flock."

Apparently this software program was able to modify perl 5.x to use flock "correctly" by "performing the final link of the perl executable using the BSD compatibility library by using /usr/ucb/cc as the linker" for building perl5 on Solaris 2.5 ... which they further asset that such linking can not be done unless one purchases Sun's "BSD compatability package."

I'm new to Perl but still this issue of flock and requiring to use the cc linker in accordance with something that must be purchased and licensed from Sun seems a bit out of date if not esoteric. I've attempted to contact the licensor of this software that requires perl5 and flock. In the mean time, does anyone know of how to address this problem for FreeBSD / Mac OS X (10.2 or higher)? Are there builds of perl in cyberspace that make use of flock? Thanks for any suggestions.

Serj

Replies are listed 'Best First'.
Re: perl 5+ and flock
by graff (Chancellor) on Dec 15, 2003 at 08:06 UTC
    Maybe the docs you're looking at -- or at least the sections that you quoted -- for this particular software package (whatever it is) were originally written for people using solaris systems, and have not been updated for the readers who use FreeBSD/MacOSX.

    Apparently this software program was able to modify perl 5.x to use flock "correctly" by "performing the final link of the perl executable using the BSD compatibility library by using /usr/ucb/cc as the linker" for building perl5 on Solaris 2.5 ... which they further asset that such linking can not be done unless one purchases Sun's "BSD compatability package."

    Actually, I don't think that software package, in itself, affects a given user's installation of perl at all. The doc here is saying that if the user -- meaning, I think, the user on a solaris system -- happens to be using a perl interpreter whose installation did not meet this certain condition, this software might not work as intended, since the "correct" flock support might not have been properly compiled into the interpreter.

    Of course, if you were on a solaris box, and had installed perl using gcc instead of /usr/ucb/cc, flock might still work quite well -- the quoted material is not specific about conditions that are known to do the wrong thing.

    Since you are on a BSD system, you should have no need whatsoever for a "BSD compatability package" -- that sort of thing would only make sense on non-BSD flavors of unix (e.g. solaris). "Correct" file locking should be a native resource for you.

    In any case, the proof is in the locking. Have you tested flock to see that it works? Have you tried installing this software and running its tests to see if they work?

    In case it helps, I posted a simple "semaphore file" module here (Re: Replacing a string in a file), which I copied out of a Sean Burke article in The Perl Journal a few years back. I have used it to good effect, including as an easy way to test whether flock works as intended on various platforms.

Re: perl 5+ and flock
by Roger (Parson) on Dec 15, 2003 at 07:20 UTC
    I thought the issue with the Sun compiler and linker does not apply to the Mac. You could write a little script that checks whether the version of Perl installed on the Mac OS-X can do 'flock' correctly or not.

    The following is a little script I wrote for testing flocks. The idea is to have one process locks a file, and then have another process trying to truncate it. If the file size gets trimmed when the file is supposed to be locked, then you know there is a problem with flock. It turns out that the version of Perl I am using on the Sun box does not implement flock correctly.
    use strict; use warnings; open FILE, ">x.txt" or die "Cannot create test file: $!"; print FILE "TEXT\n"; close FILE; my $pid = fork(); if ($pid == 0) { # in the child open FC, "<x.txt" or die "Can not open test file"; if (flock(FC, 2) < 0) { die "Can not lock" } for (0..10) { sleep 1; print "$_\n"; die "No lock" if ! -s "x.txt"; } close FC; } else { sleep 5; open FC2, ">x.txt" or print "Can not create file\n"; close FC2; sleep(5); }

      AFAICT your test script only demonstrates that flock is advisory.

Re: perl 5+ and flock
by TStanley (Canon) on Dec 15, 2003 at 19:40 UTC
    Dominus did a short presentation on file locking at YAPC, and has the slides from it posted on his website.

    TStanley
    --------
    The only thing necessary for the triumph of evil is for good men to do nothing -- Edmund Burke