develooper Front page | perl.perl5.porters | Postings from September 2003

[perl #23813] misleading example in perldoc -f flock

Thread Previous | Thread Next
From:
perlbug-followup
Date:
September 15, 2003 12:50
Subject:
[perl #23813] misleading example in perldoc -f flock
Message ID:
rt-23813-64593.7.85860840212038@rt.perl.org
# New Ticket Created by  nick@cleaton.net 
# Please include the string:  [perl #23813]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=23813 >


This is a bug report for perl from nick@cleaton.net,
generated with the help of perlbug 1.34 running under perl v5.8.0.


-----------------------------------------------------------------
[Please enter your report here]

The output of "perldoc -f flock" includes the following example
code:


       Here's a mailbox appender for BSD systems.

           use Fcntl ':flock'; # import LOCK_* constants

           sub lock {
               flock(MBOX,LOCK_EX);
               # and, in case someone appended
               # while we were waiting...
               seek(MBOX, 0, 2);
           }

           sub unlock {
               flock(MBOX,LOCK_UN);
           }

           open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
                   or die "Can't open mailbox: $!";

           lock();
           print MBOX $msg,"\n\n";
           unlock();


... which appears to have been written by someone under the
misapprehension that opening a file for append is the same as
opening for write and then seeking to the end.

This is not the case.  When a file is opened in append mode, all
writes are appended to the then end of file, irrespective of the
current file position.  From fopen(3) on FreeBSD:

``a''  Open for writing.  The file is created if it does not
       exist.  The stream is positioned at the end of the file.
       Subsequent writes to the file will always end up at the
       then current end of file, irrespective of any intervening
       fseek(3) or similar.

and the corresponding extract from open(2):

    Opening a file with O_APPEND set causes each write on the
    file to be appended to the end.

I've confirmed this behavior on FreeBSD by adding a sleep to the
example and running several copies of it in parallel.  The
messages are all appended to the end with the seek() commented
out, or even replaced with a seek() to the middle or the start of
the file.

Reading this example a couple of years ago left me with a false
impression of how O_APPEND works, and I've only just now stumbled
across the truth.

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=docs
    severity=low
---
Site configuration information for perl v5.8.0:

Configured by nick at Mon Jul 22 22:30:59 BST 2002.

Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
  Platform:
    osname=freebsd, osvers=4.6-release-p1, archname=i386-freebsd
    uname='freebsd lt1.cleaton.net 4.6-release-p1 freebsd 4.6-release-p1 #0: mon jul 1 11:11:48 bst 2002 root@:asyncobjasyncsrcsyslaptop i386 '
    config_args='-des -Dprefix=/u/usr/local/perl-5.8.0'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -I/usr/local/include',
    optimize='-O',
    cppflags='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='2.95.3 20010315 (release) [FreeBSD]', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags ='-Wl,-E  -L/usr/local/lib'
    libpth=/usr/lib /usr/local/lib
    libs=-lgdbm -lm -lc -lcrypt -lutil
    perllibs=-lm -lc -lcrypt -lutil
    libc=, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-DPIC -fpic', lddlflags='-shared  -L/usr/local/lib'

Locally applied patches:
    

---
@INC for perl v5.8.0:
    /u/usr/local/perl-5.8.0/lib/5.8.0/i386-freebsd
    /u/usr/local/perl-5.8.0/lib/5.8.0
    /u/usr/local/perl-5.8.0/lib/site_perl/5.8.0/i386-freebsd
    /u/usr/local/perl-5.8.0/lib/site_perl/5.8.0
    /u/usr/local/perl-5.8.0/lib/site_perl
    .

---
Environment for perl v5.8.0:
    HOME=/home/nick
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin:/home/nick/bin:/home/local/bin:/home/local/bin:/home/local/bin
    PERL_BADLANG (unset)
    SHELL=/usr/local/bin/bash


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About