home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!think.com!sdd.hp.com!ux1.cso.uiuc.edu!news.cso.uiuc.edu!chappell
- From: chappell@symcom.math.uiuc.edu (Glenn Chappell)
- Subject: flock bug? man page error?
- Message-ID: <By51CH.5G9@news.cso.uiuc.edu>
- Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
- Organization: Math Dept., University of Illinois at Urbana/Champaign
- Date: Sun, 22 Nov 1992 22:05:03 GMT
- Lines: 72
-
- I am writing a utility in Perl which will need to use file locking.
-
- The section of the Perl man page describing the "flock" command gives a
- mailbox appender for BSD systems. As a test, I wrote the following
- program, slightly modified from the one given in the man page. It
- appends the line "test" to the file whose name is given to it as a
- command line parameter. It uses file locking to make sure that it still
- works if you run several copies of it at once.
-
- Unfortunately, this program doesn't work properly. When I run 32 copies
- of it more-or-less simultaneously, somewhere around 22 "test" lines get
- appended to the given file. However, if I change the "print" to a
- "syswrite", it works, and I get 32 appended lines.
-
- Specifically, I changed
-
- print FILE $msg;
-
- to
-
- syswrite(FILE, $msg, length($msg));
-
- and it worked. Do any of you wonderful people have any idea what's going
- on here? Does this mean the man page needs to be changed?
-
- The program:
-
- ----------------------------------------
- #!/usr/local/bin/perl
-
- $LOCK_SH = 1;
- $LOCK_EX = 2;
- $LOCK_NB = 4;
- $LOCK_UN = 8;
-
- sub lock {
- flock(FILE,$LOCK_EX);
- # and, in case someone appended
- # while we were waiting...
- seek(FILE, 0, 2);
- }
-
- sub unlock {
- flock(FILE,$LOCK_UN);
- }
-
- open(FILE, ">>$ARGV[0]")
- || die "Can't open file: $!";
-
- do lock();
- $msg = "test\n";
- print FILE $msg;
- do unlock();
- ----------------------------------------
-
- Glenn Chappell <><
-
- P.S. Versions & such:
-
- The box in front of me says "Sun 3/50" on it. I'm running SunOS 4.1.1_U1.
-
- perl -v saith:
-
- ]This is perl, version 4.0
- ]
- ]$RCSfile: perl.c,v $$Revision: 4.0.1.6 $$Date: 91/11/11 16:38:45 $
- ]Patch level: 19
- ]
- ]Copyright (c) 1989, 1990, 1991, Larry Wall
- ]
- ]Perl may be copied only under the terms of either the Artistic License or the
- ]GNU General Public License, which may be found in the Perl 4.0 source kit.
-