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

I have been coding in perl off/on for two years now and am fairly comfortable with it on Win32 systems. I recently aqcuired responsibility for some *NIX systems and while creating some sample code to teach file locking, i found that it did not work on any (AIX,Solaris) of them. Symptoms are exactly the same:

I cannot get sleep or select(undef,undef,undef,250) to work in the while loop; my cursor just sits idle and i do not see the prints to STDOUT that are coded in the loop .... as soon as I comment out sleep/select it works fine.

BTW - this same script works on Win32.
#!/usr/bin/perl -w use strict; use Fcntl ':flock'; my $file = '/export/home/smchugh/lock_file.txt'; print FILE "\nI HAVE LOCKED YOU @ @{[scalar localtime]}\n"; open (FILE, ">>$file") || die "Couldn't open $file.\n"; flock(FILE, LOCK_EX | LOCK_NB) || die "Couldn't lock $file.\n"; seek(FILE,0,2); my $loop_var = 0; while (1) { print "File $file is locked." unless $loop_var; select(undef,undef,undef, 250); print "." if $loop_var; $loop_var = 1; }

Edit Masem 2002-02-19 - Changed title from "losing confidence..."

Replies are listed 'Best First'.
Re: losing confidence...
by Zaxo (Archbishop) on Feb 14, 2002 at 06:48 UTC

    Some old flavors of *nix don't like 4-arg select with all fileno masks undefined. Setting the first arg to zero should satisfy them. On most, select(undef,undef,undef, 250); will sleep for two hundred and fifty seconds.

    I suspect you are seeing nothing on STDOUT because autoflush is not enabled. $|=1; will fix that.

    Your infinite loop has no escape clause. Are you relying on $SIG{TERM} to happen sometime?

    Update: Btw, you're printing to FILE before you open it. I assume that's a paste error

    After Compline,
    Zaxo