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

Re: flock() on dup'ed filehandle is not enforced

Thread Previous | Thread Next
Gurusamy Sarathy
March 28, 2001 11:05
Re: flock() on dup'ed filehandle is not enforced
Message ID:
On Wed, 28 Mar 2001 13:29:19 EST, Mark-Jason Dominus wrote:
>Gwyn Judd says in clp.misc that the problem is as follows: The Perl 
>        open STDOUT, ">>&F"
>operation does not correspond to 
>        close(1);
>        dup2(6, 1);             /* here fileno(F) == 6 */
>Instead, it does:
>        dup(6);                 /* yields 7 */
>        dup2(7, 1);
>        close(7);                  
>According to Gwyn, the close(7) releases the lock on the file, and
>this semantics is mandated by POSIX.  If this is true, then it only
>works on Linux because of a bug in Linux!

I'd claim it is a bug in POSIX.

To me, the behavior on Linux makes more sense (assuming it has the
refcounting behavior) since it matches the behavior on unlink(), viz.,
inode is only removed after all the fds pointing to the inode are
closed.  According to the manpages on Solaris, dup() and dup2() create
additional references to the same lock, but close() closes the lock if
any of those fds is closed.  This means writing through any of those 
remaining fds will silently corrupt the file.  Blech.

Anyway, I think you can avoid the extra dup by doing:

    open STDOUT, ">>&=" . fileno(F);


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