develooper Front page | perl.perl5.porters | Postings from March 2001

flock() on dup'ed filehandle is not enforced

Thread Next
Mark-Jason Dominus
March 28, 2001 00:31
flock() on dup'ed filehandle is not enforced
Message ID:

        use Fcntl ':flock';
        open F,">>lock" or die $!;
        flock F,LOCK_EX|LOCK_NB or die "lock fail\n";
        open STDOUT,">>&F" or die $!;
        print sleep 1 for 1..10;
        print "\n";

If you run two instances of this program simultaneously, the first
instance gets the lock.   The second instance should die with "lock

Perl 5.6 on linux 2.2.16 behaves correctly, but Perl 5.005_03 on
Solaris does not.  On Solaris, neither process dies.  I have attached
Perl -V output for the perl/solaris combination that fails.

John Lin reports that 5.6 also fails on his solaris and HPUX systems.
You can contact him for more details.  (John brought this to my
attention; I did not find it myself.)  

I wrote a corresponding C program to confirm that the fault was in
Perl and not in the OS:

        #include <fcntl.h>
        #include <sys/file.h>
        #include <stdio.h>
        void die(char *s) {
          printf("%s\n", s);
        int main(void)
          int fd = open("/tmp/lockfile", O_WRONLY|O_CREAT|O_TRUNC,0666);
          if (fd < 0) die("open failed");
          if (flock(fd, LOCK_EX|LOCK_NB) < 0) die("lock failed");
          fprintf(stderr, "locked.\n");
          return 0;
Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
    osname=solaris, osvers=2.5.1, archname=sun4-solaris
    uname='sunos 5.5.1 generic_103640-19 sun4u sparc sunw,ultra-enterprise '
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
    cc='gcc', optimize='-O', gccversion=
    cppflags='-I/pkg/include -I/usr/local/include'
    ccflags ='-I/pkg/include -I/usr/local/include'
    stdchar='unsigned char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags ='-L/pkg/lib -L/usr/ucblib -R/pkg/lib:/usr/ucblib:/usr/lib -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib /usr/ccs/lib /pkg/lib /usr/ucblib
    libs=-lsocket -lnsl -lgdbm -ldbm -ldl -lm -lc -lcrypt
    libc=/lib/, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-fPIC', lddlflags='-G -L/pkg/lib -L/usr/ucblib -L/usr/local/lib'

Characteristics of this binary (from libperl): 
  Built under solaris
  Compiled at Apr  5 1999 12:14:30

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About