in reply to More Eyes, Please

When flock() hangs and you haven't specified LOCK_NB, then it is very, very likely that another process holds a lock on that file and you are waiting for that lock to clear.

Since the file is named "/tmp/$$.tmp", I can't think what other process would be likely to hold a lock on it. Try the following:

use Fcntl qw( :flock ); #[...] open(TMPA, "+> $tempfile") or die "Could not open tempfile: $!"; flock(TMPA, LOCK_EX|LOCK_NB) or die "ERROR: $!";

FYI, saying "it dies here" makes it sound like the "or die" is triggered and you see the value of $!.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
(Kozz) Re: More Eyes
by Kozz (Friar) on Aug 22, 2000 at 08:52 UTC
    tye, Thanks for the input. I tried your suggestion, and when configured as you suggest, the flock dies with the message "Resource temporarily unavailable". Wow. What does that mean? I mean, it says what it says, yeah. But what does it really mean, in terms of flocking? It's quite strange. When I made a temp directory of my own "/home/username/htdocs/tmp" and made it chmod 777, it was able to flock just fine.
    So is there an issue with flocking files which are not in your own userspace (i.e. /tmp or something), perhaps?

    Update: Incidentally, I got this very same result with the script on two different machines: one running Slack 7, and the other running FreeBSD 3.1
    Update 2: I just had to try the simplest version, so I did this:
    #!/usr/bin/perl use Fcntl qw( :flock ); my $tempfile="/tmp/$$.temp"; print "Content-type: text/html\n\n"; print "Testing flocking now.<BR>\n"; open(TMP, "+> $tempfile") or die "Could not open tempfile: $!"; flock(TMP, LOCK_EX|LOCK_NB) or die "no lock: $!"; seek(TMP, 0, 0); print TMP "Blah blah blah"; # just to test close(TMP); unlink $tempfile; print "That seemed to go just fine.\n";
    And it ran just fine. Go figure. I'm quite puzzled.
      Hmmm. Interesting, in some versions of Unix /tmp is held in memory I haven't access to your ?nixs but I wonder if that has a bearing on things?