Ask a Question related to PERL Miscellaneous, Design and Development.
-
Sun Guonian #1
flock() on different Unix platform
Hi,
I want to Perl flock() between to process to work with one output
data file.
But on different Unix platform, I got different results.
As followed, on Solaris7, It could work. (compile the perl-5.8.0
with default options)
But on Redhat Linux(7.3/8.0/9.0 for Intel), it couldn't work.
the output file is messed, mixed with ('A','B','C','D')
and ('1','2','3','4')
(compile the perl-5.8.0 with default options)
Someone had told me that it is related to the compile
option of Perl, and I haved tried "-Ud_flock", but it still
don't work.
I wonder if my script have any problem or anything else is
wrong.
Any advices is appreciated!
Best Regards,
Sun Guonian
===== On Solaris 7 for SPARC platform =====
% cat lock03.pl
#!/usr/bin/perl
use Fcntl ':flock';
use strict;
my($res);
my($pid);
open(FH, ">lock_out");
if($pid=fork()) {
sleep(1);
$res=mylock();
print "$$: lock res=$res\n";
sleep(5);
writefile('A' x 40);
sleep(5);
writefile('B' x 40);
sleep(5);
writefile('C' x 40);
sleep(5);
writefile('D' x 40);
$res=myunlock();
print "$$: unlock res=$res\n";
}
elsif($pid==0) {
$res=mylock();
print "$$: lock res=$res\n";
sleep(3);
writefile('1' x 40);
sleep(3);
writefile('2' x 40);
sleep(3);
writefile('3' x 40);
sleep(3);
writefile('4' x 40);
$res=myunlock();
print "$$: unlock res=$res\n";
exit(0);
}
sub writefile {
my($var)=@_;
my($max)=80000;
my($i);
for($i=0;$i<$max;$i++) {
print FH "$var\n";
}
}
sub mylock {
flock(FH, LOCK_EX);
seek(FH, 0, 2);
return 1;
}
sub myunlock {
flock(FH, LOCK_UN);
}
% ./lock03.pl
16198: lock res=1
16198: unlock res=1
16197: lock res=1
16197: unlock res=1
% wc lock_out
640000 640000 26240000 lock_out
% uniq lock_out > kkk
% cat kkk
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
3333333333333333333333333333333333333333
4444444444444444444444444444444444444444
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
===== On Solaris 7 for SPARC platform =====
Sun Guonian Guest
-
#38749 [NEW]: odbc_connect failing on Unix platform
From: david dot rothwell at wolseley dot co dot uk Operating system: SunOS 5.9 PHP version: 5.1.6 PHP Bug Type: ODBC related... -
Need help with a simple UNIX sockets server based on IO::Socket::UNIX
Hi. I've tried to create a simple client + server that communicate through a unix socket. As with all socket servers, it has a loop where it... -
fonts from platform to platform problem
Does anybody out there have an IBM clone with shockwave on it? (it's a free download like acrobat reader) I've made a director movie in a class I'm... -
non-unix admin pleads for help from unix gurus
Hello, I am a network admin working on a project which involves a SCO Unix 5 server provided by another software company. The server provides... -
SCO Registers UNIX(R) Copyrights, Offers UNIX License by ignatius.schwartz-pr.com(Postfix) with ESMTP id 5EF605D87 for <scoannmod@xenitec.on.ca>; Mon, 21
Some code of Windows is based/taken from the UNIX and LINUX environnement system. (Like some DNS, TCP and Internet Explorer things) Can we use... -
Dan Linder #2
Re: flock() on different Unix platform
Judging by the ages of RedHat reported, this is probably a fairly old post. But, just so people who are searching the Internet and come across this, I found a resolution to your problem.
In the initial test code, the lock file is created before the fork() code, and when I run their test code it too creates bad output on my workstation (Ubuntu 10.04.1, Perl 5.10.1).
If we move the lock file creation after the fork (i.e. Parent and Child each create their own file handle to it), the resulting output is correct.
The Perl documentation for flock ([url]http://perldoc.perl.org/functions/flock.html[/url]) alludes to some fork&flock anomalies, but doesn't explain. From some additional research, this appears to be an expected result. If the file handle is created before the fork, both parent and child share the file descriptors and flock doesn't see them as being different:
[url]http://www.perlmonks.org/?node_id=463377[/url]
Hope this helps!
DanL
Junior Member
- Join Date
- Sep 2010
- Posts
- 1



Reply With Quote


